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

<div class="cover">
    <h1 class="zone xl margin-bottom-large margin-top">
        Users
    </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-service="users.createUser"
                    data-event="submit"
                    data-success="alert,trigger,reset"
                    data-success-param-alert-text="Created user successfully"
                    data-success-param-trigger-events="user-create,modal-close"
                    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 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.listUsers"
                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">
                        <button class="fill" title="Search" aria-label="Search"><i class="icon-search"></i></button>
                    </div>
                </div>
            </form>

            <div
                data-service="users.listUsers"
                data-event="load,user-create,user-update,user-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">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}}" style="display: none;">
                    <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 width="220">Email</th>
                                    <th>Status</th>
                                    <th width="100">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.$uid}}&project={{router.params.project}}" data-ls-bind="{{user.name}}"></a>
                                    </td>
                                    <td data-title="Email: ">
                                        <small data-ls-bind="{{user.email}}"></span>
                                    </td>
                                    <td data-title="Status: ">
                                        <span data-ls-if="{{user.confirm}} === true">
                                            &nbsp;<span class="tag green">Verified</span>
                                        </span>

                                        <span data-ls-if="{{user.confirm}} !== true">
                                            &nbsp;<span class="tag">Unverified</span>
                                        </span>

                                        <span data-ls-if="{{user.status}} === <?php echo \Auth\Auth::USER_STATUS_BLOCKED; ?>">
                                            &nbsp;<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.listUsers"
                        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.listUsers"
                        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="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-service="teams.createTeam"
                    data-event="submit"
                    data-success="alert,trigger,reset"
                    data-success-param-alert-text="Created team successfully"
                    data-success-param-trigger-events="filter-teams-changed,team-create,modal-close"
                    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" />

                    <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.listTeams"
                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">
                        <button class="fill" title="Search" aria-label="Search"><i class="icon-search"></i></button>
                    </div>
                </div>
            </form>

            <div
                data-service="teams.listTeams"
                data-event="load,team-create,team-update,team-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" style="display: none;">
                    <h3 class="margin-bottom">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}}" style="display: none;">
                <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 width="180">Name</th>
                                    <th>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="modal close" data-button-text="{{team.name}}" data-button-class="link">
                                            <h1>Update Team</h1>

                                            <form
                                                data-service="teams.updateTeam"
                                                data-scope="sdk"
                                                data-event="submit"
                                                data-success="alert,trigger"
                                                data-success-param-alert-text="Updated team successfully"
                                                data-success-param-trigger-events="team-update,modal-close"
                                                data-failure="alert"
                                                data-failure-param-alert-text="Failed to update team"
                                                data-failure-param-alert-classname="error">

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

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

                                                <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}} Users"></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.listTeams"
                        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.listTeams"
                        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>Providers</h2>
            
            <div class="box margin-bottom margin-top-large"
                data-service="projects.getProject"
                data-event="load,platform-create,platform-update,platform-delete"
                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; } ?>
                        <li class="clear <?php echo (isset($data['enabled']) && !$data['enabled']) ? 'dev-feature' : ''; ?>">
                            <div data-ui-modal class="modal close" data-button-text="Settings" data-button-class="pull-end">
                                <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

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

                                <form
                                    data-service="projects.updateProjectOAuth"
                                    data-scope="console"
                                    data-event="submit"
                                    data-param-project-id="{{router.params.project}}"
                                    data-success="alert,trigger"
                                    data-success-param-alert-text="Updated project OAuth settings successfully"
                                    data-success-param-trigger-events="project-update,modal-close"
                                    data-failure="alert"
                                    data-failure-param-alert-text="Failed to update project OAuth 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="oauth<?php echo ucfirst($provider); ?>Appid">App ID</label>
                                    <input name="appId" id="oauth<?php echo ucfirst($provider); ?>Appid" type="text" autocomplete="off" data-ls-bind="{{console-project.usersOauth<?php echo ucfirst($provider); ?>Appid}}">

                                    <label for="oauth<?php echo ucfirst($provider); ?>Secret">App Secret</label>
                                    <input name="secret" id="oauth<?php echo ucfirst($provider); ?>Secret" type="text" autocomplete="off" data-ls-bind="{{console-project.usersOauth<?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 OAuth 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/auth/oauth/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>

                            <img src="/images/oauth/<?php echo strtolower($provider); ?>.png" alt="<?php echo ucfirst($provider); ?> Logo" class="pull-start provider margin-end" />

                            <span>
                                <?php echo ucfirst($provider); ?>
                                <span data-ls-if="
                                    !{{console-project.usersOauth<?php echo ucfirst($provider); ?>Appid}} ||
                                    !{{console-project.usersOauth<?php echo ucfirst($provider); ?>Secret}}" data-debug="1">
                                    &nbsp;<span class="tag red">Disabled</span>
                                </span>
                            </span>
                            <p class="margin-bottom-no margin-top-small text-one-liner">
                                <a href="<?php echo $data['developers']; ?>" target="_blank" rel="noopener">OAuth Developer Docs<i class="icon-link-ext"></i></a>
                            </p>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </li>
    </ul>
</div>