// updated title for reports custom according to report and date [vastvikta][17-04-2025]
// Data table code (Report section)
$(document).ready(function(){

    fill_datatable_agent_break();
    function fill_datatable_agent_break( startdatetime = '', enddatetime = ''){
        var startdatetime = $('#sttartdatetime').val();
        var enddatetime = $('#enddatetime').val();
       var startDateOnly = startdatetime.substring(0, 10);
        var endDateOnly = enddatetime.substring(0, 10);
        
       var dataTable = $('#agent_break_report').DataTable({
             "processing": true,
             "serverSide": true,
             "order": [],
             "pageLength": 10,
             "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'CRM Users Break Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'CRM Users Break Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'CRM Users Break Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'CRM Users Break Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                        extend: 'pdfHtml5',
                        filename : 'CRM Users Break Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                        messageTop : 'CRM Users Break Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                        orientation: 'landscape',
                        pageSize: 'A3',
                        text: '<i class="fa fa-file-pdf-o"></i>',
                        titleAttr: 'PDF',
                        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: 'CRM Users Break Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
          "ajax" : {
             url:"Report/report_function.php",
             type:"POST",
             data:{
                   startdatetime:startdatetime,
                   enddatetime:enddatetime,
                   action:'agent_break_report'
             }
          }
       });
    }
    
    $(document).on('click', '.toggle-view', function(e) {
        e.preventDefault();
    
        // Get the username and startdate from the clicked link
        var username = $(this).data('username');
        var startdate = $(this).data('startdate');
        
        // Get the parent row of the clicked link
        var parentRow = $(this).closest('tr');
        
        // Check if the detail row already exists
        if ($(this).hasClass('open')) {
            // Detail row is already open, so remove it (collapse)
            parentRow.next('.detail-row').remove();
            $(this).removeClass('open'); // Mark it as collapsed
        } else {
            // Collapse any other open detail rows
            $('.detail-row').remove();
            $('.toggle-view').removeClass('open');
            
            // Insert the details table right after the parent row
            var detailRow = `
                <tr class="detail-row">
                    <td colspan="7">
                        <table class="tableview tableview-2 break-details" id="break_report_details">
                           <thead>
                              <tr class="background" style="font-size: 12px">
                                 <th align="center">Sno</th>
                                 <th align="center">Break Name</th>
                                 <th align="center">Break Time In</th>
                                 <th align="center">Break Time Out</th>
                                 <th align="center">Duration</th>
                              </tr>
                           </thead>
                        </table>
                    </td>
                </tr>
            `;
            
            // Append the detailRow after the current parent row
            parentRow.after(detailRow);
            $(this).addClass('open'); // Mark the row as open
    
            // Call the function to fill the details table
            fill_datatable_break_report_details(username, startdate, `#break_report_details_${username}_${startdate}`);
        }
    });
    
    
    function fill_datatable_break_report_details(username = '', startdate = '' ){
        var dataTable = $('#break_report_details').DataTable({
              "processing": true,
              "serverSide": true,
              "order": [],
              "pageLength": 10,
              "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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',
                        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:{
                username: username,
                startdate: startdate,
                action:'break_report_details'
              }
           }
        });
     }
   // Store initial values of date inputs when document is ready
 var initialStartDate;
 var initialEndDate;
 
 $(document).ready(function() {
     initialStartDate = $('#sttartdatetime').val();
     initialEndDate = $('#enddatetime').val();
 });
 $('#submit').click(function() {
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();

    // Destroy DataTable instance to reinitialize with new data
    $('#agent_break_report').DataTable().destroy();

    // Call fill_datatable function with appropriate filter parameters
    fill_datatable_agent_break(startdatetime, enddatetime);
});

 
// Report-> Agent report datatable code
   fill_datatable();
   function fill_datatable(agent = '', timeperiod = '', startdatetime = '', enddatetime = ''){
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var startDateOnly = startdatetime.substring(0, 10);
    var endDateOnly = enddatetime.substring(0, 10);
        
       
      var dataTable = $('#agent_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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:'Login User Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Login User Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'Login User Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Login User Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'Login User Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'Login User Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'Login User Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  agent_n:agent, 
                  timeperiod:timeperiod,
                  startdatetime:startdatetime,
                  enddatetime:enddatetime,
                  action:'view_agentreport'
            }
         }
      });
   }
  // Store initial values of date inputs when document is ready
var initialStartDate;
var initialEndDate;

$(document).ready(function() {
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
});

$('#submit').click(function(){
    var agent = $('#agent_n').val();
    var timeperiod = $('#timeperiod').val();
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    
    // Destroy DataTable instance to reinitialize with new data
    $('#agent_data').DataTable().destroy();

    // Call fill_datatable function with appropriate filter parameters
    fill_datatable(agent,timeperiod,startdatetime,enddatetime);
});

