{* Set the default translation domain, that will be used by intl when the 'd' parameter is not set *}
{default_translation_domain domain='bo.default'}

<div class="form-group">
    {ifloop rel="free_attributes"}
    <form method="POST" action="{url path='/admin/configuration/templates/attributes/add'}">

        <input type="hidden" name="template_id" value="{$template_id}" />

	    <div class="input-group">
	        <select required="required" name="attribute_id" id="attribute_id" class="form-control">
		        <option value="">{intl l='Select an attribute...'}</option>
		        {loop name="free_attributes" type="attribute" exclude_template="$template_id" backend_context="1" lang="$edit_language_id"}
		            <option value="{$ID}">{$TITLE}</option>
		        {/loop}
		    </select>
		    <span class="input-group-btn">
		       <button class="btn btn-primary action-btn" type="submit"><span class="glyphicon glyphicon-plus-sign"></span></button>
		    </span>
	    </div>

	    <span class="help-block">{intl l='Select an attribute and click (+) to add it to this template'}</span>
    </form>

    {/ifloop}
    {elseloop rel="free_attributes"}
    <div class="alert alert-info">{intl l="There is currently no available attributes."}</div>
    {/elseloop}
</div>

<div class="table-responsive">
    <table class="table table-striped table-condensed table-left-aligned">
    <thead>
        <tr>
            <th>{intl l='ID'}</th>

            <th>{intl l='Attribute title'}</th>

            <th class="text-center">{intl l='Position'}</th>

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

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

    <tbody>
        {loop name="assigned_attributes" type="attribute" template="$template_id" backend_context="1" lang="$edit_language_id"}
        <tr>
            <td>{$ID}</td>

            <td><a href="{url path="admin/configuration/attributes/update" attribute_id=$ID}">{$TITLE}</a></td>

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

            {hook name="template.attributes-table-row" location="template_attributes_table_row" }

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

         {elseloop rel="assigned_attributes"}
             <tr>
                 <td colspan="4">
                     <div class="alert alert-info">
                         {intl l="This template contains no attributes"}
                     </div>
                 </td>
             </tr>
         {/elseloop}
        </tbody>
    </table>
</div>
{* Delete value confirmation dialog *}

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

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

    dialog_id       = "delete_attribute_dialog"
    dialog_title    = {intl l="Remove attribute"}
    dialog_message  = {intl l="Do you really want to remove this attribute from the template ? The attributes combinations that uses this attributes <strong>will be deleted</strong> for the products that uses this template."}

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

<script>

    $(function() {

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

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

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

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

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

    });
</script>
