$(function(){

    var messengerReport;

// Function to initialize and fill the messeneger report data table
function fill_datatables_messenger_report(startdatetime ='',enddatetime ='',status ='',send_to = '') {
   
    messengerReport = $('#messenger_report').DataTable({
        "processing": true,
        "serverSide": true,
        "order": [],
        "pageLength": 25, // 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.status = $('#status').val();
                d.send_to = $('#send_to').val();
                d.action = 'messenger_report';
            }
        }
    });
}

// Initialize messenger report table
fill_datatables_messenger_report();

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

// Submit form and reload data based on new filters
$('#messenger_report_form').submit(function(e) {
    console.log('exe');
    e.preventDefault();
    
    $('#messenger_report').DataTable().destroy();
    var startdatetime = $('#startdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var status = $('#status').val();
    var send_to = $('#send_to').val();
   
    fill_datatables_messenger_report(startdatetime,enddatetime,status,send_to);// Refresh the table with the new filter parameters
});

})

	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';
		}
	});