function getTotalValue(array,toSum){
  let total = 0;
  if(array.length > 0){
    array.map((list)=>{
      total+=Number(list[toSum]);
    });
  }
  return total;
}
$(document).ready(function () {
    var reportValue=[];
    var tbodyValue = '';
    let fromd;
    let tod;
    let shift;
    $("#calender").click(function () {
      $(".spindis").show();
      fromd= $("#input_from").val();
      tod= $("#input_to").val();
      shift= $("#shift").val();
      customer = $("#customer").val();
      //var timeFrom = $("#timefrom").val();
      //var timeTo = $("#timeto").val();
      //var tktFrom = $("#tktfrom").val();
        var vehicle = $("#vehicle").val();
        var modeTrans = $("#modeTrans").val();
        $.ajax({
          type: "GET",
          url: "ajaxreport1/fetch_transaction_age_report.php",
          dataType:"json",
          data: { tod: tod, fromd: fromd, shift:shift, customer:customer},
          success: function (data) {
            console.log(data);
            console.log(data.agewise_detailed);
            data.agewise_detailed.map((list)=>{
                reportValue.push(list);
           });
           tbodyValue = `<h3>Age Report for (${fromd} to ${tod})</h3>`;
          let limitedReport= data.agewise_detailed;
         let hideFields = [];
         let sumFields = ['total-amount','paid-amount','pending-amount'];
         
         if(data.agewise_summary) {
            tbodyValue += createTableWithJson('Age Report',[data.agewise_summary],[],[]);
         }
         if(data.agewise_detailed.length > 0) {
            tbodyValue += createTableWithJson('Age Report',data.agewise_detailed,sumFields,hideFields);
         }
           setTimeout(()=>{$("#tbodyReport").html(tbodyValue);
           $(".spindiv").hide();
           $("#input_from").val("");
           $("#input_to").val("");},2000)
          },
        });
        $("#modaldis").click();
        console.log(reportValue);
    });
    $("#excel").click(function(){
      let tableValue = document.getElementById("tbodyReport");
    let stringedTable =tableValue.innerHTML;
    let borderedTable1 = stringedTable.replaceAll(`<th style="`,`<th style="border:1px solid black;`);
    let borderedTable = borderedTable1.replaceAll(`<table>`,`<table style="border-collapse:collapse">`);
    let borderedTh = borderedTable.replaceAll(`<th>`,`<th style="border:1px solid black">`);
    let borderedTd = borderedTh.replaceAll(`<td>`,`<td style="border:1px solid black">`);
    console.log('stringedTable',borderedTd);
            var uri = 'data:text/csv;charset=utf-8,' + escape(borderedTd);
    
      // Now the little tricky part.
      // you can use either>> window.open(uri);
      // but this will not work in some browsers
      // or you will not get the correct file extension    
      
      //this trick will generate a temp <a /> tag
      var link = document.createElement("a");    
      link.href = uri;
      
      //set the visibility hidden so it will not effect on your web-layout
      link.style = "visibility:hidden";
      link.download = 'Overall Report for -(' + fromd + ' to ' + tod + ") .xls";
      
      //this part will append the anchor tag and remove it after automatic click
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
       });
  });
  