/**
* 2007-2020 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@buy-addons.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    Buy-addons    <contact@buy-addons.com>
* @copyright 2007-2020 Buy-addons
* @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

function updateQty(jsonData){
    var html = '';
    for(i=0;i<jsonData.number;i++){
        html+= '<div class = "quote_list td_quote_list">';
        html+=        '<a class="cart-images" href="'+jsonData[i].link_pro+'">';
        html+=       '<img src="'+jsonData[i].img_doubleboo+'"  alt="'+jsonData[i].name+'" style="width:80px; height:80px">';
        html+=      '</a>';
        html+=  '<div class="cart_info_product td_cart_info_product">';
        html+=     '<a href="'+jsonData[i].link_pro+'"><p class="td_name_quote">'+jsonData[i].name+'</p></a>';;
        if(jsonData[i].attr_dbboo == undefined){
            html+=     '<small>   </small>';
        }else{
        html+=     '<small class="td_attr_quote">'+jsonData[i].attr_dbboo+'</small>';
        }
        html+=     '<div class="price_cart_product td_price_cart_product">';
        if (jsonData.check_opt_price == 1) {
            html+=     '<span>'+ jsonData[i].price_tax_formatted+'</span></br></div>';
        } else {
            html+=     '<span style="display:none;">'+ jsonData[i].price_tax_formatted +'</span></div>';
        }
        
        html+=  '<div><span class = "td_count_product count_product">x'+jsonData[i].so_luong_sp+'</span>';
        html+=            '<div class="remove-wrap td_remove-wrap" >';
        html+=           '<a type = "submit" class = "remove_btn" fun_delete="1" data-id-rm = "'+jsonData[i].id+'" data-id-cb="'+jsonData[i].id_combi_dbboo+'"  href="javascript:void(0);"" rel="1_1"><i class="fa fa-trash" aria-hidden="true"></i></a>';
        html+=            '</div>'
        html+=     '</div></div>';
        html+=     '</div>';
                
    }
	if (jsonData.check_opt_price == 1) {
		html+=     '<div class="quotes-cart-prices td_quotes-cart-prices">';
		html+=        '<span class="col-xs-12 col-lg-6 td_total" style="float:left;width:50px;font-weight: 700;">Total:</span>';
		html+=        '<span class="quotes-cart-total col-xs-12 col-lg-6 td_total" style = "text-align:right;">';
		html+=           jsonData.product_total_price_formatted;
		html+=        '</span>';
		html+=    '</div>';
	} else {
		html+=     '<div class="quotes-cart-prices td_quotes-cart-prices" style="display:none;">';
		html+=        '<span class="col-xs-12 col-lg-6 td_total" style="float:left;width:50px;font-weight: 700;">Total:</span>';
		 html+=        '<span class="quotes-cart-total col-xs-12 col-lg-6 td_total" style = "text-align:right;display:none;">';
		html+=            jsonData.product_total_price_formatted;
		html+=        '</span>';
		html+=    '</div>';
	}
    jQuery(".quote_body").html(html);
}


function up_product(id_combi_up,id_pro_up){
    var session_id_doubleboo = ss_id;
    var dp_count = jQuery('.count_dbboo'+ id_combi_up +'_' + id_pro_up + '').val();
    var up = parseInt(dp_count) +1;
        jQuery('.count_dbboo'+ id_combi_up +'_' + id_pro_up +'').val(up);
        
        var price = jQuery('.price'+ id_combi_up +'_' + id_pro_up + '').attr('value');
        var up_price = parseFloat(price) * up;
        var total_pri = jQuery('#dbboo').attr('value');
        var up_total_pri = parseFloat(total_pri) + parseFloat(price);
        jQuery('#dbboo').attr('value', up_total_pri);
        jQuery('#dbboo').html('...');
        jQuery('input[name="total_dbboo"]').val(up_total_pri.toFixed(2));
        jQuery.ajax({
            type: 'POST',//phương thức gửi
            crossDomain: true,
            jscrossDomain: window.location.hostname,
            url: askquote_base_url + 'index.php?controller=viewlist&fc=module&module=askforaquotemodul', // lấy link của controller mới
            data:'price_dbboo=' + up_price + '&total_pri_dbboo=' + up_total_pri + '&so_luong=' + up + '&name_btn=count' + '&id_combination_count=' + id_combi_up + '&sessionid_count=' + session_id_doubleboo + '&quote_token=' + token_askaquotemodule + '&ajax=1' + '&id_product='+id_pro_up,  // lấy dữ liệu từ id product ở trên định nghĩa
            dataType:"json",
            success: function(jsonData_up,textStatus,jqXHR)
            {                
                updateQty(jsonData_up);
                jQuery('.price' +id_combi_up +'_' + id_pro_up).html(jsonData_up.product_pri_formatted);
                jQuery('.price' +id_combi_up +'_' + id_pro_up).parents("tr").find(".quote_unit_price").html(jsonData_up.unit_product_pri_formatted);
                jQuery('#dbboo').html(jsonData_up.product_total_price_formatted);
                
            }
        });
}

function down_product(id_combi_down,id_pro_down){
    var session_id_doubleboo = ss_id;
	var dp_count_down = jQuery('.count_dbboo'+ id_combi_down +'_' + id_pro_down + '').val();
	if(dp_count_down > 1){
		var down = parseInt(dp_count_down) -1;
		jQuery('.count_dbboo'+ id_combi_down +'_' + id_pro_down +'').val(down);        
		var price_down = jQuery('.price'+ id_combi_down +'_' + id_pro_down + '').attr('value');
		var down_price = parseFloat(price_down) * down;
		var total_pri_down = jQuery('#dbboo').attr('value');
		var down_total_pri= parseFloat(total_pri_down) - parseFloat(price_down);
		jQuery('#dbboo').attr('value',down_total_pri);
		jQuery('#dbboo').html('...');
		jQuery('input[name="total_dbboo"]').val(down_total_pri.toFixed(2));
		jQuery.ajax({
		type: 'POST',//phương thức gửi
		jscrossDomain: window.location.hostname,
		crossDomain: true,
		url: askquote_base_url + 'index.php?controller=viewlist&fc=module&module=askforaquotemodul', // lấy link của controller mới
		data:'price_dbboo=' + down_price + '&total_pri_dbboo=' + down_total_pri + '&so_luong=' + down + '&name_btn=count' + '&id_combination_count=' + id_combi_down + '&sessionid_count=' + session_id_doubleboo + '&quote_token=' + token_askaquotemodule + '&ajax=1' + '&id_product=' + id_pro_down,  // lấy dữ liệu từ id product ở trên định nghĩa
		dataType:"json",
		success: function(jsonData_down,textStatus,jqXHR)
		{                
			updateQty(jsonData_down);
			jQuery('.price' +id_combi_down +'_' + id_pro_down).html(jsonData_down.product_pri_formatted);
			jQuery('.price' +id_combi_down +'_' + id_pro_down).parents("tr").find(".quote_unit_price").html(jsonData_down.unit_product_pri_formatted);
			jQuery('#dbboo').html(jsonData_down.product_total_price_formatted);
		}
	});
	}
}


