$(document).ready(function(){
   $("#companyDetails").css("display", "none");
   $("#companybilling_address").css("display", "none");
   $("#companyshipping_address").css("display", "none");
   $("#qwe").css("display", "none");
   
    
   $('#Company_select').select2({
 ajax: {
        url: url_getcompany,
        type: "get",
        dataType: 'json',
        delay: 250,
        data: function (params) {
           return {
              searchTerm: params.term // search term
           };
        },
        processResults: function (response) {
           return {
              results: response
           };		   
		 },
        cache: true
      }
 });
  
 $('#Company_select').on('select2:select', function (e) {
		var company = e.params.data.id;
		
		$.ajax(url_getcompanydetails,{
			type : 'get',
			data :{'company' : company} ,
			success : function(data){
            $(document).find('#product_company').attr("data-id",data.id);
            $(document).find('#product_company').val(data.name);  
            $(document).find('#preview_company').attr("data-id",data.id);
            $(document).find('#preview_company').val(data.name);            
            $(document).find('#company_id').val(data.id);
            $(document).find('#details_gstno').val(data.gstin);
				$(document).find('#details_email').val(data.email);
				$("#companyDetails").css("display", "block");
			},
			error: function (jqXhr, textStatus, errorMessage) {
            console.log('Error' + errorMessage);
    }
		});
});
 
$('#Company_select').on('select2:select', function (e) {
   var company = e.params.data.id;
   
   $.ajax(url_getbillingaddressfromcompany,{
      type : 'get',
      data :{'company' : company} ,
      success : function(data){
 
         
         var typeid=data.state_id;
         showhide(typeid); 
         $(document).find('#billing_id').val(data.address_id);
         $(document).find('#quotation_billadd1').val(data.address_line1);
         $(document).find('#quotation_billadd2').val(data.address_line2);
         $(document).find('#quotation_billadd3').val(data.address_line3);
         $(document).find('#quotation_billcity').val(data.name);
         $(document).find('#quotation_billpincode').val(data.pincode);
         $(document).find('#quotation_billstate').val(data.state_title);
         $(document).find('#quotation_billcountry').val(data.country);
         $(document).find('#quotation_billmobile').val(data.mobile);
         
         $("#companybilling_address").css("display", "block");
      },
      error: function (jqXhr, textStatus, errorMessage) {
         console.log('Error' + errorMessage);
 }
   });
});



$('#Company_select').on('select2:select', function (e) {
   var company = e.params.data.id;
   var option="";
   
   $.ajax(url_getshippingaddressfromcompany,{
      type : 'get',
      data :{'company' : company} ,
      success : function(data){
     
         var i =0;
         for(result in data)
           option= option+ '<option  value='+data[result].address_id+' data-value='+result+'>'+data[result].alias_name+':'+data[result].address_line1+','+data[result].address_line2+'</option>';
        
         $("#Shiping_select").append(option);

            $('.Shiping_select').on('change', function () {
            
             
            var selid=$(this).find(':selected').data("value");
            
            $(document).find('#shipping_id').val(data[selid].address_id);
            
            
            $(document).find('#quotation_shipadd1').val(data[selid].address_line1);
            $(document).find('#quotation_shipadd2').val(data[selid].address_line2);
            $(document).find('#quotation_shipadd3').val(data[selid].address_line3);
            $(document).find('#quotation_shipcity').val(data[selid].name);
            $(document).find('#quotation_shippincode').val(data[selid].pincode);
            $(document).find('#quotation_shipstate').val(data[selid].state_title);
            $(document).find('#quotation_shipcountry').val(data[selid].country);
            $(document).find('#quotation_shipmobile').val(data[selid].mobile);
           });

       
         $("#companyshipping_address").css("display", "block");
         
      },
      error: function (jqXhr, textStatus, errorMessage) {
         console.log('Error' + errorMessage);
 }
   });
});



$('#Company_select').select2({
   ajax: {
          url: url_getcompany,
          type: "get",
          dataType: 'json',
          delay: 250,
          data: function (params) {
             return {
                searchTerm: params.term // search term
             };
          },
          processResults: function (response) {
             return {
                results: response
             };		   
         },
          cache: true
        }
   });


   
var now = new Date();

var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);

var today = now.getFullYear()+"-"+(month)+"-"+(day);

$('#issues_date').val(today);

});


function showhide(typeid)
{
   if(typeid==12)
   {
      $('#selecti').hide();
      $('#igst').hide();
      $('#finalthi').hide();
      $('#preview_igst').hide();
      $('#selectcs').show();
      $('#preview_csgst').show();
      $('#cs_gst').show();

      $('#gstbl1').text("GST(2.5% + 2.5%)");
      $('#gstbl2').text("GST(6% + 6%)");
      $('#gstbl3').text("GST(9% + 9%)");
      $('#gstbl4').text("GST(14% + 14%)");

      $('#gstblin1').attr("class","listcsstotal form-control gst25");
      $('#gstblin2').attr("class","listcsstotal form-control gst6");
      $('#gstblin3').attr("class","listcsstotal form-control gst9");
      $('#gstblin4').attr("class","listcsstotal form-control gst14");

   }
   else
   {
      $('#selectcs').hide();
      $('#cs_gst').hide();
      $('#finalthcs').hide();
      $('#preview_csgst').hide();
      $('#preview_igst').show();
      $('#selecti').show();
      $('#igst').show();

      $('#gstbl1').text("GST(5%)");
      $('#gstbl2').text("GST(12%)");
      $('#gstbl3').text("GST(18%)");
      $('#gstbl4').text("GST(28%)");

      $('#gstblin1').attr("class","listcsstotal form-control gst5");
      $('#gstblin2').attr("class","listcsstotal form-control gst12");
      $('#gstblin3').attr("class","listcsstotal form-control gst18");
      $('#gstblin4').attr("class","listcsstotal form-control gst28");

   }
}