<%!
    from kaithem.src.util import url as urlesc
    from kaithem.src import auth
    from kaithem.src import widgets
    from kaithem.src import unitsofmeasure
    import traceback
%>
<%include file="/pageheader.html"/>
<h2>User <span
%if username.startswith('__'):
class="specialentry"\
%endif
>${username}</span></h2>
<title>Edit User: ${username}</title>
<form action="/auth/updateuser/${urlesc(username)}" method="POST" name="user"  autocomplete="off" 

<h3>Basic</h3>
<div class="window paper">
<p class="warning">For performance reasons, your password is stored hashed with an ordinary cryptographic hash, rather than a password strengthening algorithm.
    Do not use weak or reused passwords on public systems!!!!</p>

<div class="stacked-form">
<label>Username<input required="required" id="un" name="username" type="text" value="${username}"></input></label>

<label>(Leave blank=don't change):<input id="pw" name="password" type="password" value="" autocomplete='off'></input></label>

<label>Retype password<input id="retype" name="password2" type="password" value="" autocomplete='off'></input></label>
<label><input name="lanonly" type="checkbox"

%if auth.getUserSetting(username,"restrict-lan"):
    checked="yes"
%endif
>Disallow login from IP outside of LAN</label>

<script>
// Clean up after auto complete that thinks this is a login field.
setTimeout( function(){
document.getElementById('retype').value =''
document.getElementById('pw').value =''
document.getElementById('un').value = "${username}"
}, 1200)

</script>

<label><input name="useSystemPassword" type="checkbox"
    %if auth.Users[username].get('password','')=='system':
        checked="yes"
    %endif
>Use Linux system user(of user with same name) password instead</label>
    

<label><input type="checkbox" name="allowcors"
%if auth.getUserSetting(username, 'allow-cors'):
checked="on"
%endif
>Allow CORS POST access as this user</label>




<label><input name="telemetryalerts" type="checkbox"

    %if auth.getUserSetting(username,"telemetry-alerts"):
        checked="yes"
    %endif
    >Telemetry alerts(Usefult for kiosks)</label>
</div>
</div>



<h3>Groups</h3>
<div class="window margin">
<details class="help"><summary><i class="mdi mdi-help-circle-outline"></i></summary>
User ${username} is a member of the following groups:<br>
(Check or uncheck to set membership)</details>
<div  class="stacked-form">
%for i in sorted(groups):
    <label><input type="checkbox" name="Group${i}" value="true"
    %if i in usergroups:
        checked="yes"
    %endif
    ></input>${i}</label>
%endfor
</div>
</div>

<input value="Save Changes" type="submit">
</div>
</form>


%try:
<h3>Login History <button type="button" onclick="document.getElementById('historybox').style.display='block'">Show</button></h3>
<div class="window paper" style="display: none;"  id="historybox">
%for i in reversed(auth.Users[username]['loginhistory']):
<b>${unitsofmeasure.strftime(i[0])|h}: login from ${i[1]|h}</b><br>
%endfor
</div>
%except:
Err in login history${traceback.format_exc()}
</div>

%endtry



%try:
    <h3>Telemetry<button type="button" onclick="document.getElementById('telemetrybox').style.display='block'">Show</button> </h3>
    <div class="window paper" style="display: none;" id="telemetrybox">
    User ${username} has open WebSocket connections from these addresses:<br>
    <dl>
    %for i in widgets.ws_connections.values():
    %if i.user == username:
        <dt>${i.peer_address|h}(<a href="https://tools.keycdn.com/geo?host=${i._peer_address[0]|u}">Geolocation</a>)</dt>
        <dd>
            <table border="1">
                <tr>
                    <td>Browsing:</td> 
                    <td>${i.pageURL|h}</td>
                </tr>
                <tr>
                    <td>User Agent:</td> 
                    <td>${i.user_agent|h}</td>
                </tr>
                <tr>
                    <td>Connetion ID:</td> 
                    <td>${i.uuid|h}</td>
                </tr>
                %if hasattr(i, 'userState'):
                <tr>
                    <td>User State:</td> 
                    <td>${i.userState|h}</td>
                </tr>
                %endif
                %if hasattr(i, 'screenState'):
                <tr>
                    <td>Screen State:</td> 
                    <td>${i.screenState|h}</td>
                </tr>
                %endif

                %if hasattr(i, 'batteryStatus'):
                <tr>
                    <td><i class="mdi mdi-battery"></i>Battery Status:</td> 
                    <td>${round(i.batteryStatus['level']*100)}%<meter min="0" max="100" low="20" value="${i.batteryStatus['level']*100}"></meter>
                        ${'<i class="mdi mdi-plugin"></i> Charging' if i.batteryStatus['charging'] else "Not Charging"}
                    </td>
                </tr>
                %endif

                <tr>
                    <td>Command:</td>
                    <td><form action="/settings/refreshuserpage/${i.uuid|u}" method="post">
                        <button title="Command the page associated with this connection to refresh itself">Refresh Page</button>
                    </form>

                    </form></td>
                </tr>
            </table>


        </dd>
    %endif
    %endfor
    </div>
%except Exception as e:
${e}
PROBABLY A RACE CONDITION OCCURED, RELOAD FOR TELEMETRY
</div>
%endtry


<%include file="/pagefooter.html"/>
