const swalWithBootstrapButtons = Swal.mixin({
	customClass: {
	    confirmButton: 'apbgf-btn apbgf-btn-success',
	    cancelButton: 'apbgf-btn apbgf-btn-danger'
	},
	buttonsStyling: true
});

jQuery( document ).ready(function($) {
	$('.apbgf-app-cancel-form').on('submit', function(event) {
		event.preventDefault();
		var app_id = $(this).find('input[name="apbgf-app-id"]').val();
		var status = $(this).find('input[name="apbgf-status"]').val();
		var reason = '';		
		swalWithBootstrapButtons.fire({
			title: 'Are you sure?',
			text: "You won't be able to revert this!",
		  	icon: 'warning',
		  	input: 'textarea',
		  	showCancelButton: true,
		  	confirmButtonText: apbgf_frontend.confirm,
		  	cancelButtonText: apbgf_frontend.cancel,
		  	showLoaderOnConfirm: true,
		  	// reverseButtons: true
		}).then((result) => {
			if ( $.trim(result.value) != '' ) {
				reason = result.value;
			}

		  	if (result.isConfirmed) {
		  		$.ajax({
					url: apbgf_frontend.ajaxurl,
					type: 'POST',
					data: `apbgf_status=${status}&post_id=${app_id}&apbgf_reason=${reason}&action=apbgf_cancel_appointment_by_customer&nonce=${apbgf_frontend.nonce}`,
					success: function (response) {
						var response = $.parseJSON(response);
						if ( 'success' == response.status ) {
							swalWithBootstrapButtons.fire(
					      		apbgf_frontend.title,
					      		response.message,
					      		response.status
					    	).then((result) => {
					    		location.reload();
					    	});
						} else {
							swalWithBootstrapButtons.fire(
					      		apbgf_frontend.title,
					      		response.message,
					      		response.status
					    	);
						}
					}
				});
		  	}
		});
	});
});