jQuery(document).ready(
    function ($) {
        // Listen for file selection in Elementor media control
        $(document).on(
            'change', '[data-setting="excel_file"]', function () {
                // Get the selected file URL
                var file_url = $(this).find('input').val();

                if (file_url) {
                    // Send AJAX request to fetch sheet names
                    $.ajax(
                        {
                            url: ajax_object.ajax_url,
                            method: 'POST',
                            data: {
                                action: 'get_excel_sheets',
                                file_url: file_url
                            },
                            success: function (response) {
                                if (response.success) {
                                    var sheetOptions = '';
                                    $.each(
                                        response.data, function (index, sheetName) {
                                            sheetOptions += '<option value="' + index + '">' + sheetName + '</option>';
                                        }
                                    );

                                    // Update the sheet dropdown in Elementor controls
                                    $('select[data-setting="sheet_select"]').html(sheetOptions);
                                } else {
                                    console.log('Error:', response.data.message);
                                }
                            },
                            error: function (xhr, status, error) {
                                console.log('AJAX Error:', error);
                            }
                        }
                    );
                }
            }
        );
    }
);
