$(function () {

    var WhatsadatappReport;

    // Function to initialize and fill the WhatsApp data table
    function fill_datatables_whatsapp_report(startdatetime = '', enddatetime = '', status = '', send_to = '') {

        WhatsadatappReport = $('#whatsapp_report').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10, // Default records per page
            "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
            "searching": false,
            "paging": true, // Ensure paging is enabled
            "dom": "lBfrtip", // "p" ensures pagination is included in the layout
            "buttons": [
                {
                    extend: 'csvHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'CSV',
                    filename: 'csv',
                    title: $('.download_label').html(),
                    exportOptions: {
                        columns: ':visible'
                    }
                }, {
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'Excel',
                    title: $('.download_label').html(),
                    exportOptions: {
                        columns: ':visible'
                    }
                }, {
                    extend: 'pdfHtml5',
                    filename: $('.report_name').text(),
                    messageTop: $('.download_label').html(),
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    // changed logo code [vastvikta][05-05-2025]
                    customize: function (doc) {

                        var logoBase64 = document.getElementById('pdf-logo-base64').innerText;
                        doc.images = doc.images || {};
                        doc.images.logo = logoBase64;

                        doc.content.splice(1, 0, {
                            margin: [0, 0, 0, 5],
                            alignment: 'left',
                            image: 'logo',
                            width: 250
                        });
                    },
                    title: '.',
                    exportOptions: {
                        columns: ':visible'

                    }
                }, {
                    extend: 'colvis',
                    text: '<i class="fa fa-columns"></i>',
                    titleAttr: 'Columns',
                    title: $('.download_label').html(),
                    postfixButtons: ['colvisRestore']
                },
            ],
            "ajax": {
                url: "Report/report_function.php",
                type: "POST",
                data: function (d) {
                    d.startdatetime = $('#startdatetime').val();
                    d.enddatetime = $('#enddatetime').val();
                    d.send_to = $('#send_to').val();
                    d.status = $('#status').val();
                    d.group_by = $('input[name="group_by"]:checked').val(); // Capture selected radio button value
                    d.action = 'Whatsapp_report';
                }
            }
        });
    }

    // Initialize WhatsApp report table
    fill_datatables_whatsapp_report();

    // Set an interval to refresh the table every 10 seconds
    // setInterval(function () {
    //     WhatsadatappReport.ajax.reload(null, false); // Reload data without resetting pagination
    // }, 10000);

    // Submit form and reload data based on new filters
    $('#whatsapp_report_form').submit(function (e) {
        console.log('exe');
        e.preventDefault();

        $('#whatsapp_report').DataTable().destroy();
        var startdatetime = $('#startdatetime').val();
        var enddatetime = $('#enddatetime').val();
        var status = $('#status').val();
        var send_to = $('#send_to').val();
        fill_datatables_whatsapp_report(startdatetime, enddatetime, status, send_to);// Refresh the table with the new filter parameters
    });

    // Reset button functionality
    $('.reset_whatsapp_report').click(function () {

        // Destroy the existing DataTable
        WhatsadatappReport.destroy();

        // Get the current date and time
        var currentDate = new Date();

        // Format the first day of the current month as '01-mm-yyyy 00:00:00'
        var startOfMonth = '01-' + ('0' + (currentDate.getMonth() + 1)).slice(-2) + '-' + currentDate.getFullYear() + ' 00:00:00';

        // Format the current date as 'dd-mm-yyyy 23:59:59'
        var formattedEndDate = ('0' + currentDate.getDate()).slice(-2) + '-' +
            ('0' + (currentDate.getMonth() + 1)).slice(-2) + '-' +
            currentDate.getFullYear() + ' 23:59:59';

        // Reset the form fields
        $('#startdatetime').val(startOfMonth);
        $('#enddatetime').val(formattedEndDate);
        $('#status').val('');
        $('#send_to').val('0'); // Reset to the default "Select" option
        $('#searchBox').val(''); // Clear the search input field

        // Optionally, clear any previous dropdown visibility or messages
        $('#dropdownContainer').hide(); // Hide the dropdown if it is visible
        $('#noPhoneNumber').hide(); // Hide the "No records found" message if visible
        $('#searchSenderBox').val('');
        $('#searchBox').val('');

        // Reinitialize the table with default filters
        fill_datatables_whatsapp_report(); // Ensure this function initializes the table correctly
    });

});

