$('#update_about_us').on("click", function (e) {
    
    $.validator.addMethod("quillNotEmpty", function(value, element) {
        var quill = new Quill('#about-quill-edit');
        return quill.getText().trim().length > 0;
    }, "Please enter description ");

    $('#update_about').validate({
        ignore: [],
        debug: false,
        rules: {
            about_title: {
                required: true
            },
            about_des: {
                required: true,
                quillNotEmpty: true
            },
            category: {
                required: true
            },
        },
        messages: {
            about_title: {
                required: "Please Enter Title"
            },
            about_des: {
                required: "Please Enter Description"
            },
            category: {
                required: "Please Select Category"
            },
        },
        errorClass: 'error-message',
        submitHandler: function(form) {
            var formData = new FormData(form);
            let base_url = url_path;

            $('#update_about_us').text('Please wait...');
            $('#update_about_us').attr('disabled', true);

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                url: base_url + '/about_us_update',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(result) {

                    console.log("result",result);
                    if (result.status_code == 200) {
                        toastr.success('About Us Data Updated Successfully');
                        setTimeout(function() {
                            window.location.href = base_url + "/manage-about-us";
                        }, 2000);
                    } else {
                        toastr.error('Something Went Wrong');
                        setTimeout(function() {
                            window.location.href = base_url + "/manage-about-us";
                        }, 2000);
                    }
                    $('#update_about_us').attr('disabled', false);
                    $('#update_about_us').text('Submit');
                },
               
            });

            $('#add_about_btn').attr('disabled', false);
                    $('#add_about_btn').text('Submit');
        }
    });
});

const imageInputEdit = document.getElementById('imageInputNormal');
const imagePreviewEdit = document.getElementById('imageInputPreviewNormal');

imageInputEdit.addEventListener('change', function() {
    const file = this.files[0];
    if (file) {
        const reader = new FileReader();
        reader.onload = function(event) {
            const img = document.createElement('img');
            img.src = event.target.result;
            img.style.maxWidth = '200px';
            img.style.maxHeight = '200px';
            imagePreviewEdit.innerHTML = '';
            imagePreviewEdit.appendChild(img);
        };
        reader.readAsDataURL(file);
    } else {
        imagePreviewEdit.innerHTML = '';
    }
});