// $(document).on("click", "#submit_location", function (e) {
//     $('#store_location').validate({
//         rules: {
//             location_name: {
//                 required: true,
//             }
//         },
//         messages: {
//             location_name: {
//                 required: "Please enter the state.",
//             }
//         },
//         errorClass: 'error-message',
//         submitHandler: function (form) {
//             e.preventDefault();
//             let base_url = url_path;
//             var formData = new FormData(form);
//             $.ajaxSetup({
//                 headers: {
//                     "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
//                 },
//             });
//             $.ajax({
//                 url: base_url + '/insert_location',
//                 type: 'POST',
//                 data: formData,
//                 processData: false,
//                 contentType: false,
//                 success: function (response) {
//                     if (response.status == 200) {
//                         toastr.success('Location added successfully');
//                         setTimeout(function () {
//                             window.location.href = base_url + "/manage_location";
//                         }, 1000);
//                     } else {
//                         toastr.error("Something went wrong");
//                     }
//                 },
//             });
//         }
//     });
// });
$(document).on("click", "#submit_location", function (e) {
    $('#store_location').validate({
        rules: {
            location_name: {
                required: true,
            }
        },
        messages: {
            location_name: {
                required: "Please enter the state.",
            }
        },
        errorClass: 'error-message',
        submitHandler: function (form) {
            e.preventDefault();
            let base_url = url_path;
            var formData = new FormData(form);
            $.ajaxSetup({
                headers: {
                    "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
                },
            });
            $.ajax({
                url: base_url + '/insert_location',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function (response) {
                    if (response.status == 200) {
                        toastr.success('Location added successfully');
                        setTimeout(function () {
                            window.location.href = base_url + "/manage_location";
                        }, 1000);
                    } else if (response.status == 422 && response.error === 'Location name already exists') {
                        toastr.error('Location name already exists');
                    } else {
                        toastr.error("Something went wrong");
                    }
                },
                error: function (xhr, status, error) {
                    // Handle the case where the server returns an error
                    if (xhr.status == 422 && xhr.responseJSON.error === 'Location name already exists') {
                        toastr.error('Location name already exists');
                    } else {
                        toastr.error("An error occurred");
                    }
                }
            });
        }
    });
});
