{extends file="admin-layout.tpl"}

{block name="no-return-functions"}
    {$admin_current_location = 'configuration'}
{/block}

{block name="page-title"}{intl l='Back-office users'}{/block}

{block name="check-resource"}admin.configuration.administrator{/block}
{block name="check-access"}view{/block}

{block name="main-content"}
<div>

    <div id="wrapper" class="container">

        <div class="clearfix">
            <ul class="breadcrumb">
                <li><a href="{url path='/admin/home'}">{intl l="Home"}</a></li>
                <li><a href="{url path='/admin/configuration'}">{intl l="Configuration"}</a></li>
                <li><a href="{url path='/admin/configuration/administrators'}">{intl l="Back-office users"}</a></li>
            </ul>
        </div>

        {hook name="administrators.top" location="administrators_top" }

        <div class="row">
            <div class="col-md-12">

                <div class="general-block-decorator">
                    <div class="table-responsive">
                        <table class="table table-striped table-condensed table-left-aligned">
                            <caption class="clearfix">
                                {intl l="Administrators"}
                                {include file='renderer/buttons.html' btn_group=false buttons=[
                                    [
                                        'type' => 'create',
                                        'class' => 'action-btn',
                                        'title' => {intl l='Create a new administrator'},
                                        'href' => '#administrator_create_dialog',
                                        'auth' => ['resource' => 'admin.administrator'],
                                        'data' => [
                                            'toggle' => 'modal'
                                        ]
                                    ]
                                ]}
                            </caption>
                            <thead>
                                <tr>
                                    <th>{intl l="Login"}</th>
                                    <th>{intl l="First Name"}</th>
                                    <th>{intl l="Last Name"}</th>
                                    <th>{intl l="E-mail"}</th>
                                    <th>{intl l="Locale"}</th>
                                    <th>{intl l="Profile"}</th>
                                    {hook name="administrators.header" location="administrator_list_header" }
                                    <th class="col-md-1">{intl l="Actions"}</th>
                                </tr>
                            </thead>
                            <tbody>

                                {loop type="admin" name="administrators" backend_context="1"}

                                {* the current current admin can always update its own profile *}
                                {$can_update = $ID == {admin attr="id"}}
                                {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.administrator" access="UPDATE"}
                                    {$can_update = true}
                                {/loop}

                                <tr class="js-admin-{$ID}">
                                    <td data-field-class="js-login">
                                        {if $can_update}
                                            <a class="js-update-administrator" title="{intl l='Change this administrator'}" href="#administrator_update_dialog" data-id="{$ID}" data-toggle="modal">{$LOGIN}</a>
                                        {else}
                                            {$LOGIN}
                                        {/if}
                                    </td>
                                    <td data-field-class="js-firstname">{$FIRSTNAME}</td>
                                    <td data-field-class="js-lastname">{$LASTNAME}</td>
                                    {* Do not display invalid emails *}
                                    {if $EMAIL|strpos:'@'}
                                        <td data-field-class="js-email">{$EMAIL}</td>
                                    {else}
                                        <td class="text-warning">
                                            <span class="glyphicon glyphicon-exclamation-sign"></span> {intl l="No email defined"}
                                        </td>
                                    {/if}
                                    <td data-field-class="js-locale" data-value="{$LOCALE}">{$LOCALE}</td>
                                    <td data-field-class="js-profile" data-value="{$PROFILE}">
                                       {if $PROFILE}
                                            {loop type="profile" name="admin-profile" id={$PROFILE}}
                                                <a title="{intl l='Afficher ce profil'}" href="{url path="/admin/configuration/profiles/update/%id" id=$ID}">{$TITLE}</a>
                                            {/loop}
                                        {else}
                                            {intl l='Superadministrator'}
                                        {/if}
                                    </td>
                                    {hook name="administrators.row" location="administrator_list_row" admin_id={$ID}}
                                    <td class="actions">
                                       {$buttons = []}

                                        {if $can_update}
                                            {$buttons[] = [
                                                'type' => 'edit',
                                                'class' => 'js-update-administrator',
                                                'title' => {intl l='Change this administrator'},
                                                'href' => '#administrator_update_dialog',
                                                'data' => [
                                                    'id' => $ID,
                                                    'toggle' => 'modal'
                                                ]
                                            ]}
                                        {/if}

                                        {if $ID != {admin attr="id"}}
                                            {loop type="auth" name="can_delete" role="ADMIN" resource="admin.configuration.administrator" access="DELETE"}
                                            {$buttons[] = [
                                                'type' => 'delete',
                                                'class' => 'js-delete-administrator',
                                                'title' => {intl l='Delete this administrator'},
                                                'href' => '#administrator_delete_dialog',
                                                'data' => [
                                                    'id' => $ID,
                                                    'toggle' => 'modal'
                                                ]
                                            ]}
                                        {/loop}
                                        {/if}

                                        {include file='renderer/buttons.html' buttons=$buttons}
                                    </td>
                                </tr>

                                {/loop}

                            </tbody>
                        </table>
                    </div>
                </div>


            </div>
        </div>

        {hook name="administrators.bottom" location="administrators_bottom" }

    </div>
</div>

{* -- Add administrator confirmation dialog ----------------------------------- *}
{form name="thelia.admin.administrator.add"}

{if $form_error_message}
    {$administratorCreateError = true}
{else}
    {$administratorCreateError = false}
{/if}

{* Capture the dialog body, to pass it to the generic dialog *}
{capture "administrator_create_dialog"}

    {form_hidden_fields}

    <div class="row">
        <div class="col-md-6">
            {render_form_field field='login'}
            {render_form_field field='email'}
        </div>
        <div class="col-md-6">
            {render_form_field field='firstname'}
            {render_form_field field='lastname'}
        </div>
    </div>

    {form_field field='password_confirm'}
        {$passwordError = $error}
    {/form_field}

    <div class="row">
        <div class="col-md-6">
            {render_form_field field='password'}
        </div>
        <div class="col-md-6">
            {render_form_field field='password_confirm'}
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            {custom_render_form_field field="locale"}
                <select {form_field_attributes field="locale" no_standard_classes=1} data-toggle="selectpicker">
                    {loop name='lang-admin' type="lang"}
                        <option value="{$LOCALE}">{$LOCALE}: {$TITLE}</option>
                    {/loop}
                </select>
            {/custom_render_form_field}
        </div>
        <div class="col-md-6">
            {custom_render_form_field field="profile"}
                <select {form_field_attributes field="profile" no_standard_classes=1} data-toggle="selectpicker">
                    {foreach $choices as $choice}
                        {if $choice->value == 0}
                            <option value="0">{intl l='Superadministrator'}</option>
                        {else}
                            {loop name='profile' type="profile" id=$choice->value}
                                <option value="{$ID}">{$TITLE}</option>
                            {/loop}
                        {/if}
                    {/foreach}
                </select>
            {/custom_render_form_field}
        </div>
    </div>

    {hook name="administrator.create-form" location="administrator_create_form" }
{/capture}

    {include
        file = "includes/generic-create-dialog.html"

        dialog_id    = "administrator_create_dialog"
        dialog_title = {intl l="Create a new administrator"}
        dialog_body  = {$smarty.capture.administrator_create_dialog nofilter}

        dialog_ok_label     = {intl l="Create"}
        dialog_cancel_label = {intl l="Cancel"}

        form_action        = {url path="/admin/configuration/administrators/add"}
        form_enctype       = {form_enctype}
        form_error_message = $form_error_message
    }

{/form}

{* -- Update administrator confirmation dialog ----------------------------------- *}
{form name="thelia.admin.administrator.update"}

{if $form_error_message}
    {$administratorUpdateError = true}
{else}
    {$administratorUpdateError = false}
{/if}

{* Capture the dialog body, to pass it to the generic dialog *}
{capture "administrator_update_dialog"}

    {form_hidden_fields}

    {if $show_email_change_notice}
        <div class="alert alert-warning">
            {intl l="From Thelia 2.3, you have to <strong>add an email address</strong> to your administrator profile. Please enter your email address below."}
        </div>
    {/if}

    <div class="row">
        <div class="col-md-6">
            {render_form_field field='login' extra_classes='js-login'}
            {render_form_field field='firstname' extra_classes='js-firstname'}

        </div>
        <div class="col-md-6">
            {custom_render_form_field field='email'}
                <input type="email" {form_field_attributes field="email" extra_classes='js-email'}{if $show_email_change_notice} autofocus{/if}>
            {/custom_render_form_field}
            {render_form_field field='lastname' extra_classes='js-lastname'}
        </div>
    </div>

    {form_field field='password_confirm'}
        {$passwordError = $error}
    {/form_field}

    <div class="row">
        <div class="col-md-6">
            {custom_render_form_field field='password'}
                <input type="password" {form_field_attributes field="password"} autocomplete="off" placeholder="{intl l='Leave empty to keep current password'}">
            {/custom_render_form_field}
        </div>
        <div class="col-md-6">
            {custom_render_form_field field='password_confirm'}
                <input type="password" {form_field_attributes field="password_confirm"} autocomplete="off" placeholder="{intl l='Leave empty to keep current password'}">
            {/custom_render_form_field}
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            {custom_render_form_field field="locale"}
                <select {form_field_attributes field="locale" no_standard_classes=1 extra_classes='js-locale'} data-toggle="selectpicker">
                    {loop name='lang-admin' type="lang"}
                        <option value="{$LOCALE}" {if $LOCALE == $value}selected="selected"{/if}>{$LOCALE}: {$TITLE}</option>
                    {/loop}
                </select>
            {/custom_render_form_field}
        </div>
        <div class="col-md-6">
            {custom_render_form_field field="profile"}
                <select {form_field_attributes field="profile" no_standard_classes=1 extra_classes='js-profile'} data-toggle="selectpicker">
                    {foreach $choices as $choice}
                        {if $choice->value == 0}
                            <option value="0">{intl l='Superadministrator'}</option>
                        {else}
                            {loop name='profile' type="profile" id=$choice->value}
                                <option value="{$ID}">{$TITLE}</option>
                            {/loop}
                        {/if}
                    {/foreach}
                </select>
            {/custom_render_form_field}
        </div>
    </div>

    {hook name="administrator.update-form" location="administrator_update_form" }
{/capture}

    {include
        file = "includes/generic-create-dialog.html"

        dialog_id    = "administrator_update_dialog"
        dialog_title = {intl l="Update an administrator"}
        dialog_body  = {$smarty.capture.administrator_update_dialog nofilter}

        dialog_ok_label     = {intl l="Update"}
        dialog_cancel_label = {intl l="Cancel"}

        form_action        = {url path="/admin/configuration/administrators/save"}
        form_enctype       = {form_enctype}
        form_error_message = $form_error_message
    }

{/form}

{* -- Delete administrator confirmation dialog ----------------------------------- *}

{capture "administrator_delete_dialog"}
    <input type="hidden" name="administrator_id" id="administrator_delete_id" value="" />

    {hook name="administrator.delete-form" location="administrator_delete_form" }

{/capture}

{include
    file = "includes/generic-confirm-dialog.html"

    dialog_id       = "administrator_delete_dialog"
    dialog_title    = {intl l="Delete administrator"}
    dialog_message  = {intl l="Do you really want to delete this administrator ?"}

    form_action         = {token_url path='/admin/configuration/administrators/delete'}
    form_content        = {$smarty.capture.administrator_delete_dialog nofilter}
}

{include
    file = "includes/generic-warning-dialog.html"

    dialog_id       = "administrator_cannot_delete_dialog"
    dialog_title    = {intl l="You can't delete this administrator"}
    dialog_body     = {intl l="They are some administrator which are linked to this administrator. Please edit/remove them before deleting this administrator."}
}

{/block}

{block name="javascript-initialization"}

{javascripts file='assets/js/bootstrap-select/bootstrap-select.js'}
    <script src='{$asset_url}'></script>
{/javascripts}

<script type="text/javascript">
jQuery(function($) {

{if $administratorCreateError == true}
    $('#administrator_create_dialog').modal();
{/if}

{if $administratorUpdateError == true}
    $('#administrator_update_dialog').modal();
{/if}

    $(".js-delete-administrator").click(function(e){
        $('#administrator_delete_id').val($(this).data('id'))
    });

    $(".js-update-administrator").click(function(e){
        $('#administrator_update_id').val($(this).data('id'));

        $('.js-admin-' + $(this).data('id')).find('td').each(function(k, v) {
            $v = $(v);

            if(!$v.data('field-class')) {
                return true;
            }
            if($v.data('value')) {
                var fieldValue = $v.data('value');
            } else {
                var fieldValue = $v.text();
            }

            $('.' + $v.data('field-class')).val($.trim(fieldValue));

            if($('.' + $v.data('field-class')).is('select')) {
                $('.' + $v.data('field-class')).selectpicker('refresh');
            }
        });
    });
})
</script>

{/block}

{block name="javascript-last-call"}
    {hook name="administrators.js" location="administrators-js" }

    {if $show_update_dialog}
        <script>
            jQuery(function($) {
                {if $show_email_change_notice}
                    {* We have to change email, highlight the field and set focus *}
                    $('#administrator_update_dialog').on('shown.bs.modal', function (e) {
                        var $emailInput = $('.js-email', $(this));

                        $emailInput.closest('.form-group').addClass('has-error');
                        $emailInput.focus();
                    });
                {/if}

                $('.js-update-administrator[data-id={admin attr='id'}]').first().click();
            });
        </script>
    {/if}
{/block}
