<?php
$providers = $this->getParam('providers', []);
?>

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

        <span>Users</span>
    </h1>
</div>

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

            <h2>Users</h2>

            <div data-ui-modal class="box modal close" data-button-text="" data-button-class="fly round" data-button-icon="icon-plus">
                <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

                <h1>Create User</h1>

                <form
                    data-analytics-event="submit"
                    data-analytics-category="console"
                    data-analytics-label="Create User"
                    data-service="users.create"
                    data-event="submit"
                    data-success="alert,trigger,reset"
                    data-success-param-alert-text="Created user successfully"
                    data-success-param-trigger-events="users.create"
                    data-failure="alert"
                    data-failure-param-alert-text="Failed to create user"
                    data-failure-param-alert-classname="error">

                    <label for="user-name">Name</label>
                    <input type="text" class="full-width" id="user-name" name="name" required autocomplete="off" />

                    <label for="user-email">Email</label>
                    <input type="email" class="full-width" id="user-email" name="email" required autocomplete="off" />

                    <label for="user-password">Password</label>
                    <input type="password" class="full-width" id="user-password" name="password" required pattern=".{6,}" title="Six or more characters" autocomplete="off" />

                    <hr />

                    <button type="submit">Create</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
                </form>
            </div>

            <form class="box padding-small margin-bottom search"
                data-service="users.list"
                data-event="submit"
                data-param-search="{{router.params.search}}"
                data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                data-param-offset=""
                data-param-order-type="DESC"
                data-scope="sdk"
                data-name="project-users"
                data-success="state"
                data-success-param-state-keys="search,offset">
                <div class="row thin responsive">
                    <div class="col span-10">
                        <input name="search" id="searchUsers" type="search" autocomplete="off" placeholder="Search" class="margin-bottom-no" data-ls-bind="{{router.params.search}}">
                    </div>
                    <div class="col span-2 desktops-only">
                        <button class="fill" title="Search" aria-label="Search"><i class="icon-search"></i></button>
                    </div>
                </div>
            </form>

            <div
                data-service="users.list"
                data-event="load,users.create,users.update,users.delete"
                data-param-search="{{router.params.search}}"
                data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                data-param-offset="{{router.params.offset}}"
                data-param-order-type="DESC"
                data-scope="sdk"
                data-name="project-users">

                <div data-ls-if="0 == {{project-users.sum}}" class="box margin-bottom">
                    <h3 class="margin-bottom-small text-bold">No Users Found</h3>

                    <p class="margin-bottom-no">Create your first user to get started</p>
                </div>

                <div data-ls-if="0 != {{project-users.sum}}">
                    <div class="margin-bottom-small margin-end-small text-align-end text-size-small"><span data-ls-bind="{{project-users.sum}}"></span> users found</div>

                    <div class="box margin-bottom">
                        <table class="vertical">
                            <thead>
                                <tr>
                                    <th width="60"></th>
                                    <th width="180">Name</th>
                                    <th>Email</th>
                                    <th width="120">Status</th>
                                    <th width="120">Joined</th>
                                </tr>
                            </thead>
                            <tbody data-ls-loop="project-users.users" data-ls-as="user">
                                <tr>
                                    <td class="hide">
                                        <img src="" data-ls-attrs="src={{user|gravatar}}" data-size="45" alt="User Avatar" class="avatar pull-start" loading="lazy" width="30" height="30" />
                                    </td>
                                    <td data-title="Name: ">
                                        <a data-ls-attrs="href=/console/users/view?id={{user.$id}}&project={{router.params.project}}">
                                            <span data-ls-bind="{{user.name}}"></span>
                                            <span data-ls-if="{{user.name}} === ''">-----</span>
                                        </a>
                                    </td>
                                    <td data-title="Email: ">
                                        <small data-ls-bind="{{user.email}}"></span>
                                    </td>
                                    <td data-title="Status: ">
                                        <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>

                                        <span data-ls-if="{{user.status}} === <?php echo \Auth\Auth::USER_STATUS_BLOCKED; ?>">
                                            <span class="tag red">Blocked</span>
                                        </span>
                                    </td>
                                    <td data-title="Created: "><small data-ls-bind="{{user.registration|date-text}}"></small></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="clear text-align-center paging">
                    <form
                        data-service="users.list"
                        data-event="submit"
                        data-param-search="{{router.params.search}}"
                        data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                        data-param-order-type="DESC"
                        data-scope="sdk"
                        data-name="project-users"
                        data-success="state"
                        data-success-param-state-keys="search,offset">
                        <button name="offset" data-paging-back data-offset="{{router.params.offset}}" data-sum="{{project-users.sum}}" class="margin-end round small" aria-label="Back"><i class="icon-left-open"></i></button>
                    </form>

                    <span data-ls-bind="{{router.params.offset|pageCurrent}} / {{project-users.sum|pageTotal}}"></span>

                    <form
                        data-service="users.list"
                        data-event="submit"
                        data-param-search="{{router.params.search}}"
                        data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                        data-param-order-type="DESC"
                        data-scope="sdk"
                        data-name="project-users"
                        data-success="state"
                        data-success-param-state-keys="search,offset">
                        <button name="offset" data-paging-next data-offset="{{router.params.offset}}" data-sum="{{project-users.sum}}" class="margin-start round small" aria-label="Next"><i class="icon-right-open"></i></button>
                    </form>
                </div>
            </div>
        </li>

        <li data-state="/console/users/teams?project={{router.params.project}}">
            <h2>Teams</h2>

            <div data-ui-modal class="box modal close" data-button-text="" data-button-class="fly round" data-button-icon="icon-plus">
                <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

                <h1>Create Team</h1>

                <form
                    data-analytics-event="submit"
                    data-analytics-category="console"
                    data-analytics-label="Create Team"
                    data-service="teams.create"
                    data-event="submit"
                    data-success="alert,trigger,reset"
                    data-success-param-alert-text="Created team successfully"
                    data-success-param-trigger-events="filter-teams-changed,teams.create"
                    data-failure="alert"
                    data-failure-param-alert-text="Failed to create team"
                    data-failure-param-alert-classname="error">

                    <label for="team-name">Name</label>
                    <input type="text" class="full-width" id="team-name" name="name" required autocomplete="off" />
                    
                    <hr />

                    <button type="submit">Create</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
                </form>
            </div>

            <form class="box padding-small margin-bottom search"
                data-service="teams.list"
                data-event="submit"
                data-param-search="{{router.params.search}}"
                data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                data-param-offset=""
                data-param-order-type="DESC"
                data-scope="sdk"
                data-name="project-teams"
                data-success="state"
                data-success-param-state-keys="search,offset">
                <div class="row thin responsive">
                    <div class="col span-10">
                        <input name="search" id="searchTeams" type="search" autocomplete="off" placeholder="Search" class="margin-bottom-no" data-ls-bind="{{router.params.search}}">
                    </div>
                    <div class="col span-2 desktops-only">
                        <button class="fill" title="Search" aria-label="Search"><i class="icon-search"></i></button>
                    </div>
                </div>
            </form>

            <div
                data-service="teams.list"
                data-event="load,teams.create,teams.update,teams.delete"
                data-param-search="{{router.params.search}}"
                data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                data-param-offset="{{router.params.offset}}"
                data-param-order-type="DESC"
                data-scope="sdk"
                data-name="project-teams">

                <div data-ls-if="0 == {{project-teams.sum}}" class="box margin-bottom">
                    <h3 class="margin-bottom-small text-bold">No Teams Found</h3>

                    <p class="margin-bottom-no">Create your first team to get started</p>
                </div>

                <div data-ls-if="0 != {{project-teams.sum}}">
                <div class="margin-bottom-small margin-end-small text-align-end text-size-small"><span data-ls-bind="{{project-teams.sum}}"></span> results found</div>

                    <div class="box margin-bottom">
                        <table class="vertical">
                            <thead>
                                <tr>
                                    <th width="60"></th>
                                    <th>Name</th>
                                    <th width="150">Members</th>
                                    <th width="150">Created</th>
                                </tr>
                            </thead>
                            <tbody data-ls-loop="project-teams.teams" data-ls-as="team">
                                <tr>
                                    <td class="hide">
                                        <img src="" data-ls-attrs="src={{team.name|gravatar}}" data-size="45" alt="Collection Avatar" class="avatar margin-end pull-start" loading="lazy" width="30" height="30" />
                                    </td>
                                    <td data-title="Name: ">
                                        <div data-ui-modal class="box modal close" data-button-text="{{team.name}}" data-button-class="link">
                                            <h1>Update Team</h1>

                                            <form
                                                data-analytics-event="submit"
                                                data-analytics-category="console"
                                                data-analytics-label="Update Team"
                                                data-service="teams.update"
                                                data-scope="sdk"
                                                data-event="submit"
                                                data-success="alert,trigger"
                                                data-success-param-alert-text="Updated team successfully"
                                                data-success-param-trigger-events="teams.update"
                                                data-failure="alert"
                                                data-failure-param-alert-text="Failed to update team"
                                                data-failure-param-alert-classname="error">

                                                <label for="name">ID</label>
                                                <div class="input-copy">
                                                    <input name="teamId" data-forms-copy type="text" disabled data-ls-bind="{{team.$id}}" />
                                                </div>

                                                <label for="name">Name</label>
                                                <input name="name" data-ls-attrs="id=name-{{team.$id}}" type="text" autocomplete="off" data-ls-bind="{{team.name}}">

                                                <hr />

                                                <button>Update</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
                                            </form>
                                        </div>
                                    </td>
                                    <td data-title="Members: "><span data-ls-bind="{{team.sum}} members"></span></td>
                                    <td data-title="Date Created: "><small data-ls-bind="{{team.dateCreated|date-text}}"></small></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="clear text-align-center paging">
                    <form
                        data-service="teams.list"
                        data-event="submit"
                        data-param-search="{{router.params.search}}"
                        data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                        data-param-order-type="DESC"
                        data-scope="sdk"
                        data-name="project-teams"
                        data-success="state"
                        data-success-param-state-keys="search,offset">
                        <button name="offset" data-paging-back data-offset="{{router.params.offset}}" data-sum="{{project-teams.sum}}" class="margin-end round small" aria-label="Back"><i class="icon-left-open"></i></button>
                    </form>

                    <span data-ls-bind="{{router.params.offset|pageCurrent}} / {{project-teams.sum|pageTotal}}"></span>

                    <form
                        data-service="teams.list"
                        data-event="submit"
                        data-param-search="{{router.params.search}}"
                        data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                        data-param-order-type="DESC"
                        data-scope="sdk"
                        data-name="project-teams"
                        data-success="state"
                        data-success-param-state-keys="search,offset">
                        <button name="offset" data-paging-next data-offset="{{router.params.offset}}" data-sum="{{project-teams.sum}}" class="margin-start round small" aria-label="Next"><i class="icon-right-open"></i></button>
                    </form>
                </div>
            </div>
        </li>
        
        <li data-state="/console/users/providers?project={{router.params.project}}">
            <h2>OAuth2 Providers</h2>

            <div class="box margin-bottom margin-top-large"
                data-service="projects.get"
                data-event="load,projects.create,projects.update,projects.deleteProject"
                data-name="console-project"
                data-param-project-id="{{router.params.project}}"
                data-scope="console">
                <ul class="list">
                    <?php foreach ($providers as $provider => $data):
                        if (isset($data['enabled']) && !$data['enabled']) { continue; }
                        if (isset($data['mock']) && $data['mock']) { continue; }
                        ?>
                        <li class="clear <?php echo (isset($data['enabled']) && !$data['enabled']) ? 'dev-feature' : ''; ?>">
                            <div data-ui-modal class="modal close" data-button-alias="none" data-open-event="provider-update-<?php echo $provider; ?>">
                                <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

                                <h1><?php echo ucfirst($provider); ?> OAuth2 Settings</h1>

                                <form
                                    data-analytics-event="submit"
                                    data-analytics-category="console"
                                    data-analytics-label="Update Project OAuth2"
                                    data-service="projects.updateOAuth2"
                                    data-scope="console"
                                    data-event="submit"
                                    data-param-project-id="{{router.params.project}}"
                                    data-success="alert,trigger"
                                    data-success-param-alert-text="Updated project OAuth2 settings successfully"
                                    data-success-param-trigger-events="projects.update"
                                    data-failure="alert"
                                    data-failure-param-alert-text="Failed to update project OAuth2 settings"
                                    data-failure-param-alert-classname="error">
                                    <input name="provider" id="provider<?php echo ucfirst($provider); ?>" type="hidden" autocomplete="off" value="<?php echo $provider; ?>">

                                    <label for="oauth2<?php echo ucfirst($provider); ?>Appid">App ID</label>
                                    <input name="appId" id="oauth2<?php echo ucfirst($provider); ?>Appid" type="text" autocomplete="off" data-ls-bind="{{console-project.usersOauth2<?php echo ucfirst($provider); ?>Appid}}">

                                    <label for="oauth2<?php echo ucfirst($provider); ?>Secret">App Secret</label>
                                    <input name="secret" id="oauth2<?php echo ucfirst($provider); ?>Secret" type="text" autocomplete="off" data-ls-bind="{{console-project.usersOauth2<?php echo ucfirst($provider); ?>Secret}}">

                                    <div class="info row thin margin-bottom margin-top">
                                        <div class="col span-1">
                                            <i class="icon-info-circled text-sign"></i>
                                        </div>
                                        <div class="col span-11">
                                            <p>To complete set up, add this OAuth2 redirect URI to your <?php echo ucfirst($provider); ?> app configuration.</p>

                                            <div class="input-copy">
                                                <input data-forms-copy type="text" disabled data-ls-bind="{{env.PROTOCOL}}://{{env.DOMAIN}}/v1/account/sessions/oauth2/callback/<?php echo $provider; ?>/{{router.params.project}}" class="margin-bottom-no" />
                                            </div>
                                        </div>
                                    </div>

                                    <button>Update</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
                                </form>
                            </div>

                            <span data-ls-if="
                                {{console-project.usersOauth2<?php echo ucfirst($provider); ?>Appid}} &&
                                {{console-project.usersOauth2<?php echo ucfirst($provider); ?>Secret}}">
                                <button class="switch on pull-end margin-top-small" data-ls-ui-trigger="provider-update-<?php echo $provider; ?>"></button>
                            </span>
                            <span data-ls-if="
                                !{{console-project.usersOauth2<?php echo ucfirst($provider); ?>Appid}} ||
                                !{{console-project.usersOauth2<?php echo ucfirst($provider); ?>Secret}}">
                                <button class="switch pull-end margin-top-small" data-ls-ui-trigger="provider-update-<?php echo $provider; ?>"></button>
                            </span>

                            <img src="/images/oauth2/<?php echo strtolower($provider); ?>.png" alt="<?php echo ucfirst($provider); ?> Logo" class="pull-start provider margin-end" />
                            <span>
                                <?php echo ucfirst($provider); ?>
                            </span>
                            <p class="margin-bottom-no margin-top-small text-one-liner">
                                <a href="<?php echo $data['developers']; ?>" target="_blank" rel="noopener">OAuth2 Developer Docs<i class="icon-link-ext"></i></a>
                            </p>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </li>
    </ul>
</div>