// add faq start here
$(document).on("click", "#submit_faq", function (e) {

    $('#store_faq').validate({
        rules: {
            question: {
                required: true,
                maxlength: 255
            },
            answer: {
                required: true,
            },
            faq_categ: {
                required: true
            },
        },
        messages: {
            question: {
                required: "Please enter the question.",
                maxlength: "The question must be at least 255 characters long."
            },
            answer: {
                required: "Please enter the answer.",
            },
            faq_categ: {
                required: "Please select category."
            },
        },
        errorClass: 'error-message',
        submitHandler: function (form) {
            let base_url = url_path;
            var formData = new FormData(form);
            e.preventDefault(); // Prevent default form submission behavior

            $.ajaxSetup({
                headers: {
                    "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
                },
            });
            $.ajax({
                url: base_url + '/store_faq',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function (response) {
                    if (response.status == 200) {
                        toastr.success('Faq added successfully');
                        setTimeout(function () {
                            window.location.href = base_url + "/faq";
                        }, 1000);
                    } else {
                        toastr.error("Something went wrong");
                    }
                },
            });
        }
    });
});


// add faq end here

//delete old 
// $(document).ready(function () {
//     $('.delete-faq-btn').on('click', function (e) {
//         e.preventDefault();

//         var faqId = $(this).data('faq-id');
//         if (confirm('Are you sure you want to delete this FAQ?')) {
//             $.ajax({
//                 url: url_path + '/delete_faq/' + faqId,
//                 type: 'DELETE',
//                 headers: {
//                     'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
//                 },
//                 success: function (response) {
//                     if (response.status == 200) {
//                         toastr.success('Faq deleted successfully');
//                         setTimeout(function () {
//                             window.location.href = url_path + "/faq";
//                         }, 1000);
//                     } else {
//                         toastr.error("Something went wrong");
//                     }
//                 },
//                 error: function () {
//                     toastr.error("Error deleting FAQ");
//                 }
//             });
//         }
//     });
// });

//new delete with modal
$(document).on("click", ".delete_about", function () {
    var delete_id = $(this).data('faq-id'); // Corrected this line
    $('#delete_about_id').val(delete_id);
});

$(document).on("click", ".delete_about_button", function (e) {
    e.preventDefault();
    let base_url = url_path;
    var faqId = $('#delete_about_id').val(); // Ensure you're getting the correct ID

    $.ajaxSetup({
        headers: {
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
        },
    });
    $.ajax({
        type: "DELETE",
        url: base_url + "/delete_faq/" + faqId,
        success: function (response) {
            if(response.success) {
                toastr.success("FAQ successfully deleted");
                window.location.href = base_url + "/faq";
            } else {
                toastr.error(response.message);
            }
        },
        error: function (response) {
            toastr.error("An error occurred while deleting the FAQ");
        }
    });
});
//end delete



// change status
$(".switch-btn").on("change", ".active_newsletter", function () {
    let base_url = url_path;
    var status = $(this).prop("checked") == true ? 1 : 0;
    var program_id = $(this).data("id");
    $.ajax({
        type: "GET",
        dataType: "json",
        url: base_url + '/change_faq_Status',
        data: {
            status: status,
            program_id: program_id,
        },
        success: function (data) {
            if (status == 1) {
                toastr.options = {
                    "timeOut": 500
                }
                toastr.success("Status Activate successfully. !!");
            } else {
                toastr.error("Status Deactivate successfully. !!");
            }
        },
    });
});