// added the code for the  save filter in login user report[vastvikta][26-03-2025]
$(document).ready(function() {// Event listener for clicking the "Save Filter" button
    $("#save_filters1").click(function() {
        // Get the filter name input and trim any extra spaces
        let filterName = $("#filter_name").val().trim();
        // Get the filter page value (hidden input field)
        let filterPage = $("#filter_page").val(); 
    
        // Validate if the filter name is empty
        if (filterName === "") {
            alert("Please enter a filter name!"); // Alert user to enter a filter name
            return; // Stop execution
        }
    
        // Collect selected filter values from the form
        let filterData = {
            agent_n: $("#agent_n").val(),  // Get selected agent name
            sttartdatetime: $("#startdatetime").val(), // Fix incorrect ID name
            enddatetime: $("#enddatetime").val(), // Get end date/time
            timeperiod: $("#timeperiod").val(), // Get time period selection
        };
    
        // Prepare the JSON payload for AJAX request
        let payload = {
            action: "save_filter", // Identify the action being performed
            filter_name: filterName, // Filter name entered by user
            filters: filterData, // Collected filter data
            filter_page: filterPage, // Page identifier
        };
    
        // Send filter name & values via AJAX request to save in the database
        $.ajax({
            url: "Report/save_filter.php", // PHP script handling the request
            type: "POST", // Use POST method for data submission
            contentType: "application/json", // Specify JSON format
            data: JSON.stringify(payload), // Convert payload to JSON string
            success: function(response) {
                if (response.status === "success") {
                    alert(response.message); // Show success message
                    $("#filterModal").hide(); // Hide the modal after saving
                    location.reload(); // Refresh the page to update saved filters
                } else {
                    alert("Error: " + response.message); // Show error message from server
                }
            },
            error: function(xhr, status, error) {
                console.error("AJAX Error:", status, error); // Debugging: log AJAX error details
                alert("Error saving the filter. Please try again."); // Inform user about failure
            }
        });
    });
    
    // Event listener for when a user selects a saved filter from the dropdown
    $("#saved_filters").change(function () {
        // Get the selected filter ID
        let selectedFilter = $("#saved_filters").val();
    
        // Validate if no filter is selected
        if (selectedFilter === "") {
            alert("Please select a saved filter."); // Alert user
            return; // Stop execution
        }
         // Send AJAX request to retrieve filter data
        $.ajax({
            url: "Report/save_filter.php", // PHP script handling filter retrieval
            type: "POST", // Use POST method
            contentType: "application/json",  // Specify JSON format
            data: JSON.stringify({ 
                action: "get_filter", // Identify the action as retrieving a filter
                filter_id: selectedFilter // Send the selected filter ID
            }),
            success: function (response) {
                if (response.status === "success") {
                    let filterData = response.filters; // Get filter data from response
                     if (filterData) {
                        // Populate form fields with retrieved filter data
                        $("#agent_n").val(filterData.agent_n);
                        $("#startdatetime").val(filterData.sttartdatetime);
                        $("#enddatetime").val(filterData.enddatetime);
                        $("#timeperiod").val(filterData.timeperiod);
    
                        // Destroy the existing DataTable instance before reloading data
                        $('#agent_data').DataTable().destroy();
    
                        // Call function to fill the table with filtered data
                        fill_datatable(filterData.agent_n, filterData.timeperiod, filterData.startdatetime, filterData.enddatetime);
    
                    } else {
                        alert("No data found in the filter."); // Inform user if no data is found
                    }
                } else {
                    alert("Error: " + response.message); // Show error message if retrieval fails
                }
            },
            error: function (xhr, status, error) {
                console.error("AJAX Error:", status, error); // Debugging: log AJAX errors
                alert("Failed to fetch filter data. Please try again."); // Inform user about failure
            }
        });
    });
    

//  added this code  for filter active inactive [vastvikta][02-04-2025]
    // Event listener for clicking the "Delete Filter" button
    $("#delete_filter").on("click", function () {
        // Get the selected filter ID from the dropdown
        var selectedFilter = $("#saved_filters").val();
        // Check if a filter is selected, if not, show an alert and exit
        if (!selectedFilter) {
            alert("Please select a filter to delete.");
            return;
        }

        // Confirm deletion with the user
        if (confirm("Are you sure you want to delete this filter?")) {
            // Prepare the data payload for the AJAX request
            let payload = {
                action: "delete_filter",  // Specifies the action to perform
                filter_id: selectedFilter // The ID of the filter to be deleted
            };

            // Send an AJAX request to delete the filter
            $.ajax({
                url: "Report/save_filter.php", // The server-side script handling the request
                type: "POST", // HTTP method
                contentType: "application/json", // Specify that we're sending JSON data
                data: JSON.stringify(payload), // Convert the payload object to a JSON string
                success: function (response) { // Function executed if request is successful
                    if (response.status === "success") {
                        alert(response.message); // Show success message
                        location.reload(); // Reload the page to reflect changes
                    } else {
                        alert("Error: " + response.message); // Show error message if deletion fails
                    }
                },
                error: function (xhr, status, error) { // Handle AJAX errors
                    console.error("AJAX Error:", status, error);
                    alert("Error deleting the filter. Please try again.");
                }
            });
        }
    });

    // Event listener for clicking the "Restore Filter" button
    $("#restore_filter").on("click", function () {
        // Get the selected filter ID from the dropdown
        var selectedFilter = $("#saved_filters").val();
        // Check if a filter is selected, if not, show an alert and exit
        if (!selectedFilter) {
            alert("Please select a filter to restore.");
            return;
        }

        // Confirm restoration with the user
        if (confirm("Are you sure you want to restore this filter?")) {
            // Prepare the data payload for the AJAX request
            let payload = {
                action: "restore_filter",  // Specifies the action to perform
                filter_id: selectedFilter // The ID of the filter to be restored
            };

            // Send an AJAX request to restore the filter
            $.ajax({
                url: "Report/save_filter.php", // The server-side script handling the request
                type: "POST", // HTTP method
                contentType: "application/json", // Specify that we're sending JSON data
                data: JSON.stringify(payload), // Convert the payload object to a JSON string
                success: function (response) { // Function executed if request is successful
                    if (response.status === "success") {
                        alert(response.message); // Show success message
                        location.reload(); // Reload the page to reflect changes
                    } else {
                        alert("Error: " + response.message); // Show error message if restoration fails
                    }
                },
                error: function (xhr, status, error) { // Handle AJAX errors
                    console.error("AJAX Error:", status, error);
                    alert("Error restoring the filter. Please try again.");
                }
            });
        }
    });
    // Event listener for clicking the "Update Filter" button
    $("#save_filters2").click(function() {
        // Get the selected filter ID from the dropdown
        let filterId = $("#saved_filters").val();
         if (!filterId) {
            alert("Please select a filter to update.");
            return;
        }

        // Get the updated filter name from the input field
        let filterName = $("#filter_name_update").val();
        
        // Get the filter page value from a hidden input field (used for tracking)
        let filterPage = $("#filter_page").val();
        if (filterName === "") {
            alert("Please enter a filter name!");
            return;
        }

        // Collect selected filter values from form inputs
        let filterData = {
            agent_n: $("#agent_n").val(), // Get agent name or ID
            startdatetime: $("#startdatetime").val(), // Get start date and time
            enddatetime: $("#enddatetime").val(), // Get end date and time
            timeperiod: $("#timeperiod").val(), // Get selected time period
        };
        // Prepare JSON payload for updating the filter
        let payload = {
            action: "update_filter", // Specify the action type
            filter_id: filterId, // ID of the filter being updated
            filter_name: filterName, // New filter name
            filters: filterData, // New filter criteria
            filter_page: filterPage, // Maintain filter page reference
        };

        // Send an AJAX request to update the filter
        $.ajax({
            url: "Report/save_filter.php", // Server-side script handling filter updates
            type: "POST", // HTTP request method
            contentType: "application/json", // Specify JSON format
            data: JSON.stringify(payload), // Convert the payload object to JSON string
            success: function(response) { // Function executed if request is successful
                if (response.status === "success") {
                    alert(response.message); // Show success message
                    $("#filterModal2").hide(); // Hide modal after updating filter
                    location.reload(); // Reload the page to reflect changes
                } else {
                    alert("Error: " + response.message); // Show error message if update fails
                }
            },
            error: function(xhr, status, error) { // Handle AJAX errors
                console.error("AJAX Error:", status, error);
                alert("Error updating the filter. Please try again.");
            }
        });
    });
});

