/**
* 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 show_customer_search(jsonData){
	if (jsonData.id_customer.length > 0) {
		window.parent.$("#td_choose").css("display", "block");
		var html_new = '';
		html_new +=     '<label class="control-label col-lg-3" style="padding-left: 15px;padding-top: 10px;float:left;width: 30%;">';
		html_new +=     '<span title="" data-toggle="tooltip" class="label-tooltip">Select a Customer:';
		html_new +=     '</span></label>';
		html_new +=     '<div class="col-lg-6" style="width:67.5%;padding-top: 8px;float:left;padding-left:45px;">';
		html_new +=     '<select id="td_id_customer" name="id_customer">';
		html_new +=     '<option  value="">Select customer</option>';
		for(i=0;i<jsonData.id_customer.length;i++){
			html_new += '<option  value="'+ jsonData.id_customer[i] + ' ">'+jsonData.firstname[i]+' '+jsonData.lastname[i]+'</option>';
		}
		html_new +=     '</select></div>';
		window.parent.$("#select_customer").html(html_new);
	} else {
		document.getElementById("alert_customer").style.display = 'block';
	}
}
    

$(document).ready(function(){
    
    $("#td_choose").click(function(){
        document.getElementById("td_background_popup_newcus").style.display = 'none';
        document.getElementById("td_new_cus").style.display = 'none';
        window.parent.$(".ba_newsletter_popup_background").css("display", "none");
        var id_customer = document.getElementById('td_id_customer').value;
        jQuery.ajax({
            type: 'POST',//phương thức gửi
            jscrossDomain: window.location.hostname,
            crossDomain: true,
            dataType: "json",
            url: '../index.php?controller=customernewquote&fc=module&module=askforaquotemodul', // lấy link của controller mới
            data: 'id_customer=' + id_customer + '&jslanguageid=' + jslanguageid + '&jsshopid=' + jsshopid + '&quote_token=' + token_askaquotemodule + '&ajax=1',  // lấy dữ liệu từ id product ở trên định nghĩa
            success: function(jsonData1,textStatus,jqXHR)
            {
                input_customer_quote(jsonData1);
                window.parent.$("#select_cus").css("display", "none");
                $("#input_first_name").attr("value", jsonData1.firstname);
                $("#input_last_name").attr("value", jsonData1.lastname);
                $("#input_email").attr("value", jsonData1.email);
                $("#input_company").attr("value", jsonData1.company);
                $("#input_phone").attr("value", jsonData1.phone);

            }  
        });
    });
    $("#search_customer").keyup(function(){
		searchcustomer();
	});
});
function searchcustomer() {
	var search = jQuery('#search_customer').val();
	var baseurl = window.location.hostname;
	window.parent.$("#select_customer").css("display", "block");
	if(search.length >= '2'){
		jQuery.ajax({
			type: 'POST',//phương thức gửi
			jscrossDomain: window.location.hostname,
			crossDomain: true,
			dataType: "json",
			url: '../index.php?controller=customernewquote&fc=module&module=askforaquotemodul', // lấy link của controller mới
			data: 'search_customer=' + search + '&jslanguageid=' + jslanguageid + '&jsshopid=' + jsshopid + '&quote_token=' + token_askaquotemodule + '&ajax=1',
			beforeSend: function() {
				$('.loadicon_searchcus').css('display', 'inline-block');
			},
			success: function(jsonData,textStatus,jqXHR)
			{
				$('.loadicon_searchcus').css('display', 'none');
				// document.getElementById("products_found").style.display = 'block';
				document.getElementById("alert_customer").style.display = 'none';
				// document.getElementById("abc").style.display = 'block';
				// jsonData = JSON.parse(jsonData);
				show_customer_search(jsonData);
				// console.log(jsonData);
				 // alert(jsonData);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				// alert(XMLHttpRequest);alert(textStatus);alert(errorThrown);
				document.getElementById("alert_customer").style.display = 'block';
				document.getElementById("products_found").style.display = 'none';
			}
			
		});
		
	}else{
			// document.getElementById("alert_customer").style.display = 'block';
			document.getElementById("products_found").style.display = 'none';
			// document.getElementById("abc").style.display = 'none';
			$(".td_row_customer").remove();
	}
}
function input_customer_quote(jsonData1){
    var html_new = '';
    html_new += '<span>'+jsonData1.infor['firstname']+''+jsonData1.infor['lastname']+' [#' + jsonData1.infor['id_customer'] + ']</span><input class="td_id_customer" id="td_id_customerr" type="hidden" name="infor_quote[id_customer]" value="' + jsonData1.infor['id_customer'] + '">';
    html_new += '<input type="hidden" name="id_customer" value="#6">';
    window.parent.$(".td_customer_newquote").html(html_new);
}
$(document).ready(function(){
    $(".td_add_new_customer").click(function(e){
        document.getElementById("td_background_popup_newcus").style.display = 'block';
        document.getElementById("td_new_cus").style.display = 'block';
        window.parent.$(".ba_newsletter_popup_background").css("display", "block");
    });
    $('.td_close_new_cus').click(function() {
        // $(".td_body_new_cus").remove();
        // $(".td_close_new_cus").remove();
        document.getElementById("td_background_popup_newcus").style.display = 'none';
        document.getElementById("td_new_cus").style.display = 'none';
    });
});