jQuery(document).ready(function () {

    // Trigger file input click when import button is clicked
    jQuery('#import_btn').click(function () {
        jQuery('#pwdms_csvim_upload_file').trigger("click");
    });

    // Select/Deselect all checkboxes when the "Select All" checkbox is clicked
    jQuery("#pwdms_select_all_csv").click(function () {
        var pwdms_select_all = jQuery(this).prop('checked');

        jQuery("#pwdms_form_csv_export .form-table input[type=checkbox]").each(function () {
            jQuery(this).prop("checked", pwdms_select_all);
        });
    });

    // Handle the export button click to start the export process
    jQuery('#pwdms_export_btn').click(function (e) {
        e.preventDefault();
        export_csv_detail_list();
    });

    // Function to handle the CSV export process
    function export_csv_detail_list() {
        var progressLabel = jQuery(".progress-label");
        var form = jQuery('#pwdms_form_csv_export').serializeArray();
        var data = {};

        // Serialize form data
        jQuery(form).each(function (index, obj) {
            data[obj.name] = obj.value;
        });

        // Perform AJAX request to export data
        jQuery.ajax({
            url: MyAjax.ajaxurl,
            method: "POST",
            dataType: "json",
            data: { from_data: data, action: 'pwdms_export_detail_list' },
            success: function (response) {
                if (typeof response.data.page !== 'undefined') {
                    jQuery('#page_num').val(response.data.page);

                    // If export is not done, update the progress bar and recursively call the function
                    if (response.data.done === false) {
                        jQuery("#csv_export_progressbar").progressbar({
                            value: 1000,
                            change: function () {
                                progressLabel.text(jQuery("#csv_export_progressbar").progressbar("value") + "%");
                            },
                        });
                        export_csv_detail_list();  // Recursive call for pagination
                    } else {
                        // Complete the progress bar and hide it
                        jQuery("#csv_export_progressbar").progressbar({
                            value: 1000,
                            change: function () {
                                jQuery("#csv_export_progressbar").text('');
                            },
                        });
                        jQuery("#csv_export_progressbar").hide();
                        jQuery('#page_num').val(1);
                        jQuery('.export_error').remove();

                        // Handle successful or no data response
                        if (response.data.found_posts > 0) {
                            pwdms_export_csv();  // Call function to export CSV
                            jQuery('.export_successfully').remove();
                            jQuery('#pwdms_csv_export_table').after('<div class="export_successfully"><p>' + MyAjax.export_data + '</p></div>');
                        } else {
                            jQuery('.export_successfully').remove();
                            jQuery('#pwdms_csv_export_table').after('<div class="export_error"><p>' + MyAjax.no_export_data + '</p></div>');
                        }
                    }
                }
            },
        });
    }

    // Function to trigger the CSV export and download
    function pwdms_export_csv() {
        jQuery.get(MyAjax.ajaxurl, {
            action: 'pwdms_export_csv'
        }).done(function () {
            window.location = MyAjax.ajaxurl + '?action=pwdms_export_csv&document_title=' + encodeURIComponent(jQuery('#pwdms_document_title').val()) + '&_wpnonce=' + MyAjax.security_nonce;
        });
    }
});
