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

{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>

        {module_include 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"}
                                {loop type="auth" name="can_create" role="ADMIN" resource="admin.profile" access="CREATE"}
                                    <a class="btn btn-default btn-primary pull-right" title="{intl l='Create a new profile'}" href="#profile_create_dialog" data-toggle="modal">
                                        <span class="glyphicon glyphicon-plus"></span>
                                    </a>
                                {/loop}
                            </caption>
                            <thead>
                                <tr>
                                    <th class="col-md-3">{intl l="Name"}</th>
                                    <th class="col-md-5">{intl l="Description"}</th>
                                    <th class="col-md-1">{intl l="Actions"}</th>
                                </tr>
                            </thead>
                            <tbody>

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

                                <tr>
                                    <td>{$TITLE}</td>
                                    <td>{$DESCRIPTION}</td>
                                    <td>
                                        <div class="btn-group">
                                            {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.profile" access="UPDATE"}
                                                <a class="btn btn-default btn-xs" title="{intl l='Change this profile'}" href="{url path="/admin/configuration/profiles/update/$ID"}"><span class="glyphicon glyphicon-edit"></span></a>
                                            {/loop}

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

                                            {loop type="auth" name="can_delete" role="ADMIN" resource="admin.configuration.profile" access="DELETE"}
                                                <a class="btn btn-default btn-xs js-delete-profile" title="{intl l='Delete this profile'}" href="#{if $linkedAdminCount > 0}profile_cannot_delete_dialog{else}profile_delete_dialog{/if}" data-id="{$ID}" data-toggle="modal"><span class="glyphicon glyphicon-trash"></span></a>
                                            {/loop}
                                        </div>
                                    </td>
                                </tr>

                                {/loop}

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


            </div>
        </div>

        {module_include 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 form=$form}

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

    {form_field form=$form field='code'}
        <div class="form-group {if $error}has-error{/if}">
            <label for="{$label_attr.for}" class="control-label">{intl l=$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 form=$form field='title'}
        <div class="form-group {if $error}has-error{/if}">
            <label for="{$label_attr.for}" class="control-label">{intl l=$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 form=$form field='chapo'}
        <div class="form-group {if $error}has-error{/if}">
            <label for="{$label_attr.for}" class="control-label">{intl l=$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 form=$form field='description'}
        <div class="form-group {if $error}has-error{/if}">
            <label for="{$label_attr.for}" class="control-label">
                {intl l=$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 form=$form field='postscriptum'}
        <div class="form-group {if $error}has-error{/if}">
            <label for="{$label_attr.for}" class="control-label">{intl l=$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}

{/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=$form}
        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="" />

    {module_include 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         = {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}

{javascripts file='assets/js/main.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}