// end of the code for the save filter in login user report 
$('#reset').click(function() {
    // Reset date inputs to initial values
    $('#startdatetime').val(initialStartDate);
    $('#enddatetime').val(initialEndDate);

    // Clear other filter inputs and reload DataTable
    $('#agent_data').DataTable().destroy();
    $('#agent_n, #timeperiod , #saved_filters').val('');
    fill_datatable();
});


// report-> customer page datatable with filter
   customer_datatable();
   function customer_datatable(district = '', village = '', startdatetime = '', enddatetime='', phone = ''){
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var startDateOnly = startdatetime.substring(0, 10);
    var endDateOnly = enddatetime.substring(0, 10);
   
      var dataTable = $('#customer_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'Customers Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Customers Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename:  'Customers Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Customers Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'Customers Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'Customers Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'Customers Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  district:district, 
                  village:village,
                  startdatetime:startdatetime,
                  enddatetime:enddatetime,
                  phone:phone,
                  action:'view_customer_report'
            }
         }
      });
   }
  // Store initial values of date inputs when document is ready
var initialStartDate;
var initialEndDate;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
});

$('#submit_cust').click(function(){
    var district = $('#district').val();
    var village = $('#village').val();
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var phone = $('#phone').val();

    // Destroy DataTable instance to reinitialize with new data
    $('#customer_data').DataTable().destroy();

    // Call customer_datatable function with appropriate filter parameters
    customer_datatable(district, village, startdatetime, enddatetime, phone);
});

$('#reset_cust').click(function() {
    // Reset date inputs to initial values
    $('#sttartdatetime').val(initialStartDate);
    $('#enddatetime').val(initialEndDate);

    // Clear other filter inputs and reload DataTable
    $('#customer_data').DataTable().destroy();
    $('#district, #village, #phone').val('');

    // Reload DataTable
    customer_datatable();
});

// Report-> report overview page data table
// added status field [vastvikta][16-05-2025]
   overview_datatable();
   function overview_datatable(startdatetime = '', enddatetime='',status = ''){
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var startDateOnly = startdatetime.substring(0, 10);
    var endDateOnly = enddatetime.substring(0, 10);
    
      var dataTable = $('#overview_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'Case Overview Report  FROM ' + startDateOnly + ' TO ' + endDateOnly, // Specify the desired filename here
                    title: 'Case Overview Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'Case Overview Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Case Overview Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename :'Case Overview Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'Case Overview Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'Case Overview Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  startdatetime:startdatetime, 
                  enddatetime:enddatetime,
                  status:status,
                  action:'view_report_overview'
            }
         }
      });
   }
// Store initial values of date inputs when document is ready
var initialStartDate;
var initialEndDate;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#startdatetime').val();
    initialEndDate = $('#enddatetime').val();
});

$('#submit_overview').click(function(){
    var startdatetime = $('#startdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var status = $('#status').val();
    // Destroy DataTable instance to reinitialize with new data
    $('#overview_data').DataTable().destroy();
    // Call overview_datatable function with appropriate filter parameters
    overview_datatable(startdatetime,enddatetime,status);
});

$('#reset_overview').click(function() {
    // Reset date inputs to initial values
    $('#startdatetime').val(initialStartDate);
    $('#enddatetime').val(initialEndDate);
    $('#status').val(status);
    // Destroy DataTable instance and reload with default data
    $('#overview_data').DataTable().destroy();
    overview_datatable();
});
// Report-> audit page datatable with filter
   audit_datatable();
   function audit_datatable(agent = '', startdatetime = '', enddatetime = ''){
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var startDateOnly = startdatetime.substring(0, 10);
    var endDateOnly = enddatetime.substring(0, 10);
   
      var dataTable = $('#audit_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'Audit Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Audit Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'Audit Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Audit Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'Audit Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'Audit Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'Audit Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  agent:agent, 
                  startdatetime:startdatetime,
                  enddatetime:enddatetime,
                  action:'view_audit_report'
            }
         }
      });
   }
  // Store initial values of date inputs and agent filter when document is ready
var initialStartDate;
var initialEndDate;
var initialAgent;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
    initialAgent = $('#agent').val();
});

$('#submit_audit').click(function(){
    var agent = $('#agent').val();
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();

    // Destroy DataTable instance to reinitialize with new data
    $('#audit_data').DataTable().destroy();

    // Call audit_datatable function with appropriate filter parameters
    audit_datatable(agent, startdatetime, enddatetime);
});

$('#reset_audit').click(function() {
    // Reset date inputs and agent filter to initial values
    $('#sttartdatetime').val(initialStartDate);
    $('#enddatetime').val(initialEndDate);
    $('#agent').val(initialAgent);

    // Destroy DataTable instance and reload with default data
    $('#audit_data').DataTable().destroy();
    audit_datatable();
});

// Report-> Frequent caller page datatable with filter
   fc_datatable();
   function fc_datatable(timeperiod = '', startdatetime = '', enddatetime = ''){
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var startDateOnly = startdatetime.substring(0, 10);
    var endDateOnly = enddatetime.substring(0, 10);
   
      var dataTable = $('#fc_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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:  'Frequent Caller Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title:  'Frequent Caller Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'Frequent Caller Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title:  'Frequent Caller Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'Frequent Caller Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop :  'Frequent Caller Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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:  'Frequent Caller Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  timeperiod:timeperiod, 
                  startdatetime:startdatetime,
                  enddatetime:enddatetime,
                  action:'view_frequent_caller'
            }
         }
      });
   }
// Store initial values of date inputs and timeperiod filter when document is ready
var initialStartDate;
var initialEndDate;
var initialTimeperiod;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
    initialTimeperiod = $('#timeperiod').val();
});

$('#submit_caller').click(function(){
    var timeperiod = $('#timeperiod').val();
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();

    // Destroy DataTable instance to reinitialize with new data
    $('#fc_data').DataTable().destroy();

    // Call fc_datatable function with appropriate filter parameters
    fc_datatable(timeperiod, startdatetime, enddatetime);
});

