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

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

{block name="page-title"}{intl l='Thelia Product Templates'}{/block}

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

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

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

        <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/templates'}">{intl l="Product templates"}</a></li>
        </ul>

        {hook name="templates.top" location="templates_top" }

        <div class="row">
            <div class="col-md-12">
                <form action="#" method="post">
	                <div class="general-block-decorator">

	                    {if ! empty($general_error) }
	                        <div class="alert alert-danger">{$general_error}</div>
	                    {/if}

                        <div class="table-responsive">
    	                    <table class="table table-striped table-condensed table-left-aligned">
    	                        <caption>
    	                            {intl l='Thelia product templates'}
                                    {include file='renderer/buttons.html' btn_group=false buttons=[
                                        [
                                            'type' => 'create',
                                            'class' => 'action-btn',
                                            'title' => {intl l='Add a new product template'},
                                            'href' => '#creation_dialog',
                                            'auth' => ['resource' => 'admin.configuration.template'],
                                            'data' => [
                                                'toggle' => 'modal'
                                            ]
                                        ]
                                    ]}
    	                        </caption>
                                <thead>
                                    <tr>
                                        <th>
                                            {admin_sortable_header
    	                                        current_order=$order
    	                                        order='id'
    	                                        reverse_order='id_reverse'
    	                                        path='/admin/configuration/templates'
    	                                        label="{intl l='ID'}"
                                            }
                                        </th>

                                        <th>
                                            {admin_sortable_header
    	                                        current_order=$order
    	                                        order='alpha'
    	                                        reverse_order='alpha_reverse'
    	                                        path='/admin/configuration/templates'
    	                                        label="{intl l='Title'}"
                                            }
                                        </th>

                                        {hook name="templates.table-header" location="templates_table_header" }

                                        <th class="actions">{intl l="Actions"}</th>
                                    </tr>
                                </thead>

                                <tbody>
                                    {loop name="list" type="product-template" backend_context="1" lang=$lang_id order=$order}
                                    <tr>
                                        <td>{$ID}</td>

                                        <td>
                                            {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.template" access="UPDATE"}
                                                <a title="{intl l='Change this template'}" href="{url path='/admin/configuration/templates/update' template_id=$ID}">{$NAME}</a>
                                            {/loop}
                                            {elseloop rel="can_change"}
                                                {$NAME}
                                            {/elseloop}
                                        </td>

                                        {hook name="templates.table-row" location="templates_table_row" template_id=$ID}

                                        <td class="actions">
                                            {include file='renderer/buttons.html' buttons=[
                                                [
                                                    'type' => 'edit',
                                                    'class' => 'template-change',
                                                    'title' => {intl l='Change this product template'},
                                                    'href' => {url path='/admin/configuration/templates/update' template_id=$ID},
                                                    'auth' => ['resource' => 'admin.configuration.template']
                                                ],
                                                [
                                                    'type' => 'duplicate',
                                                    'class' => 'template-change',
                                                    'title' => {intl l='Duplicate this product template'},
                                                    'href' => {url path='/admin/configuration/templates/duplicate' template_id=$ID},
                                                    'auth' => ['resource' => 'admin.configuration.template']
                                                ],
                                                [
                                                    'type' => 'delete',
                                                    'class' => 'area-configuration-delete template-delete',
                                                    'title' => {intl l='Delete this product template'},
                                                    'href' => '#delete_dialog',
                                                    'data' => [
                                                        'id' => $ID,
                                                        'toggle' => 'modal'
                                                    ],
                                                    'auth' => ['resource' => 'admin.configuration.template']
                                                ]
                                            ]}
                                        </td>
                                    </tr>
                                     {/loop}

                                     {elseloop rel="list"}
                                         <tr>
                                             <td colspan="4">
                                                 <div class="alert alert-info">
                                                     {intl l="No product template has been created yet. Click the + button to create one."}
                                                 </div>
                                             </td>
                                         </tr>
                                     {/elseloop}
                                </tbody>
    	                    </table>
                        </div>
	                </div>
                </form>
            </div>
        </div>

        {hook name="templates.bottom" location="templates_bottom" }

    </div>
</div>

{* Adding a new template *}

{form name="thelia.admin.template.creation"}

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

        {form_field field='success_url'}
              {* on success, redirect to the edition page, _ID_ is replaced with the created template ID, see controller  *}
              <input type="hidden" name="{$name}" value="{url path='/admin/configuration/templates/update' template_id='_ID_'}" />
        {/form_field}

        {form_field field='name'}
            <div class="form-group {if $error}has-error{/if}">
                <label for="{$label_attr.for}" class="control-label">{$label} : </label>

                {loop type="lang" name="default-lang" default_only="1"}
                    <div class="input-group">
                        <input type="text" id="{$label_attr.for}" required="required" name="{$name}" class="form-control" value="{$value}" title="{intl l='Template title'}" placeholder="{intl l='Title'}">
                        <span class="input-group-addon"><img src="{image file="assets/img/flags/{$CODE}.png"}" alt="{$TITLE}" /></span>
                    </div>

                    <div class="help-block">{intl l="Enter here the template name in the default language (%title)" title={$TITLE}}</div>

                    {* Switch edition to the current locale *}
                    <input type="hidden" name="edit_language_id" value="{$ID}" />

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

        {hook name="template.create-form" location="template_create_form" }

    {/capture}

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

        dialog_id    = "creation_dialog"
        dialog_title = {intl l="Create a new product template"}
        dialog_body  = {$smarty.capture.creation_dialog nofilter}

        dialog_ok_label     = {intl l="Create this product template"}

        form_action        = {url path='/admin/configuration/templates/create'}
        form_enctype       = {form_enctype}
        form_error_message = $form_error_message
    }
{/form}

{* Delete confirmation dialog *}

{capture "delete_dialog"}
    <input type="hidden" name="template_id" id="template_delete_id" value="" />

    {hook name="template.delete-form" location="template_delete_form" }

{/capture}

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

    dialog_id       = "delete_dialog"
    dialog_title    = {intl l="Delete template"}
    dialog_message  = {intl l="Do you really want to delete this template ? It will be removed from all products."}

    form_action     = {token_url path='/admin/configuration/templates/delete'}
    form_content    = {$smarty.capture.delete_dialog nofilter}
}
{/block}

{block name="javascript-initialization"}

    <script>
        $(function() {

            // Set proper template ID in delete from
            $('a.template-delete').click(function(ev) {
                $('#template_delete_id').val($(this).data('id'));
            });

            // JS stuff for creation form
            {include
                file      = "includes/generic-js-dialog.html"
                dialog_id = "creation_dialog"
                form_name = "thelia.admin.template.creation"
            }
        });
    </script>
{/block}

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