<div
    data-service="users.get"
    data-name="user"
    data-event="load,users.update"
    data-param-user-id="{{router.params.id}}"
    data-success="trigger"
    data-success-param-trigger-events="users.get">

    <div class="cover">
        <h1 class="zone xl margin-bottom-large">
            <a data-ls-attrs="href=/console/users?project={{router.params.project}}" class="back text-size-small link-return-animation--start"><i class="icon-left-open"></i> Users</a>
            <br />

            <span data-ls-bind="{{user.name}}">&nbsp;</span>
            <span data-ls-if="{{user.name|escape}} === '' && {{user.email}} !== ''">Unknown</span>
            <span data-ls-if="{{user.name|escape}} === '' && {{user.email}} === ''">Anonymous User</span>
        </h1>
    </div>

    <div data-ui-modal class="modal width-large box close" data-button-hide="on" data-open-event="open-json">
        <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>    
        
        <h2>JSON View</h2>

        <div class="margin-bottom">
            <input type="hidden" data-ls-bind="{{user}}" data-forms-code />
        </div>

        <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
    </div>

    <div class="zone xl">
        <ul class="phases clear" data-ui-phases data-selected="{{router.params.tab}}">
            <li data-state="/console/users/user?id={{router.params.id}}&project={{router.params.project}}">
                <h2>Overview</h2>

                <div data-ls-if="{{user.status}} === <?php echo \Appwrite\Auth\Auth::USER_STATUS_BLOCKED; ?>" style="display: none" class="box padding-small danger margin-bottom-xxl text-align-center">
                    This user account is blocked.
                </div>

                <div class="row responsive margin-top-xl">
                    <div class="col span-8">
                        <label>&nbsp;</label>

                        <div class="box margin-bottom-large">
                            <div class="text-align-center">
                                <img src="" data-ls-attrs="src={{user|avatar}}" data-size="200" alt="User Avatar" class="avatar huge margin-top-negative-xxl" />

                                <div class="margin-top-small" data-ls-bind="Member since {{user.registration|dateText}}"></div>
                                <div class="margin-top-small">
                                    <span data-ls-if="{{user.emailVerification}} === true">
                                        <span class="tag green">Verified</span>
                                    </span>
                                    <span data-ls-if="{{user.emailVerification}} !== true">
                                        <span class="tag">Unverified</span>
                                    </span>
                                </div>
                                <div class="margin-top-small" data-ls-bind="{{user.email}}"></div>
                            </div>
                        </div>

                        <h3 class="margin-bottom">Preferences</h3>

                        <div class="box margin-bottom"
                            data-service="users.getPrefs"
                            data-name="user-prefs"
                            data-event="load"
                            data-param-user-id="{{router.params.id}}"
                            data-success="trigger"
                            data-success-param-trigger-events="users.getPrefs">

                            <div data-ls-if="!{{user-prefs|isEmptyObject}}">
                                <table class="vertical">
                                    <thead>
                                        <tr>
                                            <th>Key</th>
                                            <th>Value</th>
                                        </tr>
                                    </thead>
                                    <tbody data-ls-loop="user-prefs" data-ls-as="pref">
                                        <tr>
                                            <td data-title="Key: ">
                                                <span data-ls-bind="{{$index}}"></span>
                                            </td>
                                            <td data-title="Value: ">
                                                <span data-ls-bind="{{pref}}"></span>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>

                            <div data-ls-if="{{user-prefs|isEmptyObject}}">
                                No user preferences found
                            </div>
                        </div>

                        <h3 class="text-danger">Danger Zone</h3>

                        <div class="box danger margin-bottom">
                            <p>This is the area where you can delete this user.</p>
                        
                            <p>By deleting this user you will lose all data associated with this user.</p>
                        
                            <p>PLEASE NOTE: User deletion is irreversible.</p>
                        
                            <form class="inline"
                                data-analytics
                                data-analytics-activity
                                data-analytics-event="submit"
                                data-analytics-category="console"
                                data-analytics-label="Delete User"
                                data-service="users.delete"
                                data-event="submit"
                                data-param-user-id="{{router.params.id}}"
                                data-success="alert,trigger,redirect"
                                data-confirm="Are you sure you want to delete this user?"
                                data-success-param-alert-text="Deleted user successfully"
                                data-success-param-trigger-events="users.delete"
                                data-success-param-redirect-url="/console/users?project={{router.params.project}}"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to delete user"
                                data-failure-param-alert-classname="error">
                                <input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}" />
                        
                                <button class="danger reverse" type="submit">Delete User</button>
                            </form>
                        </div>
                    </div>
                    <div class="col span-4 sticky-top">
                        <label>User ID</label>
                        <div class="input-copy margin-bottom">
                            <input id="uid" type="text" autocomplete="off" placeholder="" data-ls-bind="{{user.$id}}" disabled data-forms-copy>
                        </div>

                        <ul class="margin-bottom-large text-fade text-size-small">
                            <li class="margin-bottom-small"><i class="icon-angle-circled-right margin-start-tiny margin-end-tiny"></i> <button data-ls-ui-trigger="open-json" class="link text-size-small">View as JSON</button></li>
                        </ul>

                        <div data-ls-if="{{user.status}} !== <?php echo \Appwrite\Auth\Auth::USER_STATUS_BLOCKED; ?>" style="display: none">
                            <form name="users.updateStatus" class="margin-bottom"
                                data-analytics
                                data-analytics-activity
                                data-analytics-event="submit"
                                data-analytics-category="console"
                                data-analytics-label="Update User Status"
                                data-service="users.updateStatus"
                                data-event="submit"
                                data-param-user-id="{{router.params.id}}"
                                data-success="alert,trigger"
                                data-success-param-alert-text="Blocked user successfully"
                                data-success-param-trigger-events="users.update"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to block user"
                                data-failure-param-alert-classname="error">

                                <button name="status" type="submit" class="danger fill" value="<?php echo \Appwrite\Auth\Auth::USER_STATUS_BLOCKED; ?>" data-cast-to="integer">Block Account</button>
                            </form>
                        </div>

                        <div data-ls-if="{{user.status}} === <?php echo \Appwrite\Auth\Auth::USER_STATUS_BLOCKED; ?>" style="display: none">
                            <form name="users.updateStatus" class="margin-bottom"
                                data-analytics
                                data-analytics-activity
                                data-analytics-event="submit"
                                data-analytics-category="console"
                                data-analytics-label="Update User Status"
                                data-service="users.updateStatus"
                                data-event="submit"
                                data-param-user-id="{{router.params.id}}"
                                data-success="alert,trigger"
                                data-success-param-alert-text="Activated user successfully"
                                data-success-param-trigger-events="users.update"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to activate user"
                                data-failure-param-alert-classname="error">

                                <button name="status" type="submit" class="fill" value="<?php echo \Appwrite\Auth\Auth::USER_STATUS_ACTIVATED; ?>" data-cast-to="integer">Activate Account</button>
                            </form>
                        </div>
                    </div>
                </div>
            </li>
            <li data-state="/console/users/user/sessions?id={{router.params.id}}&project={{router.params.project}}">
                <h2>Sessions</h2>

                <div
                    data-service="users.getSessions"
                    data-name="sessions"
                    data-param-user-id="{{router.params.id}}"
                    data-event="load,users.update">

                    <div data-ls-if="{{sessions.sessions.length}} === 0" style="display: none" class="margin-top-xxl margin-bottom-xxl text-align-center">
                        No sessions available.
                    </div>

                    <div data-ls-if="{{sessions.sessions.length}} !== 0" style="display: none">
                        <div class="box margin-bottom">
                            <ul data-ls-loop="sessions.sessions" data-ls-as="session" class="list">
                                <li class="clear">
                                    <form class="pull-end"
                                        data-analytics
                                        data-analytics-activity
                                        data-analytics-event="submit"
                                        data-analytics-category="console"
                                        data-analytics-label="Delete User Session"
                                        data-service="users.deleteSession"
                                        data-event="submit"
                                        data-loading="Loading..."
                                        data-success="trigger"
                                        data-success-param-trigger-events="users.update"
                                        data-failure="alert"
                                        data-failure-param-alert-text="Failed to logout session"
                                        data-failure-param-alert-classname="error">

                                        <input type="hidden" name="userId" data-ls-bind="{{router.params.id}}">
                                        <input type="hidden" name="sessionId" data-ls-bind="{{session.$id}}">
                                        <button class="danger">Logout</button>
                                    </form>
                                    <div class="pull-start margin-end avatar-container">
                                        <img onerror="this.onerror=null;this.src='/images/unknown.svg'" data-ls-attrs="src={{env.API}}/avatars/browsers/{{session.clientCode|lowercase}}?width=120&height=120&project={{env.PROJECT}},title={{session.clientName}},alt={{session.clientName}}" class="avatar" loading="lazy" width="60" height="60" />
                                        
                                        <div data-ls-if="{{session.provider}} !== 'email'" class="corner">
                                            <img data-ls-attrs="src=/images/users/{{session.provider}}.png?buster=<?php echo APP_CACHE_BUSTER; ?>,title={{session.provider}},alt={{session.provider}}"  class="avatar xs" loading="lazy" width="30" height="30" />
                                        </div>
                                    </div>

                                    <span data-ls-bind="{{session.clientName}}"></span> <span data-ls-bind="{{session.clientVersion}}"></span> on <span data-ls-bind="{{session.deviceModel}}"></span> <span data-ls-bind="{{session.osName}}"></span> <span data-ls-bind="{{session.osVersion}}"></span>

                                    <div class="margin-top-small">
                                        <img onerror="this.onerror=null;this.src='/images/unknown.svg'" data-ls-if="{{session.countryCode}} !== '--'" data-ls-attrs="src={{env.API}}/avatars/flags/{{session.countryCode}}?width=120&height=120&project={{env.PROJECT}}" class="avatar xxs margin-end-small inline" />
                                        <small data-ls-bind="{{session.ip}}"></small> / <small data-ls-bind="{{session.countryName}}"></small>
                                    </div>
                                </li>
                            </ul>
                        </div>

                        <form class="inline margin-bottom-large"
                            data-analytics
                            data-analytics-activity
                            data-analytics-event="submit"
                            data-analytics-category="console"
                            data-analytics-label="Delete User Sessions"
                            data-service="users.deleteSessions"
                            data-param-user-id="{{router.params.id}}"
                            data-event="submit"
                            data-success="trigger"
                            data-success-param-trigger-events="users.update"
                            data-failure="alert"
                            data-failure-param-alert-text="Failed to logout all sessions"
                            data-failure-param-alert-classname="error">
                            
                            <button class="danger">Logout from all sessions</button>
                        </form>
                    </div>
                </div>

            </li>
            <li data-state="/console/users/user/audit?id={{router.params.id}}&project={{router.params.project}}">
                <h2>Activity</h2>

                <div
                    data-service="users.getLogs"
                    data-name="logs"
                    data-param-user-id="{{router.params.id}}"
                    data-event="load,logs-load">

                    <div data-ls-if="{{logs.logs.length}} === 0" style="display: none" class="margin-top-xxl margin-bottom-xxl text-align-center">
                        No logs available.
                    </div>

                    <div class="box" data-ls-if="{{logs.logs.length}} !== 0" style="display: none">
                        <table class="vertical small">
                            <thead>
                                <tr>
                                    <th width="140">Date</th>
                                    <th width="175">Event</th>
                                    <th>Client</th>
                                    <th width="90">Location</th>
                                    <th width="90">IP</th>
                                </tr>
                            </thead>
                            <tbody data-ls-loop="logs.logs" data-ls-as="log">
                                <tr>
                                    <td data-title="Date: "><span data-ls-bind="{{log.time|dateTime}}"></span></td>
                                    <td data-title="Event: "><span data-ls-bind="{{log.event}}"></span></td>
                                    <td data-title="Client: ">
                                        <img onerror="this.onerror=null;this.src='/images/unknown.svg'" data-ls-if="({{log.clientCode}})" data-ls-attrs="src={{env.API}}/avatars/browsers/{{log.clientCode|lowercase}}?width=80&height=80&project={{env.PROJECT}},title={{log.clientName}},alt={{log.clientName}}" class="avatar xxs inline margin-end-small" />
                                        <span data-ls-if="({{log.clientName}})" data-ls-bind="{{log.clientName}} {{log.clientVersion}} on {{log.model}} {{log.osName}} {{log.osVersion}}"></span>
                                        <div data-ls-if="(!{{log.clientName}})" class="text-align-center text-fade">Unknown</div>
                                    </td>
                                    <td data-title="Location: ">
                                        <img onerror="this.onerror=null;this.src='/images/unknown.svg'" data-ls-if="{{log.countryCode}} !== '--'" data-ls-attrs="src={{env.API}}/avatars/flags/{{log.countryCode}}?width=80&height=80&project={{env.PROJECT}}" class="avatar xxs inline margin-end-small" />
                                        <span data-ls-bind="{{log.countryName}}"></span>
                                    </td>
                                    <td data-title="IP: "><span data-ls-bind="{{log.ip}}"></span></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</div>
