$(document).ready(function(){
$('#new_city').click(function(){
	   console.log("city called");
    $("#add_new_city").modal('show');
  });
  $('#new_city_edit').click(function(){
	   console.log("city called edit");
    $("#add_new_city").modal('show');
  });
 $('#CreateCity').select2({
 ajax: {
        url: url_getcity,
        type: "get",
        dataType: 'json',
        delay: 100,
        data: function (params) {
           return {
              searchTerm: params.term // search term
           };
        },
        processResults: function (response) {
           return {
              results: response
           };		   
		 },
        cache: true
      }
 });
  
 $('#CreateCity').on('select2:select', function (e) {
		var city = e.params.data.id;
		console.log("her2e");
		$.ajax(url_getstatefromcity,{
			type : 'get',
			data :{'city' : city} ,
			success : function(data){
				
				console.log(data);
				$(document).find('#State').val(data.state.state_title);
				$(document).find('#State').attr("data-state-id",data.state.state_id);
                $(document).find('#States').attr("data-state-id",data.state.state_id);
				$(document).find('#Country').val("India");
				
			},
			error: function (jqXhr, textStatus, errorMessage) {
            console.log('Error' + errorMessage);
    }
		});
});


 
$('#CreateShiopToCity').select2({
   ajax: {
          url: url_getcity,
          type: "get",
          dataType: 'json',
          delay: 100,
          data: function (params) {
             return {
                searchTerm: params.term // search term
             };
          },
          processResults: function (response) {
             return {
                results: response
             };		   
         },
          cache: true
        }
   });
   
   $('#CreateShiopToCity').on('select2:select', function (e) {
        var city = e.params.data.id;
        $.ajax(url_getstatefromcity,{
           type : 'get',
           data :{'city' : city} ,
           success : function(data){
              
              
              $(document).find('#ShipToState').val(data.state.state_title);
              $(document).find('#ShipToState').attr("data-state-id",data.state.state_id);
              
              $(document).find('#ShipToCountry').val("India");
              
           },
           error: function (jqXhr, textStatus, errorMessage) {
              console.log('Error' + errorMessage);
      }
        });
  });
  
  
  
  
	$('#CreateState').select2({
	 ajax: {
			url: url_getState,
			type: "get",
			dataType: 'json',
			delay: 100,
			data: function (params) {
			   return {
				  searchTerm: params.term // search term
			   };
			},
			processResults: function (response) {
			   return {
				  results: response
			   };		   
			 },
			cache: true
		  }
	 });
	 
	  $(".submit-new-city").click(function(e){
      e.preventDefault(e);
		if(!$('.city').val()){
			alert('Add city');
	  }
	  else if($('#CreateState').val() == 0){
			alert('Select State');
	  }else{
		  $.ajax({
    
          type: "POST",

          url: url_addCity,

          type:"POST",

          data:{
			  city : $('.city').val(),
			  state_id :$('#CreateState').val()
		  },
		  success : function(response) {
			  console.log(response);
			if(response.status){
				$("#add_new_city").modal('hide');
				
			}else{
				conmsole.log(response.message)
			}
			
		  }
		  });
	  }
	  $(document).find('#add_new_company').css({
					overflow: 'auto',
					overflowY: 'scroll'
				});
	  });
	$(".cityCreate_close").on( "click", function(e) {
	$(document).find('#add_new_company').css({
					overflow: 'auto',
					overflowY: 'scroll'
				});
	  });
	  
	 $('#country_select').select2();
     $('#country_select').on('select2:select', function (e) {
		if($(this).val() != 'India'){
			$('.billCityManual').removeClass('hide');
			$('#CreateCity').next(".select2-container").hide();
			$('.newCityAddon').addClass('hide');
			$('.billstate').prop('readonly', false);
			
		}else{
			$('.billCityManual').addClass('hide');
			$('.billCityManual').val('');
			$('#CreateCity').next(".select2-container").show();
			$('.newCityAddon').removeClass('hide');
			$('.billstate').prop('readonly', true);
			$('.billstate').data('id','96');
		}

	 });
	 
	 $('#country_select_shipping').select2();
     $('#country_select_shipping').on('select2:select', function (e) {
		 
		if($(this).val() != 'India'){
			console.log('this step is called');
			$('.shipCityManual').removeClass('hide');
			$('#CreateShiopToCity').next(".select2-container").hide();
			$('.shipstate').prop('readonly', false);
			$('.shipCityManual').prop('readonly', false);
			$('.shipstate').attr("data-state-id",'96');	
			$('.shipstate').val("");
		}else{
			$('.shipCityManual').addClass('hide');
			$('.shipCityManual').val('');
			$('#CreateShiopToCity').next(".select2-container").show();
			$('.shipstate').prop('readonly', true);

		}

	 });
	 
});