$('#reset_caller').click(function() {
    // Reset date inputs and timeperiod filter to initial values
    $('#sttartdatetime').val(initialStartDate);
    $('#enddatetime').val(initialEndDate);
    $('#timeperiod').val(initialTimeperiod);

    // Destroy DataTable instance and reload with default data
    $('#fc_data').DataTable().destroy();
    fc_datatable();
});

// Report-> Frequent ticket page datatable with filter
   ft_datatable();
   function ft_datatable(timeperiod = '', startdatetime = '', enddatetime = ''){
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var startDateOnly = startdatetime.substring(0, 10);
    var endDateOnly = enddatetime.substring(0, 10);
   
      var dataTable = $('#ft_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'Frequent Ticket Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Frequent Ticket Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'Frequent Ticket Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title:'Frequent Ticket Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'Frequent Ticket Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'Frequent Ticket  Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'Frequent Ticket Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  timeperiod:timeperiod, 
                  startdatetime:startdatetime,
                  enddatetime:enddatetime,
                  action:'view_frequent_ticket'
            }
         }
      });
   }
   // This code for submit buttion get filter data
   $('#submit_ticket').click(function(){
      var timeperiod = $('#timeperiod').val();
      var startdatetime = $('#sttartdatetime').val();
      var enddatetime = $('#enddatetime').val();
      // Destroy DataTable instance to reinitialize with new data
      $('#ft_data').DataTable().destroy();
      // Call ft_datatable function with appropriate filter parameters
      ft_datatable(timeperiod,startdatetime,enddatetime);
   });
   // Reset button click code reset filter option
   $('#reset_ticket').click(function(){
      $('#ft_data').DataTable().destroy();
      $('#timeperiod').val('');
      ft_datatable();
   });


// Report-> Customer satis report summary
   csrs_datatable();
   function csrs_datatable(startdatetime = '', enddatetime = '', agent = ''){
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val(); 
    var startDateOnly = startdatetime.substring(0, 10);
    var endDateOnly = enddatetime.substring(0, 10);
   

      var dataTable = $('#csrss_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'CSAT-DSAT Summary Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'CSAT-DSAT Summary Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'CSAT-DSAT Summary Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title:'CSAT-DSAT Summary Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'CSAT-DSAT Summary Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'CSAT-DSAT Summary Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'CSAT-DSAT Summary Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  startdatetime:startdatetime,
                  enddatetime:enddatetime,
                  agent:agent, 
                  action:'view_CSAT_DSAT_summary'
            }
         }
      });
   }
// Store initial values of date inputs and agent filter when document is ready
var initialStartDate;
var initialEndDate;
var initialAgent;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
    initialAgent = $('#agent').val();
});

$('#submit_summary').click(function(){
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var agent = $('#agent').val();

    // Destroy DataTable instance to reinitialize with new data
    $('#csrss_data').DataTable().destroy();

    // Call csrs_datatable function with appropriate filter parameters
    csrs_datatable(startdatetime, enddatetime, agent);
});

$('#reset_summary').click(function() {
    // Reset date inputs and agent filter to initial values
    $('#sttartdatetime').val(initialStartDate);
    $('#enddatetime').val(initialEndDate);
    $('#agent').val(initialAgent);

    // Destroy DataTable instance and reload with default data
    $('#csrss_data').DataTable().destroy();
    csrs_datatable();
});

// Report-> Customer satis report detail
   csrd_datatable();
   function csrd_datatable(Type = '', startdatetime = '', enddatetime = '', agent = '', Phone_Number = '', Customer_email = '', source = '',mode =''){
        var startdatetime = $('#sttartdatetime').val();
        var enddatetime = $('#enddatetime').val();
        var startDateOnly = startdatetime.substring(0, 10);
        var endDateOnly = enddatetime.substring(0, 10);
       
        var dataTable = $('#csrd_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'CSAT-DSAT Detailed Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'CSAT-DSAT Detailed Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'CSAT-DSAT Detailed Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'CSAT-DSAT Detailed Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'CSAT-DSAT Detailed Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'CSAT-DSAT Detailed Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'CSAT-DSAT Detailed Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
            "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                    Type : Type,
                    startdatetime:startdatetime,
                    enddatetime:enddatetime,
                    agent:agent, 
                    Phone_Number:Phone_Number,
                    Customer_email: Customer_email,
                    source: Customer_email,
                    mode:mode,
                    action:'view_CSAT_DSAT_detail'
            }
            }
        });
   }
   // This code for submit buttion get filter data
   $('#submit_detail').click(function(){
      var Type = $('#Type').val();
      var startdatetime = $('#sttartdatetime').val();
      var enddatetime = $('#enddatetime').val();
      var agent = $('#agent').val();
      var Phone_Number = $('#Phone_Number').val();
      var Customer_email = $('#Customer_email').val();
      var source = $('#source').val();
      
      // Destroy DataTable instance to reinitialize with new data
      $('#csrd_data').DataTable().destroy();

      // Call csrd_datatable function with appropriate filter parameters
      csrd_datatable(Type,startdatetime,enddatetime,agent, Phone_Number, Customer_email, source);
   });
// Store initial dates when the page loads
var originalStartDate = $('#sttartdatetime').val();
var originalEndDate = $('#enddatetime').val();

// Adjusted reset button click event
$('#reset_detail').click(function() {
    // Clear the other filter fields
    $('#Type, #agent, #Phone_Number, #Customer_email, #source').val('');

    // Restore the start and end dates to their original values
    $('#sttartdatetime').val(originalStartDate);
    $('#enddatetime').val(originalEndDate);

    // Destroy the DataTable instance and reinitialize without filters
    $('#csrd_data').DataTable().destroy();
    csrd_datatable(); // Calling without parameters to load all data
});

// Customer Effort datatable
   EFFORT_datatable();
   function EFFORT_datatable(startdatetime = '', enddatetime = '', customer_effort = '', category = '', subcategory = '', source = '', mode = ''){
        var startdatetime = $('#sttartdatetime').val();
        var enddatetime = $('#enddatetime').val();
        var startDateOnly = startdatetime.substring(0, 10);
        var endDateOnly = enddatetime.substring(0, 10);
        var dataTable = $('#CEFFORT_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'Custome Effort Report FROM ' + startDateOnly + ' TO ' + endDateOnly,// Specify the desired filename here
                    title: 'Customer Effort Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'Customer Effort Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Customer Effort Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'Customer Effort Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'Customer Effort Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'Customer Effort Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  startdatetime:startdatetime, 
                  enddatetime:enddatetime,
                  customer_effort:customer_effort,
                  category:category,
                  subcategory:subcategory,
                  source:source,
                  mode:mode,
                  action:'view_customer_effort_report'
            }
         }
      });
   }
   // Store initial values of date inputs when the document is ready
