
$(document).ready(function () {
    var table = $("#report-server-side").DataTable({
        "searching": false,
        "responsive": true,
        "pageLength": 10,
        "lengthChange": false,
        "autoWidth": true,
        "processing": true,
        "serverSide": true,
        "searchable": false,
        dom: "Bfrtip",
        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',
                text: '<i class="fa fa-file-pdf-o"></i>',
                titleAttr: 'PDF',
                filename: $('.report_name').text() || 'Report',
                messageTop: $('.download_label').html() || 'No data available',
                orientation: 'landscape',
                pageSize: 'A3',
                exportOptions: { columns: ':visible' },
                // changed the customized function to download the pdf even when there is no data [vastvikta][19-3-2025]
                customize: function (doc) {
                    if (doc.content.length < 2) { // If table content is missing
                        doc.content.push({
                            text: 'No data available to export',
                            alignment: 'center',
                            margin: [0, 10, 0, 10]
                        });
                    }
                }
            },
            {
                extend: 'colvis',
                text: '<i class="fa fa-columns"></i>',
                titleAttr: 'Columns',
                title: $('.download_label').html(),
                postfixButtons: ['colvisRestore']
            },
        ],
        "ajax": {
            "url": "Report/report_function.php",
            "dataType": "json",
            "type": "POST",
            "data": function (d) {
                d.from_date = $('#from_date').val();
                d.end_date = $('#end_date').val();
                d.user_wise = $('#user_wise').val();
                d.action = 'twiter_report';
                d.channeltype = 'twiter';
            }
        },
        "columns": [
            { "data": "#" },
            { "data": "created_date" },
            { "data": "queue" },
            { "data": "submitted" },
            { "data": "delivered" },
            { "data": "not_delivered" },
            { "data": "message_data" },
            { "data": "schedule_time" }
        ]
    });

    $('#reset').on('click', function () {
        $('#user_wise').val('');

        // Get the first day of current month
        const fromDate = new Date();
        fromDate.setDate(1);

        const endDate = new Date(); // Today

        function formatDateTime(date) {
            const d = String(date.getDate()).padStart(2, '0');
            const m = String(date.getMonth() + 1).padStart(2, '0');
            const y = date.getFullYear();
            return `${d}-${m}-${y} ${date.getHours() === 0 ? '00:00:00' : '23:59:59'}`;
        }

        $('#from_date').val(formatDateTime(fromDate));
        $('#end_date').val(formatDateTime(endDate));

        table.ajax.reload();
    });

});