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

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

{block name="page-title"}{intl l='Administration profiles'}{/block}

{block name="check-resource"}admin.configuration.profile{/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/profiles'}">{intl l="Profiles"}</a></li>
            </ul>
        </div>

        {hook name="profiles.top" location="profiles_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="Administration profiles"}
                                {include file='renderer/buttons.html' btn_group=false buttons=[
                                    [
                                        'type' => 'create',
                                        'class' => 'action-btn',
                                        'title' => {intl l='Create a new profile'},
                                        'href' => '#profile_create_dialog',
                                        'auth' => ['resource' => 'admin.profile'],
                                        'data' => [
                                            'toggle' => 'modal'
                                        ]
                                    ]
                                ]}
                            </caption>
                            <thead>
                                <tr>
                                    <th>{intl l="Code"}</th>
                                    <th>{intl l="Name"}</th>
                                    <th>{intl l="Description"}</th>
                                    {hook name="profile.table-header"}
                                    <th class="actions">{intl l="Actions"}</th>
                                </tr>
                            </thead>
                            <tbody>

                                {loop type="profile" name="profiles" backend_context="1"}

                                {$can_change = false}
                                {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.profile" access="UPDATE"}
                                {$can_change = true}
                                {/loop}

                                <tr>
                                    <td>
                                        {if $can_change}
                                            <a title="{intl l='Change this profile'}" href="{url path="/admin/configuration/profiles/update/%id" id=$ID}">
                                                {$CODE}
                                            </a>
                                        {else}
                                            {$CODE}
                                        {/if}
                                    </td>
                                    <td>
                                        {if $can_change}
                                            <a title="{intl l='Change this profile'}" href="{url path="/admin/configuration/profiles/update/%id" id=$ID}">
                                                 {$TITLE}
                                            </a>
                                        {else}
                                            {$TITLE}
                                        {/if}
                                    </td>
                                    <td>{$DESCRIPTION}</td>
                                    {hook name="profile.table-row" profile_id=$ID}
                                    <td class="actions">
                                        {$buttons = []}

                                        {if $can_change}
                                        {$buttons[] = [
                                            'type' => 'edit',
                                            'title' => {intl l='Change this profile'},
                                            'href' => {url path="/admin/configuration/profiles/update/%id" id=$ID},
                                            'auth' => ['resource' => 'admin.configuration.profile']
                                        ]}
                                        {/if}

                                        {assign linkedAdminCount {count name="linked-admin" type="admin" profile=$ID}}

                                        {if $linkedAdminCount > 0}{$href = "#profile_cannot_delete_dialog"}{else}{$href="#profile_delete_dialog"}{/if}

                                        {$buttons[] = [
                                            'type' => 'delete',
                                            'class' => 'js-delete-profile',
                                            'title' => {intl l='Delete this profile'},
                                            'href' => $href,
                                            'data' => [
                                                'id' => $ID,
                                                'toggle' => 'modal'
                                            ],
                                            'auth' => ['resource' => 'admin.configuration.profile']
                                        ]}

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

                                {/loop}

                                {elseloop rel="profiles"}
                                    <tr>
                                        <td colspan="4">
                                            <div class="alert alert-info">
                                                {intl l="No administration profile defined yet. Click the + button to create one."}
                                            </div>
                                        </td>
                                    </tr>
                                {/elseloop}

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


            </div>
        </div>

        {hook name="profiles.bottom" location="profiles_bottom" }

    </div>
</div>

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

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

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

    {form_hidden_fields}

    {loop type="lang" name="current-edit-lang" default_only="1"}
        <div class="text-center well well-sm">
            <img src="{image file="assets/img/flags/$CODE.png"}" alt="{$TITLE}" />
            {intl l="Enter information in the default language (%title)" title={$TITLE}}
        </div>

        {form_field field='locale'}
            <input type="hidden" name="{$name}" value="{$LOCALE}" />
        {/form_field}
    {/loop}

    {form_field field='code'}
        <div class="form-group {if $error}has-error{/if}">
            <label for="{$label_attr.for}" class="control-label">{$label} : </label>
            <input type="text" id="{$label_attr.for}" name="{$name}" required="required" title="{intl l='Profile code'}" placeholder="{intl l='Profile code'}" class="form-control" value="{if $form_error}{$value}{/if}">
        </div>
    {/form_field}

    {form_field field='title'}
        <div class="form-group {if $error}has-error{/if}">
            <label for="{$label_attr.for}" class="control-label">{$label} : </label>
            <input type="text" id="{$label_attr.for}" name="{$name}" required="required" title="{intl l='Title'}" placeholder="{intl l='Title'}" class="form-control" value="{if $form_error}{$value}{/if}">
        </div>
    {/form_field}

    {form_field field='chapo'}
        <div class="form-group {if $error}has-error{/if}">
            <label for="{$label_attr.for}" class="control-label">{$label} : </label>
            <input type="text" id="{$label_attr.for}" name="{$name}" title="{intl l='Short description'}" placeholder="{intl l='Short description'}" class="form-control" value="{if $form_error}{$value}{/if}">
        </div>
    {/form_field}

    {form_field field='description'}
        <div class="form-group {if $error}has-error{/if}">
            <label for="{$label_attr.for}" class="control-label">
                {$label} :
                <span class="label-help-block">{intl l="The detailed description."}</span>
            </label>

            <textarea name="{$name}" id="{$label_attr.for}" rows="10" class="form-control wysiwyg">{if $form_error}{$value}{/if}</textarea>
        </div>
    {/form_field}

    {form_field field='postscriptum'}
        <div class="form-group {if $error}has-error{/if}">
            <label for="{$label_attr.for}" class="control-label">{$label} : </label>
            <input type="text" id="{$label_attr.for}" name="{$name}" title="{intl l='Postscriptum'}" placeholder="{intl l='Postscriptum'}" class="form-control" value="{if $form_error}{$value}{/if}">
        </div>
    {/form_field}
    {hook name="profile.create-form" location="profile_create_form" }
{/capture}

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

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

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

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

{/form}

{* -- Delete profile confirmation dialog ----------------------------------- *}

{capture "profile_delete_dialog"}
    <input type="hidden" name="profile_id" id="profile_delete_id" value="" />

    {hook name="profile.delete-form" location="profile_delete_form" }

{/capture}

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

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

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

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

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

{/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 $profileCreateError == true}
    $('#profile_create_dialog').modal();
{/if}

    $(".js-delete-profile").click(function(e){
        $('#profile_delete_id').val($(this).data('id'))
    });
})
</script>

{/block}

{block name="javascript-last-call"}
    {hook name="profiles.js" location="profiles-js" }
    {hook name="wysiwyg.js" location="wysiwyg-profiles-js" }
{/block}