var initialStartDate;
var initialEndDate;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
});

$('#submit_effort').click(function(){
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var customer_effort = $('#customer_effort').val();
    var category = $('#category').val();
    var subcategory = $('#subcategory').val();
    var source = $('#source').val();
    var mode = $('#mode').val();
    
    // Destroy DataTable instance to reinitialize with new data
    $('#CEFFORT_data').DataTable().destroy();

    // Call EFFORT_datatable function with appropriate filter parameters
    EFFORT_datatable(startdatetime, enddatetime, customer_effort, category, subcategory, source, mode);
});

$('#reset_effort').click(function() {
    // Reset date inputs to initial values
    $('#sttartdatetime').val(initialStartDate);
    $('#enddatetime').val(initialEndDate);
    
    // Clear other filter inputs
    $('#customer_effort, #category, #subcategory, #source, #mode').val('');

    // Destroy DataTable instance and reload with default data
    $('#CEFFORT_data').DataTable().destroy();
    EFFORT_datatable();
});

   //nps report data table
   nps_datatable();
   function nps_datatable(startdatetime = '', enddatetime = '', mode = '', category = '', subcategory = '', source = ''){
    var startdatetime = $('#sttartdatetime').val();
   
    var enddatetime = $('#enddatetime').val();
    var startDateOnly = startdatetime.substring(0, 10);
    var endDateOnly = enddatetime.substring(0, 10);
       
      var dataTable = $('#nps_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'NPS Report FROM ' + startDateOnly + ' TO ' + endDateOnly,// Specify the desired filename here
                    title: 'NPS Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'NPS Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'ENSEMBLER \n NPS Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename :'NPS Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'NPS Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'NPS Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  startdatetime:startdatetime,
                  enddatetime:enddatetime,
                  mode:mode,
                  category:category,
                  subcategory:subcategory,
                  source:source,                  
                  action:'view_nps_report'
            }
         }
      });
   }
// Store initial values of date inputs when the document is ready
var initialStartDate;
var initialEndDate;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
});

$('#submit_nps').click(function(){
    var startdatetime = $('#sttartdatetime').val();
   
    var enddatetime = $('#enddatetime').val();
    var mode = $('#mode').val();
    var category = $('#category').val();
    var subcategory = $('#subcategory').val();
    var source = $('#source').val();
    
    // Destroy DataTable instance to reinitialize with new data
    $('#nps_data').DataTable().destroy();

    // Call nps_datatable function with appropriate filter parameters
    nps_datatable(startdatetime, enddatetime, mode, category, subcategory, source);
});

$('#reset_nps').click(function() {
    // Reset date inputs to initial values
    $('#startdatetime').val(initialStartDate);
    $('#enddatetime').val(initialEndDate);
    
    // Clear other filter inputs
    $('#mode, #category, #subcategory, #source').val('');

    // Destroy DataTable instance and reload with default data
    $('#nps_data').DataTable().destroy();
    nps_datatable();
});
   //voice mail data table
   voicemail_datatable();
   function voicemail_datatable(startdatetime = '', enddatetime='', status = ''){
        var startdatetime = $('#sttartdatetime').val();
        var enddatetime = $('#enddatetime').val();
        var startDateOnly = startdatetime.substring(0, 10);
        var endDateOnly = enddatetime.substring(0, 10);
        
        var dataTable = $('#voice_data').DataTable({
                "processing": true,
                "serverSide": true,
                "order": [],
                "pageLength": 10,
                "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'Voice Mail Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                        title: 'Voice Mail Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                        exportOptions: {
                            columns: ':visible'
                        }
                    },{
                        extend: 'excelHtml5',
                        text: '<i class="fa fa-file-excel-o"></i>',
                        titleAttr: 'Excel',
                        filename: 'Voice Mail Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                        title: 'ENSEMBLER \n Voice Mail Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                        exportOptions: {
                            columns: ':visible'
                        }
                    },{
                        extend: 'pdfHtml5',
                        filename : 'Voice Mail Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                        messageTop : 'Voice Mail Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                        orientation: 'landscape',
                        pageSize: 'A3',
                        text: '<i class="fa fa-file-pdf-o"></i>',
                        titleAttr: 'PDF',
                        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: 'Voice Mail Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                        postfixButtons: ['colvisRestore']
                    },
                ],
            "ajax" : {
                url:"Report/report_function.php",
                type:"POST",
                data:{
                    startdatetime:startdatetime,
                    enddatetime:enddatetime,
                    status:status,
                    action:'view_voicemail'
                }
            }
        });
   }
   // This code for submit buttion get filter data
   $('#submit_voice').click(function(){
      var startdatetime = $('#sttartdatetime').val();
      var enddatetime = $('#enddatetime').val();
      var status = $('#status').val();
      // Destroy DataTable instance to reinitialize with new data
      $('#voice_data').DataTable().destroy();
      // Call voicemail_datatable function with appropriate filter parameters
      voicemail_datatable(startdatetime,enddatetime,status);
   });
   // fcr data table
   fcr_datatable();
   function fcr_datatable(startdatetime = '', enddatetime = ''){
        var startdatetime = $('#sttartdatetime').val();
        var enddatetime = $('#enddatetime').val();
        var startDateOnly = startdatetime.substring(0, 10);
        var endDateOnly = enddatetime.substring(0, 10);
       
      var dataTable = $('#fcr_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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:  'FCR Report FROM ' + startDateOnly + ' TO ' + endDateOnly,// Specify the desired filename here
                    title:  'FCR Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'FCR Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title:  'ENSEMBLER \nFCR Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'FCR Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop :  'FCR Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'FCR Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  startdatetime:startdatetime,
                  enddatetime:enddatetime,
                  action:'view_fcr_report'
            }
         }
      });
   }

// Store initial values of date inputs when the document is ready
var initialStartDate;
var initialEndDate;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
});

$('#submit_fcr').click(function(){
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    
    // Destroy DataTable instance to reinitialize with new data
    $('#fcr_data').DataTable().destroy();

    // Call fcr_datatable function with appropriate filter parameters
    fcr_datatable(startdatetime, enddatetime);
});

