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

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

{block name="page-title"}{intl l='Edit an attribute'}{/block}

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

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

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

        {loop name="attribute_edit" type="attribute" id=$attribute_id backend_context="1" lang=$edit_language_id}

            <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="Attributes"}</a></li>
                <li>{intl l='Editing attribute "%name"' name={$TITLE}}</li>
            </ul>

		{hook name="attribute-edit.top" attribute_id=$attribute_id}

    		<div class="row">
    			<div class="col-md-12 general-block-decorator">
    				<div class="row">

    					<div class="col-md-12 title title-without-tabs">
    					   {intl l='Edit attribute "%name"' name={$TITLE}}
    					</div>

                        <div class="col-md-12">
	                        <div class="form-container">
	                            {form name="thelia.admin.attribute.modification"}
		                        <form method="POST" action="{url path='/admin/configuration/attributes/save'}" {form_enctype} class="clearfix">

		                             {include file="includes/inner-form-toolbar.html" close_url="{url path='/admin/configuration/attributes'}"}

		                             <div class="col-md-6">

		                                <p class="title title-without-tabs">{intl l='Attribute information'}</p>

                                        {form_field field='id'}
                                            <input type="hidden" name="{$name}" value="{$attribute_id}" />
                                        {/form_field}

     	                                {* Be sure to get the attribute ID, even if the form could not be validated *}
     	                                <input type="hidden" name="attribute_id" value="{$attribute_id}" />

     						        	{form_hidden_fields}

     						        	{form_field field='success_url'}
     						        		<input type="hidden" name="{$name}" value="{url path='/admin/configuration/attributes'}" />
     						        	{/form_field}

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

     					            	{if $form_error}<div class="alert alert-danger">{$form_error_message}</div>{/if}

                                        {include file="includes/standard-description-form-fields.html"}
		    						</div>

		     						<div class="col-md-6">

		     						    <p class="title title-without-tabs clearfix">

		     						         {intl l='Attribute values'}

		                                     <span class="pull-right">
                                                {include file='renderer/buttons.html' btn_group=false buttons=[
                                                    [
                                                        'type' => 'create',
                                                        'class' => 'pull-right',
                                                        'title' => {intl l='Add a new attribute value'},
                                                        'href' => '#creation_dialog',
                                                        'auth' => ['resource' => 'admin.configuration.attribute'],
                                                        'data' => [
                                                            'toggle' => 'modal'
                                                        ]
                                                    ]
                                                ]}
		                                     </span>
		     						    </p>

		     						    <div class="alert alert-info">
		     						     {intl l="Enter here all possible attribute values."}
		     						    </div>

                                        <div class="table-responsive">
    				                        <table class="table table-striped table-condensed table-left-aligned">
    				                            <thead>
    				                                <tr>
    				                                    <th>
    				                                        {admin_sortable_header
    				                                            current_order=$attributeav_order
    				                                            order='id'
    				                                            reverse_order='id_reverse'
    				                                            request_parameter_name='attributeav_order'
    				                                            path={url path='/admin/configuration/attributes/update' attribute_id=$attribute_id}
    				                                            label="{intl l='ID'}"
    				                                        }
    				                                    </th>

    				                                    <th>
    				                                        {admin_sortable_header
    				                                            current_order=$attributeav_order
    				                                            order='alpha'
    				                                            reverse_order='alpha_reverse'
    				                                            request_parameter_name='attributeav_order'
    				                                            path={url path='/admin/configuration/attributes/update' attribute_id=$attribute_id}
    				                                            label="{intl l='Value'}"
    				                                        }
    				                                    </th>

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

    				                                    {hook name="attributes-value.table-header" location="attributes_value_table_header"  attribute_id=$attribute_id}

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

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

    				                                    <td>
    				                                        {* FIXME : integrate this in the encolsing form to provide standard form processing *}
    				                                        <input class="js-edit form-control" type="text" name="attribute_values[{$ID}]" value="{$TITLE}" />
    				                                    </td>

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

    				                                    {hook name="attributes-value.table-row" location="attributes_value_table_row" attribute_id=$attribute_id}

    				                                    <td class="actions">
                                                            {loop type="auth" name="can_create" role="ADMIN" resource="admin.configuration.attribute" access="UPDATE"}
                                                               <a class="btn btn-default btn-xs value-delete" title="{intl l='Delete this value'}" href="#delete_dialog" data-id="{$ID}" data-toggle="modal">
                                                                   <span class="glyphicon glyphicon-trash"></span>
                                                               </a>
                                                            {/loop}
    				                                    </td>
    				                                </tr>
    				                                 {/loop}

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

    		</div>

		{hook name="attribute-edit.bottom" attribute_id=$attribute_id}

		{/loop}

        {elseloop rel="attribute_edit"}
            <div class="row">
                <div class="col-md-12">
                    <div class="alert alert-error">
                        {intl l="Sorry, attribute ID=%id was not found." id={$attribute_id}}
                    </div>
                </div>
            </div>
        {/elseloop}

    </div>
</div>

{* Adding a new attribute *}

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

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

    {capture "creation_dialog"}
        {form_hidden_fields}

        {* Be sure to get the attribute ID, even if the form could not be validated *}
        <input type="hidden" name="attribute_id" value="{$attribute_id}" />

        {form_field field='success_url'}
            {* on success, redirect to this page  *}
            <input type="hidden" name="{$name}" value="{url path='/admin/configuration/attributes/update' attribute_id=$attribute_id}" />
        {/form_field}

        {form_field field='attribute_id'}
        <input type="hidden" name="{$name}" value="{$attribute_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="current-edit-lang" id="$edit_language_id"}
                    <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="{intl l={$TITLE}}" /></span>
                    </div>

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

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

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

    {/capture}

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

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

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

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

{* Delete value confirmation dialog *}

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

    {hook name="attribute.id-delete-form" location="attribute_id_delete_form" attribute_id=$attribute_id}
{/capture}

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

    dialog_id       = "delete_dialog"
    dialog_title    = {intl l="Delete attribute value"}
    dialog_message  = {intl l="Do you really want to delete this attribute value ?"}

    form_action     = {token_url path='/admin/configuration/attributes-av/delete'}
    form_content    = {$smarty.capture.delete_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.value-delete').click(function(ev) {
                $('#value_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.attributeav.creation"
            }

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

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

                    // 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="attribute.edit-js" location="attribute-edit-js" attribute_id=$attribute_id}
    {hook name="wysiwyg.js" location="wysiwyg-attribute-edit-js" }
{/block}
