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

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

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

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

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

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

        {hook name="attributes.top" location="attributes_top" }

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

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

                                        <th class="text-center">
                                            {admin_sortable_header
    	                                        current_order=$order
    	                                        order='manual'
    	                                        reverse_order='manual_reverse'
    	                                        path='/admin/configuration/attributes'
    	                                        label="{intl l="Position"}"
                                            }
                                        </th>

                                        {hook name="attributes.table-header" location="attributes_table_header" }

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

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

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

                                        <td class="text-center">
                                            {admin_position_block
                                            resource="admin.configuration.attribute"
                                            access="UPDATE"
                                            path="/admin/configuration/attributes/update-position"
                                            url_parameter="attribute_id"
                                            in_place_edit_class="positionChange"
                                            position="$POSITION"
                                            id="$ID"
                                            }
                                        </td>

                                        {hook name="attributes.table-row" location="attributes_table_row" attribute_id={$ID} }

                                        <td class="actions">
                                            {include file='renderer/buttons.html' buttons=[
                                                [
                                                    'type' => 'remove',
                                                    'class' => 'attribute-remove-from-all',
                                                    'title' => {intl l='Remove this attribute from all product templates'},
                                                    'href' => "#remove_from_all_dialog",
                                                    'auth' => ['resource' => 'admin.configuration.attribute', 'access' => 'UPDATE'],
                                                    'data' => [
                                                        'id' => $ID,
                                                        'toggle' => 'modal'
                                                    ]
                                                ],
                                                [
                                                    'type' => 'add',
                                                    'class' => 'attribute-add-to-all',
                                                    'title' => {intl l='Add this attribute to all product templates'},
                                                    'href' => "#add_to_all_dialog",
                                                    'auth' => ['resource' => 'admin.configuration.attribute', 'access' => 'UPDATE'],
                                                    'data' => [
                                                        'id' => $ID,
                                                        'toggle' => 'modal'
                                                    ]
                                                ],
                                                [
                                                    'type' => 'edit',
                                                    'class' => 'attribute-change',
                                                    'title' => {intl l='Change this product attribute'},
                                                    'href' => {url path='/admin/configuration/attributes/update' attribute_id=$ID},
                                                    'auth' => ['resource' => 'admin.configuration.attribute']
                                                ],
                                                [
                                                    'type' => 'delete',
                                                    'class' => 'attribute-delete',
                                                    'title' => {intl l='Delete this product attribute'},
                                                    'href' => '#delete_dialog',
                                                    'data' => [
                                                        'id' => $ID,
                                                        'toggle' => 'modal'
                                                    ],
                                                    'auth' => ['resource' => 'admin.brand']
                                                ]
                                            ]}
                                        </td>
                                    </tr>
                                     {/loop}

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

        {hook name="attributes.bottom" location="attributes_bottom" }

    </div>
</div>

{* Adding a new attribute *}

{form name="thelia.admin.attribute.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 attribute ID, see controller  *}
              <input type="hidden" name="{$name}" value="{url path='/admin/configuration/attributes/update' attribute_id='_ID_'}" />
        {/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>

                {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='Attribute 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 attribute name in the default language (%language_name)" language_name={$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}

        {form_field field='add_to_all'}
            <div class="form-group {if $error}has-error{/if}">
                <div class="checkbox {if $error}has-error{/if}">
                    <label for="{$label_attr.for}" class="control-label">
                        <input type="checkbox" name="{$name}" value="1" {if $value != 0}checked="checked"{/if}>
                        {$label}
                    </label>
                    <span class="help-block">{intl l='Check this box if you want to add this attributes to all product templates'}</span>
                </div>
            </div>
        {/form_field}

        {hook name="attribute.create-form" location="attribute_create_form" }

    {/capture}

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

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

        dialog_ok_label     = {intl l="Create this attribute"}

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

{* Delete confirmation dialog *}

{capture "delete_dialog"}
    <input type="hidden" name="attribute_id" id="attribute_delete_id" value="" />

    {hook name="attribute.delete-form" location="attribute_delete_form" }

{/capture}

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

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

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


{* Add to all dialog *}

{capture "add_to_all_dialog"}
    <input type="hidden" name="attribute_id" id="attribute_add_to_all_id" value="" />

    {hook name="attribute.add-to-all-form" location="attribute_add_to_all_form" }

{/capture}

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

    dialog_id       = "add_to_all_dialog"
    dialog_title    = {intl l="Add to all product templates"}
    dialog_message  = {intl l="Do you really want to add this attribute to all product templates ?"}

    form_action         = {url path='/admin/configuration/attributes/add-to-all-templates'}
    form_content        = {$smarty.capture.add_to_all_dialog nofilter}
}

{* Remove from all dialog *}

{capture "remove_from_all_dialog"}
    <input type="hidden" name="attribute_id" id="attribute_remove_from_all_id" value="" />

    {hook name="attribute.remove-to-all-form" location="attribute_remove_to_all_form" }

{/capture}

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

    dialog_id       = "remove_from_all_dialog"
    dialog_title    = {intl l="Remove from all product templates"}
    dialog_message  = {intl l="Do you really want to remove this attribute from all product templates ? You'll loose all product related data for this attribute."}

    form_action         = {url path='/admin/configuration/attributes/remove-from-all-templates'}
    form_content        = {$smarty.capture.remove_from_all_dialog nofilter}
}

{/block}

{block name="javascript-initialization"}

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

    <script>
        $(function() {

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

            $('a.attribute-add-to-all').click(function(ev) {
                $('#attribute_add_to_all_id').val($(this).data('id'));
            });

            $('a.attribute-remove-from-all').click(function(ev) {
                $('#attribute_remove_from_all_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.attribute.creation"
            }

            {* Inline editing of object position using bootstrap-editable *}

            $('.positionChange').editable({
                type        : 'text',
                title       : '{intl l="Enter new attribute position"}',
                mode        : 'popup',
                inputclass  : 'input-mini',
                placement   : 'left',
                success     : function(response, newValue) {
                    // The URL template
                    var url = "{url noamp='1' path='/admin/configuration/attributes/update-position' attribute_id='__ID__' position='__POS__'}";

                    // Perform subtitutions
                    url = url.replace('__ID__', $(this).data('id'))
                    .replace('__POS__', newValue);

                    // Reload the page
                    location.href = url;
                }
            });

        });
    </script>
{/block}

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