$('#reset_fcr').click(function() {
    // Reset date inputs to initial values
    $('#sttartdatetime').val(initialStartDate);
    $('#enddatetime').val(initialEndDate);

    // Destroy DataTable instance and reload with default data
    $('#fcr_data').DataTable().destroy();
    fcr_datatable();
});
   // Web case data table
   Custumcase_datatable();
   function Custumcase_datatable(fname = '', casee = '', agent = '', startdatetime = '', enddatetime = '', status = '', category = '', subcategory = '', comp = '', source = ''){
     
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val(); 
    var startDateOnly = startdatetime.substring(0, 10);
    var endDateOnly = enddatetime.substring(0, 10);
     
      var dataTable = $('#Customecase_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'Custom Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Custom Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'Custom Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'ENSEMBLER \n Custom Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'Custom Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'Custom Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'Custom Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  fname:fname,
                  casee:casee,
                  agent:agent,
                  startdatetime:startdatetime,
                  enddatetime:enddatetime,
                  status:status,
                  category:category,
                  subcategory:subcategory,
                  comp:comp,
                  source:source,
                  action:'view_Customecase_report'
            }
         }
      });
   }
// Store initial values of date inputs when the document is ready
var initialStartDate;
var initialEndDate;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
});

$('#submit_Customecase').click(function(){
    var fname = $('#fname').val();
    var casee = $('#casee').val();      
    var agent = $('#agent').val();
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();      
    var status = $('#status').val();
    var category = $('#category').val();      
    var subcategory = $('#subcategory').val();
    var comp = $('#comp').val();      
    var source = $('#source').val();
    
    // Destroy DataTable instance to reinitialize with new data
    $('#Customecase_data').DataTable().destroy();

    // Call Custumcase_datatable function with appropriate filter parameters
    Custumcase_datatable(fname, casee, agent, startdatetime, enddatetime, status, category, subcategory, comp, source);
});

$('#reset_Customecase').click(function() {
    // Reset date inputs to initial values
    $('#sttartdatetime').val(initialStartDate);
    $('#enddatetime').val(initialEndDate);
    
    // Clear other filter inputs
    $('#fname, #casee, #agent, #status, #category, #subcategory, #comp, #source').val('');

    // Destroy DataTable instance and reload with default data
    $('#Customecase_data').DataTable().destroy();
    Custumcase_datatable();
});

// Disposition datatable
   disposition_datatable();
   function disposition_datatable(fname = '', CallerID = '', type = '', disposition = '', sentiment = '', startdatetime = '', enddatetime = ''){
        var startdatetime = $('#sttartdatetime').val();
        var enddatetime = $('#enddatetime').val();
        var startDateOnly = startdatetime.substring(0, 10);
        var endDateOnly = enddatetime.substring(0, 10);
       
      var dataTable = $('#disposition_data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'Disposition Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Disposition Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'Disposition Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'ENSEMBLER \n Disposition Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'Disposition Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'Disposition Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'Disposition Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  fname:fname,
                  CallerID:CallerID,
                  type:type,
                  disposition:disposition,
                  sentiment:sentiment,
                  startdatetime:startdatetime, 
                  enddatetime:enddatetime,
                  action:'generateReport'
            }
         }
      });
   }
// Store initial values of date inputs when the document is ready
var initialStartDate;
var initialEndDate;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
});

$('#submit_dis').click(function(){
    var fname = $('#fname').val();
    var CallerID = $('#CallerID').val();
    var type = $('#type').val();
    var disposition = $('#disposition').val();
    var sentiment = $('#sentiment').val();
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    
    // Destroy DataTable instance to reinitialize with new data
    $('#disposition_data').DataTable().destroy();

    // Call disposition_datatable function with appropriate filter parameters
    disposition_datatable(fname, CallerID, type, disposition, sentiment, startdatetime, enddatetime);
});

$('#reset_dis').click(function() {
    // Reset date inputs to initial values
    $('#sttartdatetime').val(initialStartDate);
    $('#enddatetime').val(initialEndDate);
    
    // Clear other filter inputs
    $('#fname, #CallerID, #type, #disposition, #sentiment').val('');

    // Destroy DataTable instance and reload with default data
    $('#disposition_data').DataTable().destroy();
    disposition_datatable();
});


});
// Close Data table code


// Report-> FAQ Report page datatable
faq_datatable();
function faq_datatable(){
    $('#faq_data').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: 'Report/report_function.php',
            type: 'POST',
            data: { action: 'view_frequent_questions' }
        },
        lengthMenu: [[10, 25, 50, 10000], [10, 25, 50, "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: 'FAQ Report',
                title: 'FAQ Report',
                exportOptions: {
                    columns: ':visible'
                }
            },{
                extend: 'excelHtml5',
                text: '<i class="fa fa-file-excel-o"></i>',
                titleAttr: 'Excel',
                filename: 'FAQ Report',
                title:'FAQ Report',
                exportOptions: {
                    columns: ':visible'
                }
            },{
                extend: 'pdfHtml5',
                filename : 'FAQ Report',
                messageTop : 'FAQ  Report',
                orientation: 'landscape',
                pageSize: 'A3',
                text: '<i class="fa fa-file-pdf-o"></i>',
                titleAttr: 'PDF',
                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: 'FAQ Report',
                postfixButtons: ['colvisRestore']
            },
        ],
        columns: [
            { data: 'question' },
            { data: 'count' }
        ]
    });
}


jQuery(function ($){
    var Report = {
        init: function (){
            jQuery("body").on('change', '#district', this.HandleSubCounty);//function to handle break delete request
            jQuery("body").on('change', '#category', this.HandleCategory);//function to handle break delete request

            // Initialize date time picker
             // $('.date_class').datetimepicker();
             // Function to clear default text in input field on focus
             function clearText(field){
                 if (field.defaultValue == field.value) field.value = '';
                 else if (field.value == '') field.value = field.defaultValue;
             }
        },
        HandleSubCounty: function (e) {
          e.preventDefault();
        // ajax script for getting  subcategory data
            var disID = $(this).val();
            var vilID = $('#village').val();
            // alert(catID);
              $.ajax({
                type:'POST',
                url:'Report/report_function.php',
                data:{'dis_id':disID,'vill_id':vilID,'action':'display_subcounty'},
                success:function(result){
                  $('#village').html(result);
                  
                }
            });
      },
      HandleCategory: function(e){
        e.preventDefault();
        var catID = $(this).val();
        var subID = $('#subcategory').val();
        // alert(catID);
          $.ajax({
            type:'POST',
            url:'helpdesk/web_ticket_function.php',
            data:{'cat_id':catID,'subcat_id':subID,'action':'ajax_subCategory'},
            success:function(result){
              $('#subcategory').html(result);
              
            }
          });
      }
    }
    Report.init();
});
// close

