<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.sum}}" 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.sum}}">
                                <div class="margin-bottom-small margin-end-small text-align-end text-size-small margin-top-negative"><span data-ls-bind="{{project-members.sum}}"></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-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|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.name}}"></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.email}}"></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="Sending invitation, please wait..."
                                    data-success="alert,trigger,reset"
                                    data-success-param-alert-text="Invitation sent successfully"
                                    data-success-param-trigger-events="teams.createMembership"
                                    data-failure="alert"
                                    data-failure-param-alert-text="Failed to send invite"
                                    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-sum="{{project-members.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-members.sum|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-sum="{{project-members.sum}}" 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">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.dateCreated|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>
            
        </ul>
    </div>
</div>