function filterFunction(selectId, searchBoxId, messageDivId) {
    var input = document.getElementById(searchBoxId);
    var filter = input.value.toUpperCase();
    var select = document.getElementById(selectId);
    var options = select.getElementsByTagName("option");
    var visibleOptionsCount = 0;
    var dropdownContainer = document.getElementById('dropdownContainer');
    var messageDiv = document.getElementById(messageDivId);

    dropdownContainer.style.display = filter.length > 0 ? "block" : "none";
    var hasVisibleOptions = false;

    for (var i = 1; i < options.length; i++) {
        var txtValue = options[i].textContent || options[i].innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
            options[i].style.display = "";
            hasVisibleOptions = true;
        } else {
            options[i].style.display = "none";
        }
    }

    messageDiv.style.display = !hasVisibleOptions && filter.length > 0 ? "block" : "none";
}

function selectOption(selectId, searchBoxId) {
    var select = document.getElementById(selectId);
    var searchBox = document.getElementById(searchBoxId);
    searchBox.value = select.options[select.selectedIndex].text;
    document.getElementById('dropdownContainer').style.display = "none";
}

document.addEventListener('click', function (event) {
    var dropdownContainer = document.getElementById('dropdownContainer');
    var searchBox = document.getElementById('searchBox');
    if (!dropdownContainer.contains(event.target) && !searchBox.contains(event.target)) {
        dropdownContainer.style.display = 'none';
    }
});
// Handle "Select All" checkbox
$('#select-all').on('click', function () {
    const isChecked = $(this).prop('checked');
    $('.row-checkbox').prop('checked', isChecked);
});

// Collect selected row IDs
function getSelectedRowIds() {
    const selectedIds = [];
    $('.row-checkbox:checked').each(function () {
        selectedIds.push($(this).data('id'));
    });
    return selectedIds;
}

// Handle bulk actions
$('#bulk-reschedule').on('click', function () {
    const selectedIds = getSelectedRowIds();
    if (selectedIds.length === 0) {
        alert('No rows selected!');
        return;
    }
    // Open reschedule popup and pass selected IDs
    $('#reschedule-popup').fadeIn();
    $('#bulk-data-ids').val(selectedIds.join(','));
});

// Reschedule Selected
$('#confirm-reschedule').on('click', function () {
    const selectedIds = $('#bulk-data-ids').val();
    const rescheduleDate = $('#reschedule-date').val();
    if (selectedIds.length === 0) {
        alert('Please select at least one WhatsApp to reschedule.');
        return;
    }
    $.ajax({
        url: 'Report/reschedule.php',
        type: 'POST',
        data: {
            ids: selectedIds.split(','),
            DateNew: rescheduleDate,
            action: 'whatsapp_reschedule'
        },
        success: function (response) {
            alert('Rescheduling successful!');
            $('#reschedule-popup').fadeOut();
            window.location.reload();
        },
        error: function (xhr) {
            alert('Error rescheduling WhatsApp.');
        }
    });
});
document.getElementById('cancel-reschedule').addEventListener('click', function () {
    document.getElementById('reschedule-popup').style.display = 'none';
});
// Expire Selected
$('#expire_selected').on('click', function () {
    const selectedIds = getSelectedRowIds();
    if (selectedIds.length === 0) {
        alert('Please select at least one WhatsApp to expire.');
        return;
    }
    // Show confirmation dialog
    const confirmation = confirm('Are you sure you want to mark the selected WhatsApp as expired?');
    if (!confirmation) {
        return; // Exit if the user cancels
    }
    $.ajax({
        url: 'Report/reschedule.php',
        type: 'POST',
        data: {
            ids: selectedIds,
            action: 'whatsapp_expire'
        },
        success: function (response) {
            alert('WhatsApp marked as expired.');
            window.location.reload();
        },
        error: function (xhr) {
            alert('Error expiring WhatsApp.');
        }
    });
});