function case_datatable() {
    var startDate = $('#startDate').val();
    var endDate = $('#endDate').val();  
    var startDateOnly = startDate ? startDate.substring(0, 10) : '';
    var endDateOnly = endDate ? endDate.substring(0, 10) : '';
 
    var table = $('#caseDetails').DataTable({
        "processing": true,
        "serverSide": true,
        "destroy": true,
        "order": [],
        "pageLength": 10,
        "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "All"]],
        "searching": false,
        "paging": true,
        "ajax": {
    url: "Report/report_function.php",
    type: "POST",
    data: function (d) {
        d.startDate = startDate;
        d.endDate = endDate;
        d.loaneeNo      = $('#loaneeNo').val();
        d.phoneno       = $('#phoneno').val(); // selected value
        d.customerName  = $('#customerName').val();
        d.caseID        = $('#caseID').val();
        d.agentName     = $('#agentName').val();
        d.state         = $('#state').val();
        d.status        = $('#status').val();
        d.origin        = $('#origin').val();
        d.product       = $('#product').val();
        d.department    = $('#department').val();
        d.nationalid    = $('#nationalid').val();
        d.enquiryType   = $('#enquirytype').val();
        d.requestType   = $('#requesttype').val();
        d.vCaseType     = $('#vCaseType').val();

        d.action = 'view_case_reportt';
    }
},
        "columns": [
            {
                "className": 'details-control',
                "orderable": false,
                "data": null,
                "defaultContent": '<i class="fa fa-plus-square text-success" style="cursor:pointer;"></i>'
            },
            { "data": 1 },
            { "data": 2 },
            { "data": 3 },
            { "data": 4 },
            { "data": 5 },
            { "data": 6 },
            { "data": 7 },
            { "data": 8 },
            { "data": 9 },
            { "data": 10 },
            { "data": 11 },
            { "data": 12 },
            { "data": 13 }

        ],           
        dom: 'lBfrtip',
        "buttons": [
                {
                    extend: 'csvHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'CSV',
                    filename: 'Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                        extend: 'pdfHtml5',
                        filename : 'Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                        messageTop : 'Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                        orientation: 'landscape',
                        pageSize: 'A3',
                        text: '<i class="fa fa-file-pdf-o"></i>',
                        titleAttr: 'PDF',
                        customize: function ( doc ) {
                            var logoBase64El = document.getElementById('pdf-logo-base64');
                            if (logoBase64El) {
                                var logoBase64 = logoBase64El.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: 'Case Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
        "columnDefs": [
            { "targets": [0], "width": "5%" }
        ],
        "order": [[2, 'desc']],
        "createdRow": function (row, data) {
            $(row).attr('data-ticketid', data[0]);
        }
    });

    // Track expanded rows
    var expandedRows = {};
    // for total records update [vastvikta][11-03-2025]
$('#caseDetails').off('xhr.dt').on('xhr.dt', function(e, settings, json, xhr) {
    if (json && json.recordsTotal !== undefined) {
        $('#recordTopValue').text(json.recordsTotal);
        $('#recordBottomValue').text(json.recordsTotal);
    }
});


    // Expand/collapse logic
    $('#caseDetails tbody').off('click', 'td.details-control').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = table.row(tr);
        var ticketID = tr.data('ticketid');
        var icon = $(this).find('i');

        if (row.child.isShown()) {
            // Collapse this row
            row.child.hide();
            tr.removeClass('shown');
            icon.removeClass('fa-minus-square text-danger').addClass('fa-plus-square text-success');
            delete expandedRows[ticketID];
        } else {
            // If already expanded before, just re-show from memory
            if (expandedRows[ticketID]) {
                row.child(expandedRows[ticketID]).show();
                tr.addClass('shown');
                icon.removeClass('fa-plus-square text-success').addClass('fa-minus-square text-danger');
            } else {
                // Fetch details and expand
                fetch_case_details(ticketID, function (html) {
                    row.child(html).show();
                    tr.addClass('shown');
                    icon.removeClass('fa-plus-square text-success').addClass('fa-minus-square text-danger');
                    expandedRows[ticketID] = html; // store the expanded content
                });
            }
        }
    });

    // Redraw event — restore open rows
    table.on('draw', function () {
        for (var id in expandedRows) {
            var tr = $('#caseDetails tbody tr[data-ticketid="' + id + '"]');
            var row = table.row(tr);
            if (row) {
                row.child(expandedRows[id]).show();
                tr.addClass('shown');
                tr.find('td.details-control i')
                  .removeClass('fa-plus-square text-success')
                  .addClass('fa-minus-square text-danger');
            }
        }
    });
}

function fetch_case_details(ticketID, callback) {
    $.ajax({
        url: "Report/report_function.php",
        type: "POST",
        data: {
            action: "fetch_case_details",
            ticketid: ticketID
        },
        success: function (response) {
            let res = JSON.parse(response);
            let html = format_details_table(res.data);
            callback(html);
        }
    });
}


// Build nested comment table
function format_details_table(data) {
    let html = '<table class="tableview tableview-2 mb-0" style="background:#f9f9f9;">';
    html += '<thead style="font-size:12px"><tr><th>Product</th><th>Department</th><th>Enquiry Type</th><th>Request Type</th><th>Case Title</th></tr></thead><tbody>';
    if (data && data.length > 0) {
        data.forEach(c => {
            html += `<tr>
                <td>${c.productName}</td>
                <td>${c.department}</td>
                <td>${c.enquiryType}</td>
                <td>${c.requestType}</td>
                <td>${c.caseTitle}</td>
            </tr>`;
        });
    } else {
        html += `<tr><td colspan="3" class="text-center text-muted">No comments available</td></tr>`;
    }
    html += '</tbody></table>';
    return html;
}

// Store initial values of date inputs when document is ready
var initialStartDate;
var initialEndDate;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#startDate').val();
    initialEndDate = $('#endDate').val();

    // Initialize DataTable to set time out to  call function just upon  reloading [vastvikta][16-12-2024]
   setTimeout(function() {
    case_datatable();
    }, 1000);  // Calls case_datatable() every 1 second

});

// Submit button click handler
$('#submitCase').click(function() {

    // Destroy DataTable instance to reinitialize with new data
    $('#caseDetails').DataTable().destroy();
    
    // Call case_datatable function with appropriate filter parameters
    case_datatable();
});

