jQuery(document).ready(function($) {
	//WPCSR ADD SHIPMENT RATE
	$( "#add-shipment-rate-form" ).on('submit',function(e) {
		e.preventDefault();
		var value 				= '';
		var shipping_rate_data 	= [];
		var package_type 		= [];
		$(".wpcsr_detail").each(function(){
			var name = $(this).attr('name');
			if($(this).attr('type') != 'radio' && $(this).attr('type') != 'checkbox'){
				value = $(this).val();
				shipping_rate_data.push({'name':name, 'value':value});
			}
		});
		$('input[name=rate_type]').each(function () {
			var name = $(this).attr('name');
			if (this.checked) {
				value = $(this).val();
				shipping_rate_data.push({'name':name, 'value':value});
			}
		});
		$('input[name=piece_type]').each(function () {
			if (this.checked) {
				var package_value = $(this).val();
				package_type.push(package_value);
			}
		});
		if(package_type != ''){
			var packageString = package_type.join();
			shipping_rate_data.push({'name':'piece_type', 'value':packageString});
		}
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action 		: 'ajax_save_wpcsr',
				rate_data	: shipping_rate_data,
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location.reload();
			}
		});
		// End of Ajax 
	});
	// Filter if the value entered is correct on shipping rate
    $('#wpcsr_min_weight, #wpcsr_max_weight').on('change', function(){
        var min_weight      = $('#wpcsr_min_weight');
        var max_weight      = $('#wpcsr_max_weight');
		wpcsr_validate_weight( min_weight, max_weight );
	});
	$('body').on( 'change', '#wpcsr_update_min_weight, #wpcsr_update_max_weight', function(){
		var min_weight      = $('body').find('#wpcsr_update_min_weight');
		var max_weight      = $('body').find('#wpcsr_update_max_weight');
		wpcsr_validate_weight( min_weight, max_weight );
	});
	
	function wpcsr_validate_weight( min_weight, max_weight ){
		var minWeight = parseFloat(min_weight.val());
		var maxWeight = parseFloat(max_weight.val());
		if( minWeight != '' && maxWeight != ''){
            if( maxWeight < minWeight ){
				alert('Invalid weight value');
				min_weight.val('');
				max_weight.val('');
            }
        }
	}
	//WPCSR UPDATE SHIPMENT RATE
	$('#update-shipment-rate').on('submit', function(e){
		e.preventDefault();
		var id 			= $('#update-shipment-rate #rateID').val();
		var formData 	= $( this ).serializeArray();		
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action		: 'wpcsr_update_rate',
				id 			: id,
				formData 	: formData
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location.reload();
			}
		});
		
	});
	//WPCSR ADD LOCATION
	$( "form.add-location-form" ).on('submit', function(e) {
		e.preventDefault();
		var location 	= $('#wpcsr_location').val();
		var slug 		= $('#wpcsr_location_slug').val();
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'ajax_save_wpcsr_location',
				location : location,
				slug : slug,
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				if( response ){
                    $('.wpcsr-location-table table.location-table tbody').prepend('<tr class="tbl-data-'+response+'">'+
                                        '<td class="wpcsr-td-id location"><input type="checkbox" class="bulk_id" name="rate_bulk_action[]" value="'+response+'"></td>'+
										'<td class="wpcsr-td-id location">'+response+'</td>'+
										'<td class="wpcsr-td-name location">'+location+'</td>'+
										'<td class="wpcsr-td-slug location">'+slug+'</td>'+
                                        '<td>'+
                                            '<span class="dashicons dashicons-edit wpcsr-edit edit-location" data-section="location" data-id="'+response+'"></span>'+
                                            '<span class="dashicons dashicons-trash wpcsr-delete" data-section="location" data-id="'+response+'"></span>'+
										'</td>'+
									'</tr>');
				}else{
					alert('Something went wrong please reload the page ang try again.');
				}
				$('form.add-location-form').trigger("reset");
				$('body .wpc-loading').remove();
			}
		});
	});
	//WPCSR UPDATE LOCATION
	$( "#update-location" ).on('submit',function( e ) {
		e.preventDefault();
		var id = $('#wpcsr_location_id').val();
		var location = $('#update_location').val();
		var slug = $('#update_location_slug').val();
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'ajax_update_wpcsr_location',
				id:id,
				location : location,
				slug : slug
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location.reload();
			}
		});
	});
	//WPCSR ADD ZONE
	$( "#add-zone-form" ).on('submit',function( e ) {
		e.preventDefault();
		var zone = $('#wpcsr_zone').val();
		var from = $('#wpcsr_zone_from').val();
		var to = $('#wpcsr_zone_location').val();
		var zone_location = to.join(", ");
		var rate = $('#wpcsr_zone_rate').val();
		
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'ajax_save_wpcsr_zone',
				zone : zone,
				from : from,
				zone_location : zone_location,
				rate : rate,
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location.reload();
			}
		});
	});
	//WPCSR UPDATE ZONE
	$( "#update-zone" ).on('submit',function( e ) {
		e.preventDefault();
		var id = $('#wpcsr_zone_id').val();
		var zone = $('#update_zone').val();
		var from = $('#update_zone_from').val();
		var to = $('#update_zone_location').val();
		var zone_location = to.join(", ");
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'ajax_update_wpcsr_zone',
				id:id,
				zone : zone,
				from : from,
				zone_location : zone_location,
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location.reload();
			}
		});
	});
	//WPCSR UPDATE USER GROUP
	$( "#user-group-edit" ).on('submit',function( e ) {
		e.preventDefault();
		var id = $('#wpcsr_ug_id_edit').val();
		var label = $('#wpcsr_ug_label_edit').val();
		var description = $('#wpcsr_ug_desc_edit').val();
		var users = $('#wpcsr_ug_users_edit').val();
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'ajax_update_user_group',
				id : id,
				label : label,
				description : description,
				users : users,
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location.reload();
			}
		});
	});
	//DELETE DATA
	$("body").on('click','.wpcsr-delete',function(e){
		e.preventDefault();
		var id = $(this).attr('data-id');
		var section = $(this).attr('data-section');
		var confirmDelete = confirm('Are you sure you want to delete this data?');
		if( confirmDelete ){
			$.ajax({
				url : wpcsr_add.ajax_url,
				type : 'post',
				data : {
					action : 'ajax_delete_wpcsr',
					id : id,
					section : section,
				},
				beforeSend:function(){
					$('body').append('<div class="wpc-loading">Loading...</div>');
				},
				success : function( response ) {
					window.location.reload();
				}
			});
		}
	});
	// BULK ACTION 
	$("body").on('click','.wpcsrate-bulk-delete',function(e){
		e.preventDefault();
		var id = [];
		var section = $(this).attr('data-section');
		var confirmDelete = confirm('Are you sure you want to delete selected data?');
		$.each( $( '.bulk_id:checked' ), function(){
			id.push( $(this).val() );
		} );
		if( confirmDelete ){
			$.ajax({
				url 	: wpcsr_add.ajax_url,
				type	: 'post',
				data 	: {
						action 	: 'ajax_rate_bulk',
						id 		: id,
						section : section
				},
				beforeSend:function(){
					$('body').append('<div class="wpc-loading">Loading...</div>');
				},
				success:function(data){
					window.location.reload();
				}
			});
		}
	});
	//  Check and Uncheck ALL		  
 	$('#checkall:checkbox').click( function(){
 		if(  $(this).prop("checked") == true){	         		
         	$(".tbl-data td :checkbox").attr("checked", true);
     	}else {
     		$(".tbl-data td :checkbox").attr("checked", false);         		
     	}
 	});       	
	//WPCSR UPDATE LOCATION MODAL
	$('body').on('click', '.edit-location', function(e){
		e.preventDefault();
		$('#location-edit.modal').css("display", "block");
		var id = $(this).attr('data-id');
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'get_location_data',
				id:id
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				$('#location-edit.modal .modal-body form').html( response );
				$('body .wpc-loading').remove();
			}
		});
	});
	//WPCSR UPDATE ZONE MODAL
	$('body').on('click', '.edit-zone', function(e){
		e.preventDefault();
		$('#zone-edit.modal').css("display","block");
		var id = $(this).attr('data-id');
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'get_zone_data',
				id:id
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				$('#zone-edit.modal .modal-body form').html( response );
				$('#update_zone_from, #update_zone_location').select2({
					placeholder: '',
					allowClear: true
				});
				$('body .wpc-loading').remove();
			}
		});
	});
	//WPCSR UPDATE USER GROUP MODAL
	$('body').on('click', '.edit-user-group', function(e){
		e.preventDefault();
		$('#user-group-edit.modal').css("display","block");
		var id = $(this).attr('data-id');
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'get_user_group_data',
				id:id
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				$('#user-group-edit .modal-body form').html( response );
				$('#wpcsr_ug_users_edit').select2({
					placeholder: '',
					allowClear: true
				});
				$('body .wpc-loading').remove();
			}
		});
	});
	//WPCSR UPDATE SHIPMENT RATE MODAL
	$('body').on('click', '.edit-rate', function(e){
		e.preventDefault();
		//modal.style.display = "block";
		$('#shipment-rate-edit').css("display","block");
		var id = $(this).attr('data-id');
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'get_rate_data',
				id:id
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				$('#shipment-rate-edit.modal .modal-body form').html( response );
				$('body .wpc-loading').remove();
				$('#wpcsr_update_user_group').select2({
					placeholder: '',
					allowClear: true
				});
			}
		});
	});
	//WPCSR SHOW UPDATE DELIVERY MODAL
	$('body').on('click', '.edit-delivery', function(e){
		e.preventDefault();
		$('#delivery-edit.modal').css("display","block");
		var id = $(this).attr('data-id');
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'get_delivery_data',
				id:id
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				$('#delivery-edit.modal .modal-body form').html( response );
				$('body .wpc-loading').remove();
			}
		});
	});
	//WPCSR UPDATE SHIPMENT DELIVERY
	$('#update-delivery').on('submit', function(e){
		e.preventDefault();
		var id 			= $('#update-delivery #wpcsr_delivery_id').val();
		var title 		= $('#update-delivery #title').val();
		var description = $('#update-delivery #description').val();
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'wpcsr_update_delivery',
				id:id,
				title : title,
				description : description
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location.reload();
			}
		});
	});
	// Update Pickup rate
	$('body').on('click', '.edit-pickup-rate', function(e){
		e.preventDefault();
		$('#wpcsr-pickup-rate-edit').css("display","block");
		var id = $(this).attr('data-id');
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'get_pickuprate_data',
				id:id
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				$('#wpcsr-pickup-rate-edit.modal .modal-body form').html( response );
				$('body .wpc-loading').remove();
			}
		});
	});
	$('#update-pickup-rate').on('submit', function(e){
		e.preventDefault();
		var pickupID 	= $('#update-pickup-rate #pickupID').val();
		var pickupName 	= $('#update-pickup-rate #name').val();
		var warehouse 	= $('#update-pickup-rate #warehouse').val();
		var pickupZone 	= $('#update-pickup-rate #pickup_zone').val();
		var minWeight 	= $('#update-pickup-rate #min_weight').val();
		var maxWeight 	= $('#update-pickup-rate #max_weight').val();
		var price 		= $('#update-pickup-rate #price').val();
		// 
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'wpcsr_update_pickuprate',
				pickupID:pickupID,
				pickupName : pickupName,
				warehouse : warehouse,
				pickupZone : pickupZone,
				minWeight : minWeight,
				maxWeight : maxWeight,
				price : price
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location.reload();
			}
		});
	});
	//WPCSR EXPORT LOCATION
	$('#wpcsr-export-location').on('click', function( e ){
		e.preventDefault();
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'export_location',
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location=response;
				$('body .wpc-loading').remove();
			}
		});
	});
	//WPCSR EXPORT ZONE
	$('#wpcsr-export-zone').on('click', function( e ){
		e.preventDefault();
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'export_zone',
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location=response;
				$('body .wpc-loading').remove();
			}
		});
	});
	//WPCSR EXPORT ZONE
	$('#wpcsr-export-rate').on('click', function( e ){
		e.preventDefault();
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'export_rate',
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location=response;
				$('body .wpc-loading').remove();
			}
		});
	});
	// ADD DELIVERY
	$( "#add-delivery-form" ).on('submit',function(e) {
		e.preventDefault();
		var title 			= $('#wpcsr_delivery').val();
		var description 	= $('#wpcsr_delivery_description').val();
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'save_wpcsr_delivery',
				title : title,
				description : description,
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location.reload();
			}
		});
	});
	//WPCSR EXPORT DELIVERY
	$('#wpcsr-export-delivery').on('click', function( e ){
		e.preventDefault();
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'export_delivery',
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location=response;
				$('body .wpc-loading').remove();
			}
		});
	});
	// Add pickup rates
	$( "#pickup-rate-form" ).on('submit',function( e ) {
		e.preventDefault();
		var pickupLabel = $('#name').val();
		var warehouse = $('#warehouse').val();
		var location = $('#pickup_zone').val();
		var minWeight = $('#min_weight').val();
		var maxWeight = $('#max_weight').val();
		var price = $('#price').val();
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'save_pickup_rate',
				pickupLabel : pickupLabel,
				warehouse : warehouse,
				location : location,
				minWeight : minWeight,
				maxWeight : maxWeight,
				price : price,
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location.reload();
			}
		});
	});
	
	// Add user group
	$( "#add-user-group-form" ).on('submit',function( e ) {
		e.preventDefault();
		var label = $('#wpcsr_ug_label').val();
		var description = $('#wpcsr_ug_desc').val();
		var wpcsr_ug_users = $('#wpcsr_ug_users').val();
		$.ajax({
			url : wpcsr_add.ajax_url,
			type : 'post',
			data : {
				action : 'save_user_group',
				label : label,
				description : description,
				wpcsr_ug_users : wpcsr_ug_users,
			},
			beforeSend:function(){
				$('body').append('<div class="wpc-loading">Loading...</div>');
			},
			success : function( response ) {
				window.location.reload();
			}
		});
	});
	
	$('#wpcsr_zone_from, #wpcsr_zone_location, #wpcsr_ug_users, #wpcsr_user_group').select2({
		placeholder: '',
		allowClear: true
	});
});