{*
* 2007-2024 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <contact@prestashop.com>
*  @copyright 2007-2024 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*}
{extends file='module:agilemultisellerpro/views/templates/front/layouts/selleraccount.tpl'}

{block name='page_content'}
	{if isset($isSeller) AND $isSeller}
		{if $product_menu == 13 && $product && $product->available_for_order}
			<style type="text/css">
				.form-group-show_price {
					display: none;
				}
			</style>
		{/if}
		{include file="module:agilemultisellerpro/views/templates/front/products/product_top.tpl"}
		<div class="row" {if $hasOwnerShip}{else}style="display:none;" {/if}>
			{include file="module:agilemultisellerpro/views/templates/front/products/product_nav.tpl"}
			<form id="product_form" name="product"
				action="{$link->getModuleLink('agilemultisellerpro', 'sellerproductdetail', ['id_product'=>$id_product,'product_menu'=>$product_menu], true)|escape:'htmlall':'UTF-8'}"
				enctype="multipart/form-data" method="post" class="form-horizontal col-md-9">

				<div>
					{block "form_fields"}
						{foreach from=$formFields item="field"}
							{block "form_field"}
								{if $field.templateFile}
									{include file="{$field.templateFile|escape:'htmlall':'UTF-8'}"
										languages=$languages
										field=$field
									}
								{elseif $field.isAgileForm}
									{include file="module:agilemultisellerpro/views/templates/front/_partials/form-fields.tpl"
										languages=$languages
										field=$field
									}
								{elseif $field.type === "password"}
									<div class="field-password-policy">
										{form_field field=$field}
									</div>
								{else}
									{form_field field=$field}
								{/if}
							{/block}
						{/foreach}
					{/block}
				</div>

				{block name='customer_form_footer'}
					<footer class="form-footer clearfix mt-3">
						<input type="hidden" name="submitSellerinfo" value="1">
						{block "form_buttons"}
							<a class="btn btn-link"
								href="{$link->getModuleLink('agilemultisellerpro', 'sellerproducts', [], true)|escape:'htmlall':'UTF-8'}">
								<i class="icon-th-list"></i>{l s=' Back to product list' d='Modules.Agilemultisellerpro.Product'}
							</a>

							{if $product_menu != 2 && $product_menu != 7}
								<button class="btn btn-primary form-control-submit float-xs-right" name="submitTabProduct" value="{$product_menu|intval}"
									data-link-action="save-customer" type="submit">
									{l s='Save' d='Shop.Theme.Actions'}
								</button>
							{/if}
						{/block}
					</footer>
				{/block}

			</form>
		</div>
		<br />
	{/if}

	</div>

{/block}

