{config_load file='variables.conf'}

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

{loop name="product_edit" type="product" visible="*" id=$product_id backend_context="1" lang=$edit_language_id}
<div class="form-container">

    <div class="row">
        <div class="col-md-12">
            <p></p> {* <---- FIXME Lame ! *}
		    <form method="POST" class="clearfix" action="{url path="/admin/product/%id/set-product-template" id=$ID}" id="product_template_form">

                <input type="hidden" name="product_id" value="{$product_id}" />
                <input type="hidden" name="current_tab" value="attributes" />

		        <div class="row">
		            <div class="col-md-12">
		                <div class="well well-sm">
		                    <p>{intl
		                          l="To use features or attributes on this product, please select a product template. You can define product templates in the <a href=\"%tpl_mgmt_url\" target=\"tpl_window\">configuration section</a> of the administration."
		                          tpl_mgmt_url={url path='/admin/configuration/templates'}
		                       }
		                    </p>

			                <label for="template_id" class="control-label">
			                    {intl l='Current product template'} :
			                </label>

			                <div class="input-group">
			                    <select required="required" name="template_id" id="template_id" class="form-control">
			                        <option value="0">{intl l='Do not use a product template'}</option>
			                        {loop name="product_template_select" type="product-template"}
			                            <option value="{$ID}" {if $TEMPLATE == $ID}selected="selected"{/if}>{$NAME}</option>
			                        {/loop}
			                    </select>

			                    <span class="input-group-btn" id="apply_template_button">
			                       <button class="btn btn-primary action-btn" type="submit">{intl l="Apply"}</button>
			                    </span>
			                </div>
		                </div>
		            </div>
		        </div>
		    </form>
        </div>
    </div>

    {* Check if a product template is defined *}

    <div class="row">
        <div class="col-md-12">

			<form method="POST" action="{url path="/admin/product/$ID/update-attributes-and-features"}" id="attribute_form">

			    <input type="hidden" name="product_id" value="{$product_id}" />
			    <input type="hidden" name="current_tab" value="attributes" />

			    {include
			        file = "includes/inner-form-toolbar.html"
			        hide_submit_buttons = false

			        page_url  = "{url path='/admin/products/update' product_id=$ID}"
			        close_url = "{url path='/admin/categories' category_id=$DEFAULT_CATEGORY}"
			    }

			    {* -- Begin attributes management ------------------------------- *}

                <div class="row">
			        <div class="col-md-12">
				        <div class="well well-sm">
					        <div class="form-group">
					            <p class="title title-without-tabs">{intl l='Product Attributes'}</p>

	                            <p>
	                            {if $TEMPLATE}
	                                {intl
                                        l="You can change template attributes and their positions in <a href=\"%tpl_mgmt_url\" target=\"tpl_window\">the template configuration page</a>."
                                        tpl_mgmt_url={url path='/admin/configuration/templates/update' template_id=$TEMPLATE}
	                                }
	                            {else}
	                                {intl
	                                    l="You can change attributes and their positions in <a href=\"%tpl_mgmt_url\" target=\"tpl_window\">the attributes configuration page</a>."
	                                    tpl_mgmt_url={url path='/admin/configuration/attributes'}
	                                }
	                            {/if}
	                            </p>

	                            <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 Name'}</th>

	                                            {hook name="product.attributes-table-header" location="product_attributes_table_header" }
	                                        </tr>
	                                    </thead>

	                                    <tbody>
	                                        {loop name="product-attributes" type="attribute" order="manual" product=$product_id backend_context="1" lang="$edit_language_id"}
	                                        <tr>
	                                            <td>{$ID}</td>

	                                            <td>{$TITLE}</td>

	                                            {hook name="product.attributes-table-row" location="product_attributes_table_row" }
	                                        </tr>
	                                        {/loop}

	                                        {elseloop rel="product-attributes"}
	                                            <tr>
	                                                <td colspan="2">
	                                                    <div class="alert alert-info">
	                                                        {intl l="This product template does not contains any features"}
	                                                    </div>
	                                                </td>
	                                            </tr>
	                                        {/elseloop}
	                                    </tbody>
	                                </table>
	                            </div>
					        </div>
					    </div>
					</div>
			    </div>

			    {* -- Begin features management ---------------------------------- *}

                <div class="row">
			        <div class="col-md-12">
			            <div class="well well-sm">
				            <div class="form-group">
				                <p class="title title-without-tabs">{intl l='Product Features'}</p>

	                            <p>
	                            {if $TEMPLATE}
	                                {intl
	                                    l="You can change templates features and their positions in <a href=\"%tpl_mgmt_url\" target=\"tpl_window\">the template configuration page</a>."
	                                    tpl_mgmt_url={url path='/admin/configuration/templates/update' template_id=$TEMPLATE}
	                                }
	                            {else}
	                                {intl
	                                    l="You can change feature and their positions in <a href=\"%tpl_mgmt_url\" target=\"tpl_window\">the features configuration page</a>."
	                                    tpl_mgmt_url={url path='/admin/configuration/features'}
	                                }
	                            {/if}
	                            </p>

				                <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 Name'}</th>
						                        <th>{intl l='Feature value for this product'}</th>

						                        {hook name="product.features-table-header" location="product_features_table_header" }

						                    </tr>
						                </thead>

										<tbody>
										    {loop name="product-features" type="feature" order="manual" product=$product_id backend_context="1" lang="$edit_language_id"}
										    <tr>
	                                            <td>{$ID}</td>

										        <td>{$TITLE}</td>

							                    <td>
							                        {* Multiple values *}

							                        {ifloop rel="product-features-av"}

		                                                {* load all selected values in an array to speed up things a little *}

		                                                {$selected = array()}

							                            {loop name="free-text-value" exclude_free_text="true" type="feature_value" product=$product_id feature=$ID  backend_context="1" lang="$edit_language_id"}
							                               {$selected[] = $FEATURE_AV_ID}
							                            {/loop}

								                        {capture name="select_options"}
								                        {loop name="product-features-av" type="feature-availability" feature=$ID order="manual" backend_context="1" lang="$edit_language_id"}
								                            <option value="{$ID}" {if in_array($ID, $selected)}selected="selected"{/if}>{$TITLE}</option>

								                            {$options_count = $LOOP_COUNT} {* LOOP_COUNT is only available inside the loop ! *}
								                        {/loop}

                                                        {if $options_count > #maximum_product_attribute_select_size#}
                                                            {$select_size = #maximum_product_attribute_select_size#}
                                                        {else}
                                                            {$select_size = $options_count}
                                                        {/if}
								                        {/capture}

								                        <div class="input-form">
								                            <select multiple="multiple" name="feature_value[{$ID}][]" id="feature_value_{$ID}" size="{$select_size}" class="form-control">
								                            {$smarty.capture.select_options nofilter}
								                            </select>
								                        </div>

													    <span class="help-block text-right">
													        {intl l='Use Ctrl+click to select more than one value. You can also <a href="#" class="clear_feature_value" data-id="%id">clear selected values</a>.' id={$ID}}
													    </span>
							                        {/ifloop}

                                      {* Free text *}

                                      {elseloop rel="product-features-av"}
                                          {$feature_value=''}
                                          {loop name="free-text-value" type="feature_value" product=$product_id feature=$ID backend_context="1" lang="$edit_language_id" limit="1"}
                                              {$feature_value=$TITLE}
                                          {/loop}
                                      <input type="text" id="feature_text_value_{$ID}" name="feature_text_value[{$ID}]" title="{intl l='Enter here the feature value as free text'}" placeholder="{intl l='Feature value'}" class="form-control" value="{$feature_value}">
                                      {/elseloop}
							                    </td>

										        {hook name="product.features-table-row" location="product_features_table_row" }

										    </tr>
										    {/loop}

										    {elseloop rel="product-features"}
										        <tr>
										            <td colspan="3">
										                <div class="alert alert-info">
										                    {intl l="This product template does not contains any features"}
										                </div>
										            </td>
										        </tr>
										    {/elseloop}
										</tbody>
				                    </table>
				                </div>
				            </div>
				        </div>
                    </div>
			    </div>

                {include
                file = "includes/inner-form-toolbar.html"
                hide_submit_buttons = false
                hide_flags = true

                page_url  = "{url path='/admin/products/update' product_id=$ID}"
                close_url = "{url path='/admin/categories' category_id=$DEFAULT_CATEGORY}"
                }

			</form>
		</div>
	</div>

</div>
{/loop}

<script>
$(function() {

    // Unselect all options in attribute + feature tab
    $('.clear_feature_value').click(function(event){
        $('#feature_value_' + $(this).data('id') + ' option').prop('selected', false);

        event.preventDefault();
    });
});
</script>
