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

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

            <span data-ls-bind="{{team.name}}">&nbsp;</span>
            <span data-ls-if="{{team.name|escape}} === ''">Unknown</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="{{team}}" 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/teams/team?id={{router.params.id}}&project={{router.params.project}}">
                <h2>Overview</h2>

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

                        <div class="box margin-bottom-large">
                            <form
                                data-analytics
                                data-analytics-activity
                                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">

                                <input data-ls-attrs="id=team-id-{{team.$id}}" name="teamId" type="hidden" disabled data-ls-bind="{{team.$id}}" />

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

                                <hr />

                                <button>Update</button>
                            </form>
                        </div>

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

                        <div
                            data-service="teams.getMemberships"
                            data-event="load,teams.create,teams.update,teams.delete,teams.deleteMembership,teams.createMembership"
                            data-param-team-id="{{router.params.id}}"
                            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-members">

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

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

                            <div data-ls-if="0 != {{project-members.total}}">
                                <div class="margin-bottom-small margin-end-small text-align-end text-size-small margin-top-negative text-fade"><span data-ls-bind="{{project-members.total}}"></span> memberships found</div>

                                <div class="box margin-bottom">
                                    <ul data-ls-loop="project-members.memberships" data-ls-as="member" 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 Team Membership"
                                                data-service="teams.deleteMembership"
                                                data-event="submit"
                                                data-success="alert,trigger"
                                                data-confirm="Are you sure you want to remove that user from the team?"
                                                data-success-param-alert-text="Member Removed Successfully"
                                                data-success-param-trigger-events="teams.deleteMembership"
                                                data-failure="alert"
                                                data-failure-param-alert-text="Failed to Remove Member"
                                                data-failure-param-alert-classname="error">

                                                <input name="teamId" data-ls-attrs="id={{member.$id}}" type="hidden" data-ls-bind="{{router.params.id}}">
                                                <input name="membershipId" data-ls-attrs="id=leave-membershipId-{{member.$id}}" type="hidden" data-ls-bind="{{member.$id}}">

                                                <button class="danger">Remove</button>
                                            </form>

                                            <img src="" data-ls-attrs="src={{member.userName|avatar}}" data-size="200" alt="User Avatar" class="avatar pull-start margin-end" loading="lazy" width="60" height="60" />

                                            <div class="margin-bottom-tiny">
                                                <a data-ls-attrs="href=/console/users/user?id={{member.userId}}&project={{router.params.project}}"><span data-ls-bind="{{member.userName}}"></span></a> &nbsp;&nbsp;
                                                <span data-ls-if="1 == {{member.roles.length}}" class="text-fade tooltip" data-ls-bind="{{member.roles.length}} role" data-ls-attrs="data-tooltip={{member.roles|arraySentence}}"></span>
                                                <span  data-ls-if="2 <= {{member.roles.length}}" class="text-fade tooltip" data-ls-bind="{{member.roles.length}} roles" data-ls-attrs="data-tooltip={{member.roles|arraySentence}}"></span>
                                            </div>
                                            <small class="text-size-small text-fade" data-ls-bind="{{member.userEmail}}"></small> &nbsp;&nbsp;<span data-ls-if="false === {{member.confirm}}" class="text-danger text-size-small">Pending Approval</span>
                                        </li>
                                    </ul>
                                </div>
                            </div>

                            <div data-ui-modal class="box modal close" data-button-text="Add Member" data-button-class="pull-start">
                                <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

                                <h1>Add Member</h1>

                                <form name="teams.createTeamMembership"
                                    data-analytics
                                    data-analytics-activity
                                    data-analytics-event="submit"
                                    data-analytics-category="console"
                                    data-analytics-label="Create Team Membership"
                                    data-service="teams.createMembership"
                                    data-event="submit"
                                    data-loading="Adding user, please wait..."
                                    data-success="alert,trigger,reset"
                                    data-success-param-alert-text="User added successfully"
                                    data-success-param-trigger-events="teams.createMembership"
                                    data-failure="alert"
                                    data-failure-param-alert-text="Failed to add user"
                                    data-failure-param-alert-classname="error">

                                    <input name="teamId" id="team-teamId" type="hidden" data-ls-bind="{{team.$id}}">
                                    <input name="url" type="hidden" data-ls-bind="{{env.ENDPOINT}}" />

                                    <label for="email">Email</label>
                                    <input name="email" id="email" type="email" autocomplete="email" required>

                                    <label for="team-name">Name <small>(optional)</small></label>
                                    <input name="name" id="team-name" type="text" autocomplete="name" maxlength="128" />

                                    <label for="roles">Roles</label>
                                    <input type="hidden" id="team-roles" name="roles" data-forms-tags data-cast-to="json" data-ls-bind="<?php echo $this->escape(json_encode(['owner'])); ?>" placeholder="Add any role you like" />

                                    <hr />

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

                            <div class="clear text-align-center paging pull-end">
                                <form
                                    data-service="teams.getMemberships"
                                    data-event="submit"
                                    data-param-team-id="{{router.params.id}}"
                                    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-members"
                                    data-success="state"
                                    data-success-param-state-keys="search,offset">
                                    <button name="offset" data-paging-back data-offset="{{router.params.offset}}" data-total="{{project-members.total}}" 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-members.total|pageTotal}}"></span>

                                <form
                                    data-service="teams.getMemberships"
                                    data-event="submit"
                                    data-param-team-id="{{router.params.id}}"
                                    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-members"
                                    data-success="state"
                                    data-success-param-state-keys="search,offset">
                                    <button name="offset" data-paging-next data-offset="{{router.params.offset}}" data-total="{{project-members.total}}" class="margin-start round small" aria-label="Next"><i class="icon-right-open"></i></button>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="col span-4 sticky-top">
                        <label>Team ID</label>
                        <div class="input-copy margin-bottom">
                            <input id="uid" type="text" autocomplete="off" placeholder="" data-ls-bind="{{team.$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"
                                    data-analytics
                                    data-analytics-event="click"
                                    data-analytics-category="console"
                                    data-analytics-label="View as JSON (Team)">
                                    View as JSON
                                </button>
                            </li>
                            <li class="margin-bottom-small"><i class="icon-angle-circled-right margin-start-tiny margin-end-tiny"></i> Created: <span data-ls-bind="{{team.$createdAt|dateText}}"></span></li>
                        </ul>

                        <form name="teams.delete" class="margin-bottom"
                            data-analytics
                            data-analytics-activity
                            data-analytics-event="submit"
                            data-analytics-category="console"
                            data-analytics-label="Delete Team"
                            data-service="teams.delete"
                            data-event="submit"
                            data-confirm="Are you sure you want to delete this team?"
                            data-param-team-id="{{router.params.id}}"
                            data-success="alert,trigger,redirect"
                            data-success-param-alert-text="Team deleted successfully"
                            data-success-param-trigger-events="users.update"
                            data-success-param-redirect-url="/console/users/teams?project={{router.params.project}}"
                            data-failure="alert"
                            data-failure-param-alert-text="Failed to delete team"
                            data-failure-param-alert-classname="error">

                            <button type="submit" class="danger fill">Delete Team</button>
                        </form>
                    </div>
                </div>
            </li>
            <li data-state="/console/users/teams/team/audit?id={{router.params.id}}&project={{router.params.project}}">
                <h2>Activity</h2>

                <div
                    data-service="teams.listLogs"
                    data-name="logs"
                    data-param-team-id="{{router.params.id}}"
                    data-event="load,logs-load">

                    <div class="box margin-top margin-bottom" data-ls-if="{{logs.logs.length}} === 0" style="display: none" class="margin-top-xxl margin-bottom-xxl text-align-center">
                        <h3 class="text-bold margin-bottom-no">No logs available.</h3>
                    </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.className='avatar hide'" data-ls-if="{{log.clientCode|lowercase}} !== 'cli'" 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" />
                                        
                                        <img onerror="this.onerror=null;this.className='avatar hide'" data-ls-if="{{log.clientCode|lowercase}} === 'cli'" data-ls-attrs="src=/images/clients/terminal.png?buster=<?php echo APP_CACHE_BUSTER; ?>,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.className='avatar hide'" 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>