$(document).ready(function(){
  $('.company_create').click(function(){
      generateCOMPANY_CODE();

    $("#add_new_company").modal('show');
  });

  $('.companyCreate_close').click(function(){
    $('#company_create')[0].reset();  
    $('#CreateCity').val(null).trigger('change');
    $('#CreateShiopToCity').val(null).trigger('change');
  });
 
  $("#company_create_model").css("width","800px");

  $(document).on('click', 'div.addressblockcreate .fa-chevron-down', function () {
    // $(document).find(".addressblock .fa-chevron-down").toggleClass("rtoate180");
    $(document).find(".addressblockdetails").stop().slideToggle(500);

  });
  
  $(".submit-company").click(function(e){
    
      e.preventDefault(e);

      var c_name=$('.cname').val();
      var c_gstin=$('#cgst').val();
      var c_email=$('#cemail').val();
       if(c_email=="")
      {
        c_email="null";
      }
      var company_code=$('#generated_companycode').val();
      
      var generated_by=$('#generated_by').val();
      var alter_email=$('#alternate_cemail').val();
       
      if(alter_email=="")
      {
        alter_email="null";
      }
 
      var company_billaddline1=$('.billaddl1').val();
      var company_billaddline2=$('.billaddl2').val();
      var company_billaddline3=$('.billaddl3').val();
      
      if(company_billaddline3=="")
      {
        company_billaddline3="null";
      }
      var company_billpincode=$('.billpincode').val();

      var company_billcountry=$('#country_select').val();
	  var company_billcity=$('#CreateCity').val();
	  
      var company_billstate=$('#States').data("state-id");
	  if(company_billcountry != 'India'){
		  company_billcity =$('.billCityManual').val() ;
		  company_billstate = $('#State').val() ;
	  }
	  console.log(company_billstate);
      var company_billmobile=$('.billmobile').val();
      var company_shipaddressname=[];
      var company_receivername=[];
      var company_shipaddline1=[];
      var company_shipaddline2=[];
      var company_shipaddline3=[];
      var company_shipcity=[];
      var company_shippincode=[];
      var company_shipstate=[];
      var company_shipcountry=[];
      var company_shipmobile=[];
      var alter_mobile=[];
      var ship_company_name=[];

      $('.address_name').each(function(){
        company_shipaddressname.push($(this).val());
      });

      $('.receiver_name').each(function(){
        company_receivername.push($(this).val());
      });

      $('.shipaddl1').each(function(){
        company_shipaddline1.push($(this).val());
      });

      $('.shipaddl2').each(function(){
        company_shipaddline2.push($(this).val());
      });

      $('.shipaddl3').each(function(){
        
        if($(this).val()==''){
          company_shipaddline3.push("null");
        }
        else
        {
          company_shipaddline3.push($(this).val());
        }

      });

      $('.shipcity').each(function(){
		company_shipcity.push($(this).val());
			
      });

		$('.shipCityManual').each(function(){
			company_shipcity.push($('.shipCityManual').val());
		 
      });
	
     /* $('.editadd_shipstate').each(function(){
        company_shipstate.push($(this).data('state-id'));
		console.log('this state is called ' + company_ships);
      });*/
	  
	  $('.shipstate').each(function(){
		  if($(this).closest('.company_shipcountry').val() != 'India'){
			company_shipstate.push($(this).val());
		  }else{
        company_shipstate.push($(this).data('state-id'));
		  }
      });
	
      $('#country_select_shipping').each(function(){
		  console.log($(this).val());
      company_shipcountry.push($(this).val());
      });

      $('.shippincode').each(function(){
      company_shippincode.push($(this).val());
      });

      $('.shipmobile').each(function(){
        company_shipmobile.push($(this).val());
      });

      $('.shipTocompanyname').each(function(){

        if($(this).val()==''){
          ship_company_name.push("null");
        }
        else
        {
          ship_company_name.push($(this).val());
        }

        
      });

      $('.alternate_mobile').each(function(){
        
        if($(this).val()==''){
          alter_mobile.push("0");
        }
        else
        {
          alter_mobile.push($(this).val());
        }

      });
    
    //   if(c_name==""||c_gstin==""||c_email==""||company_billaddline1==""||company_billaddline2==""||company_billcity==""||company_billstate==""||company_billcountry==""||company_billpincode==""||company_billmobile==""||company_shipaddline1==""||company_shipaddline2==""||company_shipcity==""||company_shipstate==""||company_shipcountry==""||company_shippincode==""||company_shipmobile==""||company_receivername=="")
      if(c_name==""||c_gstin==""||company_billaddline1==""||company_billaddline2==""||company_billcity==""||company_billstate==""||company_billcountry==""||company_billpincode==""||company_billmobile==""||company_shipaddline1==""||company_shipaddline2==""||company_shipcity==""||company_shipstate==""||company_shipcountry==""||company_shippincode==""||company_shipmobile==""||company_receivername=="")
      {
         
        alert("Please Enter All Data Properlly!!!!");

      }

      else if(company_billaddline1==company_shipaddline1&&company_billaddline2==company_shipaddline2&&company_billaddline3==company_shipaddline3&&company_billcity==company_shipcity&&company_billpincode==company_shippincode&&company_billmobile==company_shipmobile)
      {
        
        if($("#sameAsShippingAddress").prop('checked') == false){
        
          alert("If You want enter same address as Billing You have to select checkbox");

        }
        
      }
      
      else
      {
   
	  
	   
        $.ajaxSetup({

            headers: {

              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

            }

        });  

        $.ajax({
    
          type: "POST",

          url: url_createcompany,

          type:"POST",

          data:{

            company_name:c_name,

            company_cgstin:c_gstin,

            company_cemail:c_email,

            company_code:company_code,

            generated_by:generated_by,

            alter_email:alter_email,

            company_billaddline1:company_billaddline1,

            company_billaddline2:company_billaddline2,

            company_billaddline3:company_billaddline3,

            company_billcity:company_billcity,

            company_billpincode:company_billpincode,

            company_billstate:company_billstate,

            company_billcountry:company_billcountry,

            company_billmobile:company_billmobile,

            company_shipaddressname:company_shipaddressname,

            company_receivername:company_receivername,

            company_shipaddline1:company_shipaddline1,

            company_shipaddline2:company_shipaddline2,

            company_shipaddline3:company_shipaddline3,

            company_shipcity:company_shipcity,

            company_shippincode:company_shippincode,

            company_shipstate:company_shipstate,

            company_shipcountry:company_shipcountry,

            company_shipmobile:company_shipmobile,

            alter_mobile:alter_mobile,

            ship_company_name:ship_company_name,

          },
          success:function(response) {
          
            console.log(response);
          
            $("#add_new_company").modal('hide');
           
              $('#company_create')[0].reset();  
              $('#CreateCity').val(null).trigger('change');
              $('#CreateShiopToCity').val(null).trigger('change');
           
          
            var success="";

              success=success+
            
              '<p style="text-align:center;"><b>Company Created Successfully!!!</b></p>';
             
              $('#company_success').append(success);
              $('#company_success').removeClass("hide");

              setTimeout(function(){
                location.reload();
              }, 200);
          }

        });
      
	  }
  });

});

function generateCOMPANY_CODE()
{
  var final="";
  
      $.ajaxSetup({

        headers: {
      
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      
        }
      
      });  

      $.ajax({
      
        url:url_generatenumber,
      
        type:"POST",
      
        data:{
            "company_code":"company_code"
        },
         
        success:function(data)
        {
          if(data.company_code==null)
          {
            var final=('TTB-C-000001');
            $(document).find('#generated_companycode').val(final);
          }
          else
          {
            var lastno=data.company_code;  
            var number=lastno.replace('TTB-C-','');
              number++;
            var getfinalnumber=pad(number,"6");
            final=('TTB-C-'+getfinalnumber);
            $(document).find('#generated_companycode').val(final);

          }
          
          

        }

      });
      return final;
      
}

function pad (str, max) {
  str = str.toString();
  return str.length < max ? pad("0" + str, max) : str;
} 