// Reset button click handler
$('#resetCase').click(function() {
    // Reset date inputs to initial values
    $('#startDate').val(initialStartDate);
    $('#endDate').val(initialEndDate);

    // Clear other filter inputs
    // Reset text inputs
    $('#loaneeNo, #customerName, #caseID, #state ,#status, #origin').val('');

    // Reset dropdowns
    $('#agentName, #product, #department, #enquiryType, #requestType, #vCaseType')
        .val(null)
        .trigger('change');

    // Destroy existing DataTable if exists
    if ($.fn.DataTable.isDataTable('#caseDetails')) {
        $('#caseDetails').DataTable().destroy();
    }

    // Reload DataTable
    case_datatable();
});


function loadRequestTypes(department) {

    // reset child dropdowns
    $("#requesttype").html('<option value="0">Select</option>');
    $("#enquirytype").html('<option value="0">Select</option>');

    if (department === "0" || department === "") return;

    $.ajax({
        url: "helpdesk/web_ticket_function.php",
        type: "POST",
        data: { action: "getrequesttype", department: department },
        dataType: "json",
        success: function(response) {

            let options = '<option value="0">Select</option>';

            if (response.success && response.data.length > 0) {
                response.data.forEach(function(row) {
                    options += `<option value="${row.requestType}">${row.requestType}</option>`;
                });
            } else {
                options = '<option value="0">N/A</option>';
            }

            $("#requesttype").html(options);
        },
        error: function(xhr, status, error) {
            console.error("Error:", error);
        }
    });
}

// Also call after enquiry types are loaded dynamically
function loadEnquiryType(requestType) {
    if (requestType === "0" || requestType === "") {
        $("#enquirytype").html('<option value="">Select</option>');
        return;
    }

    $.ajax({
        url: "helpdesk/web_ticket_function.php",
        type: "POST",
        data: { action: "getenquiry", requestType: requestType },
        dataType: "json",
        success: function(response) {
            let options = '';
            if (response.success && response.data.length > 0) {
                response.data.forEach(function(dept) {
                    options += `<option value="${dept.enquiryType}">${dept.enquiryType}</option>`;
                });
            } else {
                options = '<option value="0">N/A</option>';
            }
            $("#enquirytype").html(options);
        },
        error: function(xhr, status, error) {
            console.error("Error:", error);
        }
    });
}
// new  ai suggestion usage report [vastvikta][30-01-2026]
agent_usage_datatable();
function agent_usage_datatable(agent = '', startdatetime = '', enddatetime = '',ai_model_used ='',channel_type = ''){
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var channel_type = $('#channel_type').val();
    var ai_model_used = $('#ai_model_used').val();
    var startDateOnly = startdatetime.substring(0, 10);
    var endDateOnly = enddatetime.substring(0, 10);
   
      var dataTable = $('#agent_usage').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "pageLength": 10,
            "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "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: 'Agent Usage Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Agent Usage Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'excelHtml5',
                    text: '<i class="fa fa-file-excel-o"></i>',
                    titleAttr: 'Excel',
                    filename: 'Agent Usage Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    title: 'Agent Usage Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    exportOptions: {
                        columns: ':visible'
                    }
                },{
                    extend: 'pdfHtml5',
                    filename : 'Agent Usage Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    messageTop : 'Agent Usage Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    orientation: 'landscape',
                    pageSize: 'A3',
                    text: '<i class="fa fa-file-pdf-o"></i>',
                    titleAttr: 'PDF',
                    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: 'Agent Usage Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                    postfixButtons: ['colvisRestore']
                },
            ],
         "ajax" : {
            url:"Report/report_function.php",
            type:"POST",
            data:{
                  agent:agent, 
                  startdatetime:startdatetime,
                  enddatetime:enddatetime,
                  ai_model_used:ai_model_used,
                  channel_type:channel_type,
                  action:'view_agent_usage'
            }
         }
      });
   }
  // Store initial values of date inputs and agent filter when document is ready
var initialStartDate;
var initialEndDate;
var initialAgent;

$(document).ready(function() {
    // Store initial values
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
    initialAgent = $('#agent').val();
});

$('#submit_agent_usage').click(function(){
    var agent = $('#agent').val();
    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();

    // Destroy DataTable instance to reinitialize with new data
    $('#agent_usage').DataTable().destroy();

    // Call audit_datatable function with appropriate filter parameters
    agent_usage_datatable(agent, startdatetime, enddatetime);
});

cf_datatable();

function cf_datatable(startdatetime = '', enddatetime = '', category = '', channel = '', satisfaction = '', rating = '', ticketid = '') {

    var startdatetime = $('#sttartdatetime').val();
    var enddatetime = $('#enddatetime').val();
    var category = $('#category').val();

    var startDateOnly = startdatetime ? startdatetime.substring(0, 10) : '';
    var endDateOnly = enddatetime ? enddatetime.substring(0, 10) : '';

    var dataTable = $('#cf_data').DataTable({
        "processing": true,
        "serverSide": true,
        "order": [],
        "pageLength": 10,
        "lengthMenu": [[10, 25, 50, 10000], [10, 25, 50, "All"]],
        "searching": false,
        "paging": true,
        "dom": "lBfrtip",
        buttons: [
            {
                extend: 'csvHtml5',
                text: '<i class="fa fa-file-excel-o"></i>',
                filename: 'Custom Feedback Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                title: 'Custom Feedback Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                exportOptions: { columns: ':visible' }
            },
            {
                extend: 'excelHtml5',
                text: '<i class="fa fa-file-excel-o"></i>',
                filename: 'Custom Feedback Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                title: 'Custom Feedback Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                exportOptions: { columns: ':visible' }
            },
            {
                extend: 'pdfHtml5',
                filename: 'Custom Feedback Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                messageTop: 'Custom Feedback Report FROM ' + startDateOnly + ' TO ' + endDateOnly,
                orientation: 'landscape',
                pageSize: 'A3',
                text: '<i class="fa fa-file-pdf-o"></i>',
                titleAttr: 'PDF',
                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',
                postfixButtons: ['colvisRestore']
            }
        ],
        "ajax": {
            url: "Report/report_function.php",
            type: "POST",
            data: {
                startdatetime: startdatetime,
                enddatetime: enddatetime,
                category: category,
                action: 'view_custom_feedback'
            }
        }
    });
}

/* STORE INITIAL VALUES */
var initialStartDate;
var initialEndDate;
var initialCategory;
$(document).ready(function () {
    initialStartDate = $('#sttartdatetime').val();
    initialEndDate = $('#enddatetime').val();
    initialCategory = $('#category').val();
});

/* RUN REPORT */
$('#submit_customer').click(function () {

    $('#cf_data').DataTable().destroy();

    cf_datatable();
});