{* 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_features"}
    <form method="POST" action="{url path='/admin/configuration/templates/features/add'}">

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

	    <div class="input-group">
	        <select required="required" name="feature_id" id="feature_id" class="form-control">
		        <option value="">{intl l='Select a feature...'}</option>
		        {loop name="free_features" type="feature" 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-default btn-primary action-btn" type="submit"><span class="glyphicon glyphicon-plus-sign"></span></button>
		    </span>
	    </div>

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

    {/ifloop}
    {elseloop rel="free_features"}
    <div class="alert alert-info">{intl l="There is currently no available features."}</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='Feature title'}</th>

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

            {module_include location='template_features_table_header'}

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

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

            <td>
                {$TITLE}
            </td>

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

            {module_include location='template_features_table_row'}

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

         {elseloop rel="assigned_features"}
             <tr>
                 <td colspan="4">
                     <div class="alert alert-info">
                         {intl l="This template contains no features"}
                     </div>
                 </td>
             </tr>
         {/elseloop}
        </tbody>
    </table>
</div>

{* Delete value confirmation dialog *}

{capture "delete_feature_dialog"}
    <input type="hidden" name="template_id" value="{$template_id}" />
    <input type="hidden" name="feature_id" id="feature_delete_id" value="" />
{/capture}

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

    dialog_id       = "delete_feature_dialog"
    dialog_title    = {intl l="Remove feature"}
    dialog_message  = {intl l="Do you really want to remove this feature from the template ?"}

    form_action     = {url path='/admin/configuration/templates/features/delete'}
    form_content    = {$smarty.capture.delete_feature_dialog nofilter}
}

<script>
    $(function() {

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

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

        $('.featurePositionChange').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-feature-position' template_id=$template_id feature_id='__ID__' position='__POS__'}";

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

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

    });
</script>
