/**
* 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 isValidEmailTest(emailTest) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailTest);
};

function showpro_quote(jsonData){
    var html = '';
    for(i=0;i<jsonData.number;i++){
        html+=        '<tr class = "'+jsonData[i].id_combi_dbboo+'" style="border: none;">';
        html+=           '<td class = "product_display" style = "width: 120px;border-left: none;border-right: none;">';
        html+=              '<a href="'+jsonData[i].link_pro+'">';
        html+=                  '<img src="'+jsonData[i].img_doubleboo+'" style="border: 1px solid #cacaca;border-radius: 6px;width:80px;" alt="'+jsonData[i].name+'">';
        html+=              '</a>';
        html+=            '</td>';
        html+=         '<td class = "product_display" style="border-left: none;border-right: none;">';
        html+=              '<a href="'+jsonData[i].link_pro+'">';
        html+=                 '<p>'+jsonData[i].name+'</p>';
        html+=              '</a>';
        if(jsonData[i].attribute_dbboo == '' || jsonData[i].attribute_dbboo == undefined){
        }else{
        html+=            '<small>'+jsonData[i].attribute_dbboo+' </small><br>';
        }
        if(jsonData[i].reference == ''){
        }else{
        html+=            '<small>'+table_header_sku + ' ' +jsonData[i].reference+' </small><br>';
        }
        html+=         '</td>';
        if (jsonData.check_opt_price == 1) {
            html+=            '<td class = "product_display quote_unit_price" style="border-left: none;border-right: none;">';
            html+=               jsonData[i].price_tax_formatted;    
            html+=          '</td>';
        } else {
            html+=            '<td class = "product_display quote_unit_price" style="border-left: none;border-right: none; display:none;">';
            html+=             jsonData[i].price_tax_formatted;        
            html+=          '</td>';
        }
        html+=          '<td class = "product_display" style="border-left: none;border-right: none;width: 20%;">';
        html+=                '<div>';
        html+=                     '<div style="width:40px; float:left;height:40px;margin-right: 1px;">';
        html+=                         '<input class = "count_dbboo'+jsonData[i].id_combi_dbboo+'_'+jsonData[i].id+'" size="3" maxlength="6" type="text" value="'+jsonData[i].so_sp+'" style = "border: 1px solid #bdbdbd;border-radius: 3px;text-align: center;width: 100%;height: 100%;" disabled>';
             
        html+=                     '</div>';
        html+=                     '<div>';
        html+=                            '<div>';
        html+=                              '<a onclick="up_product('+jsonData[i].id_combi_dbboo+','+jsonData[i].id+')" href="javascript:void(0);" class = "btn btn-default up_count" id="'+jsonData[i].id_combi_dbboo+'_'+jsonData[i].id+'" rel="1_1"><i class="icon-plus"></i></a>';
        html+=                                '<br>'
        html+=                              '<a onclick="down_product('+jsonData[i].id_combi_dbboo+','+jsonData[i].id+')"href="javascript:void(0);" class = "btn btn-default down_count" id = "'+jsonData[i].id_combi_dbboo+'_'+jsonData[i].id+'" rel="1_1"><i class="icon-minus"></i></a>';
        html+=                          '</div>';
        html+=                     '</div>';
        html+=               '</div>';
        html+=          '</td>';
		var t = jsonData[i].total_price_tax_formatted;
        if (jsonData.check_opt_price == 1) {
            html+=          '<td class = "product_display quote_total_price" style="border-left: none;border-right: none;">';
            html+=                '<span class = "price'+jsonData[i].id_combi_dbboo+'_'+jsonData[i].id+'" value="'+parseFloat(jsonData[i].price_tax).toFixed(2)+'">'+ t +'</span> ';
            html+=          '</td>';
        }else{
            html+=          '<td class = "product_display quote_total_price" style="border-left: none;border-right: none;display:none;">';
            html+=                '<span class = "price'+jsonData[i].id_combi_dbboo+'_'+jsonData[i].id+'" value="'+parseFloat(jsonData[i].price_tax).toFixed(2)+'">'+ t +'</span> ';
            html+=          '</td>';
        }
        html+=          '<td class = "product_display" style="border-left: none;border-right: none;">';
        html+=                '<a onclick="del_product('+jsonData[i].id_combi_dbboo+','+jsonData[i].id+')" class = "del_btn del_btn_pri" id= "del_dboo" fun_delete="1" data-id-rm = "'+jsonData[i].id+'"  data-id-combi="'+jsonData[i].id_combi_dbboo+'" href="javascript:void(0);" rel="1_1" style = "font-size: 20px;display: block;margin: auto;width: 20px;height: 25px;"><i class="icon-trash"></i></a>';
        html+=             '</td>';
        html+=      '</tr>';
    };
    
    if(jsonData.number == 0){
        jQuery(".quotes_cart_block").css("display", "none");
        var html_warning_quote = '';
        html_warning_quote += '<div >';
        html_warning_quote += '<p class="alert alert-warning">' + no_product_in_quote + '</p>'
        html_warning_quote += '</div>';
        jQuery(".quote_body_new_dbboo").html(html_warning_quote);
    };
    if(jsonData.number > 0)
    {
        jQuery(".quotes_cart_block").removeAttr("style");
        if (jsonData.check_opt_price == 1) {
            html+=        '<tr class="total_display" style = "border:none;">';
            html+=           '<td colspan="4" align="right" style= "text-align: right;border-left: none;border-right: none;"><h5>'+cart_total_l+'</h5></td>';
            html+=            '<td colspan="2" style="border-left: none;border-right: none;">';
            html+=                '<span class = "total" value = "' + parseFloat(jsonData._total_price).toFixed(2) +'" id="dbboo"  >' + jsonData._total_price_formatted +'</span>';
            html+=                '<input type="hidden" name="total_dbboo" value = "' + parseFloat(jsonData._total_price) +'"/>';
            html+=             '</td>';
            html+=        '</tr>';
        }else {
            html+=        '<tr class="total_display" style = "border:none; display:none;">';
            html+=           '<td colspan="4" align="right" style= "text-align: right;border-left: none;border-right: none;"><h5>'+cart_total_l+'</h5></td>';
            html+=            '<td colspan="2" style="border-left: none;border-right: none;">';
            html+=                '<span class = "total" value = "' + parseFloat(jsonData._total_price).toFixed(2) + '" id="dbboo"  >' + jsonData._total_price_formatted +'</span>';
            html+=                '<input type="hidden" name="total_dbboo" value = "' + parseFloat(jsonData._total_price) +'"/>';
			html+=             '</td>';
            html+=        '</tr>';
        }
    }
    jQuery(".table_body").html(html);
    
    var html1 = '';
    var html_count = '';
	html_count+= '<a href = "'+url_php+'index.php?controller=quotes&fc=module&module=askforaquotemodul" style = "font-size:17px;cursor:pointer;" class="td_cart_quote" >';
	html_count+=         '<i id="bag" class="ion-bag"></i>';
	html_count+= '</a>';
	if(jsonData.number > 0 && jsonData.number < 2){
		html_count+= '<span style = "font-size:11px;cursor:pointer;" class = "number_product_dbboo td_number_product_dbboo" id="'+jsonData.number+'">'+jsonData.number+'</span>';
		
	}
	else if (jsonData.number >= 2){
		html_count+= '<span style = "font-size:11px;cursor:pointer;" class = "number_product_dbboo td_number_product_dbboo" id="'+jsonData.number+'">'+jsonData.number+'</span>';
	}
	else{
		html_count+= '<span style = "font-size:11px;cursor:pointer;" class = "number_product_dbboo td_number_product_dbboo" id="'+jsonData.number+'">0</span>';
	}
    
    for(i=0;i<jsonData.number;i++){
        html1+= '<div class = "quote_list td_quote_list">';
        html1+=        '<a class="cart-images" href="'+jsonData[i].link_pro+'">';
        html1+=       '<img src="'+jsonData[i].img_doubleboo+'"  alt="'+jsonData[i].name+'" style="width:80px; height:80px">';
        html1+=      '</a>';
        html1+=  '<div class="cart_info_product td_cart_info_product">';
        html1+=     '<a href="'+jsonData[i].link_pro+'"><p class="td_name_quote">'+jsonData[i].name+'</p></a>';;
        if(jsonData[i].attr_dbboo == undefined){
            html1+=     '<small>   </small>';
        }else{
        html1+=     '<small class="td_attr_quote">'+jsonData[i].attr_dbboo+'</small>';
        }
        html1+=     '<div class="price_cart_product td_price_cart_product">';
        if (jsonData.check_opt_price == 1) {
            html1+=     '<span>'+ jsonData[i].price_tax_formatted+'</span></br></div>';
        } else {
            html1+=     '<span style="display:none;">'+ jsonData[i].price_tax_formatted +'</span></div>';
        }
        
        html1+=  '<div><span class = "td_count_product count_product">x'+jsonData[i].so_sp+'</span>';
        html1+=            '<div class="remove-wrap td_remove-wrap" >';
        html1+=           '<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>';
        html1+=            '</div>'
        html1+=     '</div></div>';
        html1+=     '</div>';
                
    }
	if (jsonData.check_opt_price == 1) {
		html1+=     '<div class="quotes-cart-prices td_quotes-cart-prices">';
		html1+=        '<span class="col-xs-12 col-lg-6 td_total" style="float:left;width:50px;font-weight: 700;">Total:</span>';
		html1+=        '<span class="quotes-cart-total col-xs-12 col-lg-6 td_total" style = "text-align:right;">';
		html1+=           jsonData._total_price_formatted;
		html1+=        '</span>';
		html1+=    '</div>';
	} else {
		html1+=     '<div class="quotes-cart-prices td_quotes-cart-prices" style="display:none;">';
		html1+=        '<span class="col-xs-12 col-lg-6 td_total" style="float:left;width:50px;font-weight: 700;">Total:</span>';
		html1+=        '<span class="quotes-cart-total col-xs-12 col-lg-6 td_total" style = "text-align:right;display:none;">';
		html1+=            jsonData._total_price_formatted;
		html1+=        '</span>';
		html1+=    '</div>';
	}
    jQuery(".quote_body").html(html1);
    jQuery(".quotes").html(html_count);
};

function del_product(id_combi_del,id_product){
        var session_id_doubleboo = ss_id;
        var baseurl = window.location.hostname;
        var dp_count_del = jQuery('.count_dbboo'+ id_combi_del +'_'+ id_product +'').val();
        
        var price_del = jQuery('.price'+ id_combi_del +'_'+ id_product +'').attr('value');
        var price_tt_del = price_del * dp_count_del;
        
        var total_pri = jQuery('.total').attr('value');

        var _total_pri = parseFloat(total_pri) - price_tt_del;
        jQuery('.total').val(_total_pri);
        jQuery('.total').html('...');
        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:'total_pri_del=' + _total_pri + '&id_product='+id_product + '&session_id=' + session_id_doubleboo  + '&id_combination=' +id_combi_del +'&name_btn=del' + '&quote_token=' + token_askaquotemodule + '&ajax=1',  // lấy dữ liệu từ id product ở trên định nghĩa
            dataType:"json",
            success: function(jsonData,textStatus,jqXHR)
            {        
                showpro_quote(jsonData);
                jQuery('.total').val(jsonData._total_price);
                jQuery('.total').html(jsonData._total_price_formatted);
            }   
        });
        jQuery(this).closest('tr').remove();
};


jQuery(document).ready(function(){
    if (window['email_dbboo'] == undefined) {
        email_dbboo = 0;
    } else{
        if(email_dbboo == 1)
        {
            if (error_email == 1) {
                jQuery(".email_dbboo").css("border", "1px solid red");
                jQuery(".show_warning_email").show();
            } else {
                jQuery(".email_dbboo").css("border", "1px solid #d6d4d4");
                jQuery(".show_warning_email").hide();
            }
        }
    }
    if (window['file_invalid'] == undefined) {
        file_invalid = 0;
    } else{
        if (file_invalid == 1) {
            jQuery(".show_warning_file_invalid").show();
        } else {
            jQuery(".show_warning_file_invalid").hide();
        }
    }
    if (window['error_type'] == undefined) {
        error_type = 0;
    } else{
        if (error_type == 1) {
            jQuery(".show_warning_file_type").show();
        } else {
            jQuery(".show_warning_file_type").hide();
        }
    }
    if (window['error_size'] == undefined) {
        error_size = 0;
    } else{
        if (error_size == 1) {
            jQuery(".show_warning_file_size").show();
        } else {
            jQuery(".show_warning_file_size").hide();
        }
    }
    if (window['error_captcha'] == undefined) {
        error_captcha = 0;
    } else{
        if (error_captcha == 1) {
            jQuery(".text_captcha").css("border", "1px solid red");
            jQuery(".show_warning").show();
        } else {
            jQuery(".text_captcha").css("border", "1px solid #d6d4d4");
            jQuery(".show_warning").hide();
        }
    }
    if (window['success_submit'] == undefined) {
        success_submit = 0;
    } else{
        if (success_submit == 1) {
            jQuery(".show_success_quote").show();
        }
    }
});