{block name='javascript_bottom'}
	{$smarty.block.parent}

	{* common begin ---------------------------------------------------------------- *}
	<script type="text/javascript">
		var currentmenuid = {$product_menu|intval};

		var base_dir = "{$base_dir_ssl|escape:'htmlall':'UTF-8'}";
		var id_product = {$id_product|intval};
		var priceDisplayPrecision = {$smarty.const._PS_PRICE_DISPLAY_PRECISION_|intval};

		hideOtherLanguage({$id_language|intval});

		function changeMyLanguage(field, fieldsString, id_language_new, iso_code) {
			changeLanguage(field, fieldsString, id_language_new, iso_code);
			$("img[id^='language_current_']").attr("src", base_dir_ssl + "img/l/" + id_language_new + ".jpg");
		}

		$(document).ready(function() {
			tinySetup({
				selector: ".rte",
				toolbar1: "code,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,|,blockquote,colorpicker,pasteword,|,bullist,numlist,|,outdent,indent,|,link,unlink,|,cleanup"
			});

			listGroupCollaps();

			$('.list-group-collaps').on('click', function(){
				var listCollaps = getCookie('ag-list-group-collaps');

				if (!listCollaps) {
					listCollaps = 'visible';
				}

				if ('hide' == listCollaps) {
					setCookie('ag-list-group-collaps', 'visible');
				} else {
					setCookie('ag-list-group-collaps', 'hide');
				}

				listGroupCollaps();
			})
		})

		function listGroupCollaps() {
			var listCollaps = getCookie('ag-list-group-collaps');

			if ('hide' == listCollaps) {
				$('.productTabs >.list-group').hide();
				$('.productTabs').removeClass('col-md-3').addClass('col-md-1 list-group-hide');
				$('#product_form').removeClass('col-md-9').addClass('col-md-11');
				$('.list-group-collaps-hide').hide();
				$('.list-group-collaps-show').show();
			} else {
				$('.productTabs >.list-group').show();
				$('.productTabs').removeClass('col-md-1 list-group-hide').addClass('col-md-3');
				$('#product_form').removeClass('col-md-11').addClass('col-md-9');
				$('.list-group-collaps-hide').show();
				$('.list-group-collaps-show').hide();
			}
		}

		function setCookie(name,value,days) {
			var expires = "";
			// if (days) {
			// 	var date = new Date();
			// 	date.setTime(date.getTime() + (days*24*60*60*1000));
			// 	expires = "; expires=" + date.toUTCString();
			// }
			document.cookie = name + "=" + (value || "")  + expires + "; path=/";
		}

		function getCookie(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}
	</script>
	{* common end ---------------------------------------------------------------- *}


	{* Information begin ---------------------------------------------------------- *}
	{if $product_menu == 1}
		<script type="text/javascript">
			var msg_select_one = "{l s='Please select at least one product.' d='Modules.Agilemultisellerpro.Products' js=1}";
			var msg_set_quantity = "{l s='Please set a quantity to add a product.' d='Modules.Agilemultisellerpro.Products' js=1}";

			{if isset($ps_force_friendly_product) && $ps_force_friendly_product}
				var ps_force_friendly_product = 1;
			{else}
				var ps_force_friendly_product = 0;
			{/if}

			{if isset($PS_ALLOW_ACCENTED_CHARS_URL) && $PS_ALLOW_ACCENTED_CHARS_URL}
				var PS_ALLOW_ACCENTED_CHARS_URL = 1;
			{else}
				var PS_ALLOW_ACCENTED_CHARS_URL = 0;
			{/if}

			var pathCSS = prestashop.urls.css_url;

			$(document).ready(function() {
				$(".checker").removeClass("checker");

				$("[id^='cancellink_']").click(function() {
					return confirm("{l s='Are you sure want to cancel selected options?' d='Modules.Agilemultisellerpro.Products'}");
				});

			});
		</script>

	{/if}
		{* Informaiton end --------------------------------------------------- *}


		{* Images begin --------------------------------------------------- *}
		{if $product_menu == 2}
			<script type="text/javascript">
				var upbutton = "{l s='Upload an image' d='Modules.Agilemultisellerpro.Products'}";
				var token = '{$token|escape:'htmlall':'UTF-8'}';
				var success_add =  "{l s='image has been successfully added' d='Modules.Agilemultisellerpro.Products'}";
				var id_tmp = 0;
				// var ajax_products_url = "{$base_dir_ssl|escape:'htmlall':'UTF-8'}modules/agilemultiseller/ajax_products.php";
				var ajax_products_url = ajax_products;
				var image_count = {count($field.availableValues.images)|intval};
				var image_number_limit = {$field.availableValues.image_number_limit|intval};
				{literal}
					/** _agile_ Ready Function _agile_ **/
					$(document).ready(function() {
						$.fn.editable.defaults.mode = 'inline';
						$('.editable').editable();

						$("#imageTable").tableDnD({
							onDrop: function(table, row) {
								current = $(row).attr("id");
								stop = false;
								image_up = "{";
								$("#imageList").find("tr").each(function(i) {
									$("#td_" + $(this).attr("id")).html(i + 1);
									if ($(this).attr("id") == current) {
										image_up += '"' + $(this).attr("id") + '" : ' + (i + 1) + ',';
										stop = true;
									}
									if (!stop || (i + 1) == 2)
										image_up += '"' + $(this).attr("id") + '" : ' + (i + 1) + ',';
								});
								image_up = image_up.slice(0, -1);
								image_up += "}";
								updateImagePositon(image_up);
							}
						});
						var filecheck = 1;

						/**
		 * on success function
		 */
						function afterDeleteProductImage(data) {
							data = $.parseJSON(data);
							if (data) {
								cover = 0;
								id = data.content.id;
								if (data.status == 'ok') {
									if ($("#" + id).find(".covered").attr("src") == "{$base_dir_ssl}img/admin/enabled.gif")
									cover = 1;
									$("#" + id).remove();
								}
								if (cover)
									$("#imageTable tr").eq(1).find(".covered").attr("src", "{$base_dir_ssl}img/admin/enabled.gif");
									$("#countImage").html(parseInt($("#countImage").html()) - 1);
								refreshImagePositions($("#imageTable"));

								image_count--;
								if (image_count < image_number_limit && image_number_limit > 0) $("#divUploadControl").show();

								if (data.confirmations) agile_show_message(data.confirmations);

								if (parseInt($("#countImage").html()) <= 1)
									$('#caption_selection').addClass('hidden');

							}
						}

						$('.delete_product_image').off().on('click', function(e) {
							e.preventDefault();
							id = $(this).parent().parent().attr('id');
							if (confirm("{/literal}{l s='Are you sure?' d='Modules.Agilemultisellerpro.Products' js=1}{literal}"))
							doFrontAjax(ajax_products_url, {
									"action": "deleteProductImage",
									"id_image": id,
									"id_product" : {/literal}{$id_product|intval}{literal},
									"id_category" : {/literal}{$id_category_default|intval}{literal},
									"ajax": 1
								},
								afterDeleteProductImage
							);
						});

						$('.covered').off().on('click', function(e) {
							e.preventDefault();
							id = $(this).parent().parent().parent().attr('id');

							$("#imageList .cover i.material-icons").each(function(i) {
								$(this).removeClass('btn-outline-success').addClass('btn-outline-danger').text('close');
							});

							$(this).removeClass('btn-outline-danger').addClass('btn-outline-success').text('done');

							doFrontAjax(ajax_products_url, {
								"action": "UpdateCover",
								"id_image": id,
								"id_product" : {/literal}{$id_product|intval}{literal},
								"ajax": 1
							});

						});

						$('.image_shop').off().on('click', function() {
							active = false;
							if ($(this).attr("checked"))
								active = true;
							id = $(this).parent().parent().attr('id');
							id_shop = $(this).attr("id").replace(id, "");
							doFrontAjax(ajax_products_url, {
								"action": "UpdateProductImageShopAsso",
								"id_image": id,
								"id_shop": id_shop,
								"active": active,
								"token" : "{/literal}{$token|escape:'htmlall':'UTF-8'}{literal}",
								"tab": "AdminProducts",
								"ajax": 1
							});
						});

						/** _agile_ function	_agile_ **/
						function updateImagePositon(json) {
							doFrontAjax(ajax_products_url, {
								"action": "updateImagePosition",
								"json": json,
								"token" : "{/literal}{$token|escape:'htmlall':'UTF-8'}{literal}",
								"tab": "AdminProducts",
								"ajax": 1
							});

						}

						function delQueue(id) {
							$("#img" + id).fadeOut("slow");
							$("#img" + id).remove();
						}

						function refreshImagePositions(imageTable) {
							var reg = /_[0-9]$/g;
							var up_reg = new RegExp("imgPosition=[0-9]+&");

							imageTable.find("tbody tr").each(function(i, el) {
								$(el).find("td.positionImage").html(i + 1);
							});
							imageTable.find("tr td.dragHandle a:hidden").show();
							imageTable.find("tr td.dragHandle:first a:first").hide();
							imageTable.find("tr td.dragHandle:last a:last").hide();
						}

					});
				{/literal}
			</script>
		{/if}
		{* Images end --------------------------------------------------- *}


		{* Association begin --------------------------------------------------- *}
		{if $product_menu == 4}
			<script type="text/javascript">
				var base_dir_ssl = "{$base_dir_ssl|escape:'htmlall':'UTF-8'}";

				function categorisTreeCallback() {
					searchCategory();
					buildTreeView();
					/** _agile_ Not allow product in home _agile_ **/
					if({$formFields[0]['availableValues']['allow_register_athome']} != 1) {
					$("input[type='checkbox'][name='categoryBox[]'][value=2]").parent().remove();
				}
				}
			</script>
		{/if}
		{* Association end --------------------------------------------------- *}



		{* Prices begin --------------------------------------------------- *}
		{if $product_menu == 5}
			<script type="text/javascript">
				var product_prices = new Array();
				{foreach from=$field.availableValues.combinations item='combination'}
					product_prices['{$combination.id_product_attribute|intval}'] = '{$combination.price|escape:'htmlall':'UTF-8'}';
				{/foreach}
			</script>

			<script type="text/javascript">
				noTax = {if $field.availableValues.tax_exclude_taxe_option}true{else}false{/if};
				taxesArray = taxesRatesByGroup;
				ecotaxTaxRate = {($field.availableValues.ecotaxTaxRate / 100)|floatval};
			</script>

			<script type="text/javascript">
				var Customer = new Object();
				var ecotax_tax_excl = parseFloat({$product->ecotax|floatval});
				var priceDisplayPrecision = {$smarty.const._PS_PRICE_DISPLAY_PRECISION_|intval};

				$(document).ready(function() {
					Customer = {
							"hiddenField": jQuery('#id_customer'),
							"field": jQuery('#customer'),
							"container": jQuery('#customers'),
							"loader": jQuery('#customerLoader'),
							"init": function() {
								jQuery(Customer.field).typeWatch({
									"captureLength": 1,
									"highlight": true,
									"wait": 50,
									"callback": Customer.search
								}).focus(Customer.placeholderIn).blur(Customer.placeholderOut);
							},
							"placeholderIn": function() {
								if (this.value == '{l s='All customers' d='Modules.Agilemultisellerpro.Products'}') {
								this.value = '';
							}
						},
						"placeholderOut": function() {
							if (this.value == '') {
								this.value = '{l s='All customers' d='Modules.Agilemultisellerpro.Products'}';
							}
						},
						"search": function() {
							Customer.showLoader();
							jQuery.ajax({
								"type": "POST",
								"url": ajax_agile_getcustomers,
								"async": true,
								"dataType": "json",
								"data": {
									"ajax": "1",
									"tab": "AgileProductCustomers",
									"action": "searchCustomers",
									"customer_search": "John"
								},
								"success": Customer.success
							});
						},
						"success": function(result) {
							if (result.found) {
								var html = '<ul class="list-unstyled">';
								jQuery.each(result.customers, function() {
									html += '<li>' + this.firstname + ' ' + this.lastname + (this.birthday ? ' - ' + this.birthday : '');
									html += ' - ' + this.email;
									html += '<a onclick="Customer.select('+this.id_customer+', \''+this.firstname+' '+this.lastname+'\'); return false;" href="#" class="btn btn-default">{l s='Choose' d='Modules.Agilemultisellerpro.Products'}</a></li>';
								});
								html += '</ul>';
							} else
								html = '<div class="alert alert-warning">{l s='No customers found' d='Modules.Agilemultisellerpro.Products'}</div>';
								Customer.hideLoader();
							Customer.container.html(html);
							jQuery('.fancybox', Customer.container).fancybox();
						},
						"select": function(id_customer, fullname) {
							Customer.hiddenField.val(id_customer);
							Customer.field.val(fullname);
							Customer.container.empty();
							return false;
						},
						"showLoader": function() {
							Customer.loader.fadeIn();
						},
						"hideLoader": function() {
							Customer.loader.fadeOut();
						}
				}; Customer.init();
				});
			</script>

			<script type="text/javascript">
				$(document).ready(function() {
					product_prices['0'] = $('#sp_current_ht_price').html();

					$('#id_product_attribute').change(function() {
						$('#sp_current_ht_price').html(product_prices[$('#id_product_attribute option:selected').val()]);
					});

					$('#leave_bprice').click(function() {
						if (this.checked)
							$('#sp_price').attr('disabled', 'disabled');
						else
							$('#sp_price').removeAttr('disabled');
					});

					calcPriceTI();
					unitPriceWithTax('unit');

				});
			</script>
		{/if}
		{* Prices end --------------------------------------------------- *}

		{* Quantities begin --------------------------------------------------- *}
		{if $product_menu == 6}
			<script type="text/javascript">
				var quantities_ajax_success = '{l s='Data saved' d='Modules.Agilemultisellerpro.Products'}';
				var quantities_ajax_waiting = '{l s='Saving data...' d='Modules.Agilemultisellerpro.Products'}';
			</script>
			<div class="agile-padding"> </div>
			</div>

			<script type="text/javascript">
				var showAjaxError = function(msg) {
					$('#available_quantity_ajax_error_msg').html(msg);
					$('#available_quantity_ajax_error_msg').show();
					$('#available_quantity_ajax_msg').hide();
					$('#available_quantity_ajax_success_msg').hide();
				};

				var showAjaxSuccess = function(msg) {
					$('#available_quantity_ajax_success_msg').html(msg);
					$('#available_quantity_ajax_error_msg').hide();
					$('#available_quantity_ajax_msg').hide();
					$('#available_quantity_ajax_success_msg').show();
				};

				var showAjaxMsg = function(msg) {
					$('#available_quantity_ajax_msg').html(msg);
					$('#available_quantity_ajax_error_msg').hide();
					$('#available_quantity_ajax_msg').show();
					$('#available_quantity_ajax_success_msg').hide();
				};

				var ajaxCall = function(data) {
					data.ajaxProductQuantity = 1;
					data.id_product = '{$product->id|intval}';
					data.ajax = 1;
					data.action = "productQuantity";
					showAjaxMsg('{l s='Saving data...' d='Modules.Agilemultisellerpro.Products'}');
					$.ajax({
						type: "POST",
						// url: "{$base_dir_ssl|escape:'htmlall':'UTF-8'}modules/agilemultiseller/ajax_products.php",
						url: ajax_products,
						data: data,
						dataType: 'json',
						async: true,
						success: function(msg) {
							if (msg.error) {
								showAjaxError(msg.error);
								return;
							}
							showAjaxSuccess('{l s='Data saved' d='Modules.Agilemultisellerpro.Products'}');
						},
						error: function(msg) {
							showAjaxError(msg.error);
						}
					});
				};

				var refreshQtyAvaibilityForm = function() {
					if ($('#depends_on_stock_0').attr('checked')) {
						$('.available_quantity').find('input').show();
						$('.available_quantity').find('span').hide();
					} else {
						$('.available_quantity').find('input').hide();
						$('.available_quantity').find('span').show();
					}
				};

				$('.depends_on_stock').click(function(e) {
					refreshQtyAvaibilityForm();
					ajaxCall({ actionQty: 'depends_on_stock', value: $(this).val() });
					if ($(this).val() == 0)
						$('.available_quantity input').trigger('change');
				});

				$('.advanced_stock_management').click(function(e) {
					var val = 0;
					if ($(this).attr('checked'))
						val = 1;

					ajaxCall({ actionQty: 'advanced_stock_management', value: val });
					if (val == 1) {
						$(this).val(1);
						$('#depends_on_stock_1').attr('disabled', false);
					} else {
						$(this).val(0);
						$('#depends_on_stock_1').attr('disabled', true);
						$('#depends_on_stock_0').attr('checked', true);
						ajaxCall({ actionQty: 'depends_on_stock', value: 0 });
						refreshQtyAvaibilityForm();
					}
					refreshQtyAvaibilityForm();
				});

				/** _agile_ bind enter key event on search field _agile_ **/
				$('.available_quantity').find('input').bind('keypress', function(e) {
					var code = (e.keyCode ? e.keyCode : e.which);
					if (code == 13) {
						/** _agile_ Enter keycode  field _agile_ **/
						e.stopPropagation(); /** _agile_ Stop event propagation  field _agile_ **/
						return false;
					}
				});

				$('.available_quantity').find('input').change(function(e, init_val) {
					ajaxCall({ actionQty: 'set_qty', id_product_attribute: $(this).parent().attr('id').split('_')[1], value: $(this)
							.val() });
				});

				$('[name="out_of_stock"]').on('change', function(e) {
					refreshQtyAvaibilityForm();
					ajaxCall({ actionQty: 'out_of_stock', value: $(this).val() });
				});

				refreshQtyAvaibilityForm();
			</script>
		{/if}
		{* Quantities end --------------------------------------------------- *}


		{* Combination begin --------------------------------------------------- *}
		{if $product_menu == 7 && !$product->is_virtual}
			<script type="text/javascript">
				noTax = {if $field.availableValues.tax_exclude_taxe_option}true{else}false{/if};
				taxesArray = taxesRatesByGroup;

				ecotaxTaxRate = {($field.availableValues.ecotaxTaxRate / 100)|floatval};
			</script>


			<script type="text/javascript">
				var msg_combination_1 = "{l s='Please choose an attribute' d='Modules.Agilemultisellerpro.Products'}";
				var msg_combination_2 = "{l s='Please choose a value' d='Modules.Agilemultisellerpro.Products'}";
				var msg_combination_3 = "{l s='You can only add one combination per type of attribute' d='Modules.Agilemultisellerpro.Products'}";
				var msg_new_combination = "{l s='New combination' d='Modules.Agilemultisellerpro.Products'}";
				var msg_cancel_combination = '{l s='Cancel combination'  d='Modules.Agilemultisellerpro.Products'}';

				var combination_images = combinationImagesJs;
				// console.log(combination_images)
				var attrs = new Array();
				var modifyattributegroup = "{l s='Modify this attribute combination'  d='Modules.Agilemultisellerpro.Products' js=1}";
				attrs[0] = new Array(0, "---");

				{foreach from=$field.availableValues.attributeJs key=idgrp item=group}
					attrs[{$idgrp|intval}] = [
					0,
					'---'
					{foreach from=$group key=idattr item=attrname}
						, "{$idattr|intval}", "{$attrname|escape:'htmlall':'UTF-8'}"
					{/foreach}
					];
				{/foreach}

				add_new_combination_form_visible = {if !empty($errors)}true{else}false{/if};
				// ajax_products_url = "{$base_dir_ssl|escape:'htmlall':'UTF-8'}modules/agilemultiseller/ajax_products.php";
				ajax_products_url = ajax_products;

				$(document).ready(function() {
					check_impact();
					check_weight_impact();
					check_unit_impact();

					$('[name="attribute_price_impact"]').on('change', function(){
						check_impact();
						calcImpactPriceTI();
					})

					$('[name="attribute_weight_impact"]').on('change', function(){
						check_weight_impact();
					})

					$('[name="attribute_unit_impact"]').on('change', function(){
						check_unit_impact();
					})
				})

				function toggle_add_new_combination_form() {
					attr_selectall();
					populate_attrs();
					check_impact();
					check_weight_impact();
					check_unit_impact();
					if (add_new_combination_form_visible) {
						$("#add_new_combination").hide();
						$("#brnCancelNewComb").hide();
						$("#submitCombinations").hide();
						$("#btnAddNewComb").show();
					} else {
						$("#add_new_combination").show();
						$("#brnCancelNewComb").show();
						$("#submitCombinations").show();
						$("#btnAddNewComb").hide();
					}
					add_new_combination_form_visible = !add_new_combination_form_visible;
				}

				function delete_comb(id_product, id_product_attribute) {
					if(confirm("{l s='Are sure want to delete this combination?' d='Modules.Agilemultisellerpro.Products' js=1}"))
					{
						doFrontAjax(ajax_products_url, {
								"action": "deleteProductAttribute",
								"id_product": id_product,
								"id_product_attribute": id_product_attribute,
								"ajax": 1
							},
							function(data) {
								data = $.parseJSON(data);
								agile_show_message(data.message);
								$("#tr_comb_" + id_product_attribute).remove();
							}
						);
					}
				}

				function set_default_comb(id_product, id_product_attribute) {
					doFrontAjax(ajax_products_url, {
							"action": "defaultProductAttribute",
							"id_product": id_product,
							"id_product_attribute": id_product_attribute,
							"ajax": 1
						},
						function(data) {
							data = $.parseJSON(data);
							agile_show_message(data.message);
							$("[id^='icon_default_']").text('radio_button_unchecked');
							$("#icon_default_" + id_product_attribute).text('check_circle_outline');
						}
					);

				}

				function edit_comb(id_product, id_product_attribute) {
					$.ajax({
						url: ajax_products_url,
						type: "POST",
						data: {
							"id_product": id_product,
							"id_product_attribute": id_product_attribute,
							ajax: true,
							action: 'editProductAttribute'
						},
						dataType: 'json',
						async: false,
						success: function(data) {
							// color the selected line
							add_new_combination_form_visible = false;
							toggle_add_new_combination_form();
							$('#attribute_quantity').show();
							var wholesale_price = Math.abs(data[0]['wholesale_price']);
							var price = data[0]['price'];
							var weight = data[0]['weight'];
							var unit_impact = data[0]['unit_price_impact'];
							var reference = data[0]['reference'];
							var ean = data[0]['ean13'];
							var quantity = data[0]['quantity'];
							var image = false;
							var product_att_list = new Array();
							for (i = 0; i < data.length; i++) {
								product_att_list.push(data[i]['group_name'] + ' : ' + data[i]['attribute_name']);
								product_att_list.push(data[i]['id_attribute']);
							}

							var id_product_attribute = data[0]['id_product_attribute'];
							var default_attribute = data[0]['default_on'];
							var eco_tax = data[0]['ecotax'];
							var upc = data[0]['upc'];
							var minimal_quantity = data[0]['minimal_quantity'];
							var available_date = data[0]['available_date'];

							if (wholesale_price != 0 && wholesale_price > 0) {
								$("#attribute_wholesale_price_full").show();
								$("#attribute_wholesale_price_blank").hide();
							} else {
								$("#attribute_wholesale_price_full").hide();
								$("#attribute_wholesale_price_blank").show();
							}
							fillCombination(
								wholesale_price,
								price,
								weight,
								unit_impact,
								reference,
								ean,
								quantity,
								image,
								product_att_list,
								id_product_attribute,
								default_attribute,
								eco_tax,
								upc,
								minimal_quantity,
								available_date
							);
							//calcImpactPriceTI();
						}
					});
				}

				function fillCombination(wholesale_price, price_impact, weight_impact, unit_impact, reference,
					ean, quantity, image, old_attr, id_product_attribute, default_attribute, eco_tax, upc, minimal_quantity,
					available_date) {
					var link = '';
					init_elems();
					$('#stock_mvt_attribute').show();
					$('#initial_stock_attribute').hide();
					$('#attribute_quantity').html(quantity);
					$('#attribute_quantity').show();
					$('#attr_qty_stock').show();

					$('[name="attribute_minimal_quantity"]').val(minimal_quantity);

					getE('attribute_reference').value = reference;

					getE('attribute_ean13').value = ean;
					getE('attribute_upc').value = upc;
					//getE('attribute_wholesale_price').value = Math.abs(wholesale_price);
					getE('attribute_price').value = ps_round(Math.abs(price_impact), 2);
					//getE('attribute_priceTEReal').value = Math.abs(price_impact);
					//getE('attribute_weight').value = Math.abs(weight_impact);
					//getE('attribute_unity').value = Math.abs(unit_impact);
					if ($('#attribute_ecotax').length != 0)
						getE('attribute_ecotax').value = eco_tax;

					if (default_attribute == 1)
						getE('attribute_default').checked = true;
					else
						getE('attribute_default').checked = false;

					// if (price_impact < 0) {
					// 	getE('attribute_price_impact').options[getE('attribute_price_impact').selectedIndex].value = -1;
					// 	getE('attribute_price_impact').selectedIndex = 2;
					// } else if (!price_impact) {
					// 	getE('attribute_price_impact').options[getE('attribute_price_impact').selectedIndex].value = 0;
					// 	getE('attribute_price_impact').selectedIndex = 0;
					// } else if (price_impact > 0) {
					// 	getE('attribute_price_impact').options[getE('attribute_price_impact').selectedIndex].value = 1;
					// 	getE('attribute_price_impact').selectedIndex = 1;
					// }
					// if (weight_impact < 0) {
					// 	getE('attribute_weight_impact').options[getE('attribute_weight_impact').selectedIndex].value = -1;
					// 	getE('attribute_weight_impact').selectedIndex = 2;
					// } else if (!weight_impact) {
					// 	getE('attribute_weight_impact').options[getE('attribute_weight_impact').selectedIndex].value = 0;
					// 	getE('attribute_weight_impact').selectedIndex = 0;
					// } else if (weight_impact > 0) {
					// 	getE('attribute_weight_impact').options[getE('attribute_weight_impact').selectedIndex].value = 1;
					// 	getE('attribute_weight_impact').selectedIndex = 1;
					// }
					// if (unit_impact < 0) {
					// 	getE('attribute_unit_impact').options[getE('attribute_unit_impact').selectedIndex].value = -1;
					// 	getE('attribute_unit_impact').selectedIndex = 2;
					// } else if (!unit_impact) {
					// 	getE('attribute_unit_impact').options[getE('attribute_unit_impact').selectedIndex].value = 0;
					// 	getE('attribute_unit_impact').selectedIndex = 0;
					// } else if (unit_impact > 0) {
					// 	getE('attribute_unit_impact').options[getE('attribute_unit_impact').selectedIndex].value = 1;
					// 	getE('attribute_unit_impact').selectedIndex = 1;
					// }

					$("#add_new_combination").show();

					/* Reset all combination images */
					combinationImages = $('#id_image_attr').find("input[id^=id_image_attr_]");
					combinationImages.each(function() {
						this.checked = false;
					});

					/* Check combination images */

					if (typeof(combination_images[id_product_attribute]) != 'undefined')
						for (i = 0; i < combination_images[id_product_attribute].length; i++)
							$('#id_image_attr_' + combination_images[id_product_attribute][i]['id_image']).prop('checked', true);

					check_impact();
					check_weight_impact();
					check_unit_impact();

					var elem = getE('product_att_list');

					for (var i = 0; i < old_attr.length; i++) {
						var opt = document.createElement('option');
						opt.text = old_attr[i++];
						opt.value = old_attr[i];
						try {
							elem.add(opt, null);
						} catch (ex) {
							elem.add(opt);
						}
					}
					getE('id_product_attribute').value = id_product_attribute;

					$('[name="available_date_attribute"]').val(available_date);
				}

				init_elems = function() {
					var impact = getE('attribute_price_impact');
					var impact2 = getE('attribute_weight_impact');
					var elem = getE('product_att_list');

					if (elem.length)
						for (i = elem.length - 1; i >= 0; i--)
							if (elem[i])
								elem.remove(i);

					$('input[name="id_image_attr[]"]').each(function() {
						$(this).attr('checked', false);
					});

					$('[name="attribute_default"]').attr('checked', false);

					// getE('attribute_price_impact').selectedIndex = 0;
					// getE('attribute_weight_impact').selectedIndex = 0;
					// getE('attribute_unit_impact').selectedIndex = 0;
					$('#span_unit_impact').hide();
					$('#unity_third').html($('#unity_second').html());

					if ($('#unity').is())
						if ($('#unity').get(0).value.length > 0)
							$('#tr_unit_impact').show();
						else
							$('#tr_unit_impact').hide();
					try {
						if (impact.options[impact.selectedIndex].value == 0)
							$('#span_impact').hide();
						if (impact2.options[impact.selectedIndex].value == 0)
							getE('span_weight_impact').style.display = 'none';
					} catch (e) {
						$('#span_impact').hide();
						//getE('span_weight_impact').style.display = 'none';
					}
				}


				function select_all() {
					$("#product_att_list option").attr("selected", "selected");
				}

				function attribute_price_keyup() {
					var price = $('#attribute_price').val().replace(/,/g, '.');
					$('#attribute_priceTEReal').val(price);
					if (isArrowKey(event)) return;
					$('#attribute_price').val(price);
					calcImpactPriceTI();
				}


				function attribute_priceTI_keyup() {
					if (isArrowKey(event)) return;
					calcImpactPriceTE();
				}
			</script>

		{/if}
		{* Combination end --------------------------------------------------- *}


		{* Virtual product begin --------------------------------------------------- *}
		{if $product_menu == 8}
			<script type="text/javascript">
				var newLabel = '{l s='New label' d='Modules.Agilemultisellerpro.Products'}';
				var choose_language = '{l s='Choose language:' d='Modules.Agilemultisellerpro.Products'}';
				var required = '{l s='required' d='Modules.Agilemultisellerpro.Products'}';
				var customizationUploadableFileNumber = '{$product->uploadable_files|escape:'htmlall':'UTF-8'}';
				var customizationTextFieldNumber = '{$product->text_fields|escape:'htmlall':'UTF-8'}';
				var uploadableFileLabel = 0;
				var textFieldLabel = 0;
				var cache_default_attribute =  {$product->cache_default_attribute|intval};
				var base_dir_ssl = "{$base_dir_ssl|escape:'htmlall':'UTF-8'}";

				$(document).ready(function() {
					show_hide_error();

					$('[name="is_virtual_good"]').on('change', function() {
						is_virtual_goods_onchange();
					})

					$('[name="is_virtual_file"]').on('change', function() {
						is_virtual_file_onclick();
					})

					$('[name="virtual_product_file"]').on('change', function() {
						uploadFile();
					})
				});

				function uploadFile() {
					$("#imgProcessing").show();
					$.ajaxFileUpload({
						url: '{$link->getModuleLink('agilemultisellerpro', 'uploadproductfile')|escape:'htmlall':'UTF-8'}',
						secureuri: false,
						fileElementId: 'field-virtual_product_file',
						dataType: 'xml',
						success: function(data, status) {
							$("#imgProcessing").hide();
							data = data.getElementsByTagName('return')[0];
							var result = data.getAttribute("result");
							var msg = data.getAttribute("msg");
							var fileName = data.getAttribute("filename");
							if (result == "error") {
								$("#upload-confirmation").hide();
								$("#upload-error td").html('<div class="error">{l s='Error:' d='Modules.Agilemultisellerpro.Products'} ' + msg + '</div>');
								$("#upload-error").show();
							} else {
								$('#upload-error').hide();
								$('#file_missing').hide();
								$('#upload_input').hide();
								$('[name="virtual_product_name"]').attr('value', fileName);
								$("#upload-confirmation .error").remove();
								$('#upload-confirmation div').prepend('<span>{l s='The file' d='Modules.Agilemultisellerpro.Products'}&nbsp;"'+fileName+'"&nbsp;{l s='has successfully been uploaded' d='Modules.Agilemultisellerpro.Products'}' +
								'</span>');
								$("#upload-confirmation").show();

								$('[name="virtual_product_filename"]').val(msg);

								$('[name="virtual_product_file"]').closest('.form-group').remove();
							}
						}
					});
				}

				function ajax_delete_virtual_downloadfile() {
					var ans = confirm("{l s='Are you sure want to delete the file?' d='Modules.Agilemultisellerpro.Products'}");
					if (!ans) return false;

					$.ajax({
						url: ajax_products,
						type: "POST",
						data: {
							action: 'deleteVirtualProduct',
							id_product: {$product->id|intval}
						},
						dataType: 'json',
						async: false,
						success: function(data) {
							if (data.status == 'ok') {
								$('#file_missing').show();
								$('#upload_input').show();
								$("#upload-confirmation").hide();
								$("#tr_link_to_file").remove();
							} else
								agile_show_message(data.message);
						}
					});

					return false;
				}

				function is_virtual_goods_onchange() {
					if ($('[name="is_virtual_good"]').is(":checked")) {
						$("#tr_downloadable").show();
						is_virtual_file_onclick();
					} else {
						$("#tr_downloadable").hide();
						$("#virtual_good").hide();
					}
					show_hide_error();
				}

				function is_virtual_file_onclick() {
					if ($("[name=is_virtual_file]:checked").val() == 1) {
						$("#virtual_good").show();
					} else {
						$("#virtual_good").hide();
					}
					show_hide_error();
				}

				function show_hide_error() {
					if (cache_default_attribute)
						$("#error_edit_file").show();
					else
						$("#error_edit_file").hide();
				}
			</script>
		{/if}
		{* Virtual product end --------------------------------------------------- *}

		{* Shipping begin --------------------------------------------------- *}
		{if $product_menu == 9}
			<script>
				$(document).ready(function() {
					$("#addCarrier").on('click', function(e) {
						$('#availableCarriers option:selected').each(function() {
							$('#selectedCarriers').append("<option value='" + $(this).val() + "'>" + $(this).text() +
								"</option>");
							$(this).remove();
						});
						$('#selectedCarriers option').prop('selected', true);
					});

					$("#removeCarrier").on('click', function(e) {
						$('#selectedCarriers option:selected').each(function() {
							$('#availableCarriers').append("<option value='" + $(this).val() + "'>" + $(this).text() +
								"</option>");
							$(this).remove();
						});
						$('#selectedCarriers option').prop('selected', true);
					});

					$('[name="submitTabProduct"]').on('click', function() {
						$("#selectedCarriers").find("option").each(function() {
							$(this).prop("selected", "selected");
						});
					});

				});
			</script>
		{/if}
		{* Shipping end --------------------------------------------------- *}


		{* Attachment begin --------------------------------------------------- *}
		{if $product_menu == 10}
			<script type="text/javascript">
				var pathCSS = prestashop.urls.css_url;
				hideOtherLanguage({$id_language|intval});
			</script>

			<script type="text/javascript">
				$('document').ready(function() {
					$("#addAttachment").on('click', function() {
						$('#selectAttachment1 option:selected').each(function() {
							$('#selectAttachment2').append("<option value='" + $(this).val() + "'>" + $(this).text() +
								"</option>");
							$(this).remove();
						});
						$('#selectAttachment2 option').prop('selected', true);
					});

					$("#removeAttachment").on('click', function() {
						$('#selectAttachment2 option:selected').each(function() {
							$('#selectAttachment1').append("<option value='" + $(this).val() + "'>" + $(this).text() +
								"</option>");
							$(this).remove();
						});
					});

					$('[name="submitTabProduct"]').on('click', function() {
						$("#selectAttachment2 option").each(function(i) {
							$(this).prop("selected", "selected");
						});
					});
				});
			</script>
		{/if}
		{* Attachment begin --------------------------------------------------- *}


		{if $product_menu == 11}
			{* ProductTags begin --------------------------------------------------- *}
			<script>
				$(function() {
					$("#product_tag_save").on("click", function(e) {
						$('.succes_save_tag').addClass('hidden');
						e.preventDefault();
						array_tags = [];
						$('#product_tags .agile_tag').each(function() {
							name = $(this).attr('name');
							id_tag = name.replace('id_tag_', '');
							array_tags.push({
								'id_tag': id_tag,
								'check': $(this).is(':checked')
							});
						})
						var url = $('#product_tags #site_url').val() + 'modules/agileproducttags/ajax.php'
						changeTags(array_tags, url, $('#product_tags #id_product').val());
					});

					$('#product_tags .agile_tag').on('change', function() {
						$('.succes_save_tag').addClass('hidden');
					});

					function changeTags(array_tags, url, id_product) {
						$.ajax({
							type: "post",
							url: url,
							cache: false,
							async: true,
							dataType: "json",
							data: {
								'array_tags': array_tags,
								'id_product': id_product,
								'pageType': 'saveProductTags',
							},
							success: function(response) {
								$('.succes_save_tag').removeClass('hidden');
							},
							error: function(XMLHttpRequest, textStatus, errorThrown) {
								console.log("TECHNICAL ERROR: unable to load form.\n\nDetails:\nError thrown: " + XMLHttpRequest +
									"\n" + 'Text status: ' + textStatus);
							}
						});
					}
				});
			</script>
		{/if}
		{* ProductTags End --------------------------------------------------- *}

		{* Description and Seo begin ---------------------------------------------------------- *}
		{if $product_menu == 12}
			<script>
				{foreach from=$languages item=language}
					{literal}
						$().ready(function() {
						var input_id = '{/literal}tags_{$language.id_lang|intval}{literal}';
						$('#'+input_id).tagify({delimiters: [13,44], addTagPrompt: '{/literal}{l s='Add tag' d='Modules.Agilemultisellerpro.Products' js=1}{literal}'});
							$({/literal}'#product_form{literal}').submit( function() {
							$(this).find('#' + input_id).val($('#' + input_id).tagify('serialize'));
							});
							});
					{/literal}
				{/foreach}
			</script>
		{/if}
		{* Description and Seo end ---------------------------------------------------------- *}

		{* Options begin ---------------------------------------------------------- *}
		{if $product_menu == 13}
			<script>
				$(document).ready(function() {
					$("[name='available_for_order']").click(function() {
						showPrice();
					});

					showPrice();
				});

				function showPrice() {
					if ($("[name='available_for_order']").is(':checked')) {
						$('[name=\'show_price\']').attr('disabled', 'disabled').closest('.form-group').slideUp();
					} else {
						$('[name=\'show_price\']').removeAttr('disabled').closest('.form-group').slideDown();
					}
				}
			</script>
		{/if}
		{* Options end ---------------------------------------------------------- *}
	{/block}
