function dateDiffInDays(a) {
   const _MS_PER_DAY = 1000 * 60 * 60 * 24;
   return Math.floor((Date.now() - new Date(a)) / _MS_PER_DAY);
}

$(function() {
 $button.click(function () {
   $data = $table.bootstrapTable('getData')
   $dl = $data.length
   $nSCCM=0; $nAD=0; $nGLPI=0;$nEPO=0;$nNBU=0;$nOS=0;$nSCOM=0;$nZabbix=0;$nS1=0;
   for( $i=0 ; $i < $dl ; $i++ ){
      if ($data[$i]['AD'] !== 'Missing') { $nAD++ ; }
      if ((dateDiffInDays($data[$i]['ADlu'].split(" ")[0]) > 45 || $data[$i]['ADlu'] == 'Missing') && $data[$i]['AD'] !== 'Missing'){ $nAD-- ;}
      if ($data[$i]['SCCM'] !== 'Missing') { $nSCCM++ ; }
      if ((dateDiffInDays($data[$i]['SCCMlu'].split(" ")[0]) > 7 || $data[$i]['SCCMlu'] == 'Missing') && $data[$i]['SCCM'] !== 'Missing'){ $nSCCM-- ;}
      if ($data[$i]['GLPI'] !== 'Missing') { $nGLPI++ ; }
      if ((dateDiffInDays($data[$i]['GLPIlu'].split(" ")[0]) > 7 || $data[$i]['GLPIlu'] == 'Missing') && $data[$i]['GLPI'] !== 'Missing'){ $nGLPI-- ;}
      if ($data[$i]['NESSUS'] !== 'Missing') { $nEPO++ ; }
      if ($data[$i]['Backup'] !== 'Missing') { $nNBU++ ; }
      if ((dateDiffInDays($data[$i]['NBUlu'].split(" ")[0]) > 30 || $data[$i]['NBUlu'] == 'Missing') && $data[$i]['Backup'].split(" ")[0] !== 'Missing'){ $nNBU-- ;}
      if (($data[$i]['OS'].includes('XP') || $data[$i]['OS'].includes('2000') || $data[$i]['OS'].includes('2003') || $data[$i]['OS'].includes('2008') || $data[$i]['OS'].includes('2012') || $data[$i]['OS'].includes('7') || $data[$i]['OS'].includes('Windows NT')) == false && $data[$i]['OS'] !=='&nbsp;') { $nOS++ ;}
      if ($data[$i]['SCOM'] !== 'Missing') { $nSCOM++ ; }
      if ($data[$i]['Zabbix'] !== 'Missing') { $nZabbix++ ; }
      if ($data[$i]['S1'] !== 'Missing') { $nS1++ ; }
      if ((dateDiffInDays($data[$i]['S1lu'].split(" ")[0]) > 7 || $data[$i]['S1lu'] == 'Missing') && $data[$i]['S1'] !== 'Missing'){ $nS1-- ;}
   }
   
   document.getElementById("KPIAD").innerHTML = Math.round($nAD/$dl*1000)/10 + "%";
   if(Math.round($nAD/$dl*100) >=90){ 
      document.getElementById("KPIAD").className = "badge text-bg-success font-weight-bold";
   }else if(Math.round($nAD/$dl*100) >=75){
      document.getElementById("KPIAD").className = "badge text-bg-warning font-weight-bold";
   }else{
      document.getElementById("KPIAD").className = "badge text-bg-danger font-weight-bold";
   } 

   document.getElementById("KPIGLPI").innerHTML = Math.round($nGLPI/$dl*1000)/10 + "%";
   if(Math.round($nGLPI/$dl*100) >=90){ 
      document.getElementById("KPIGLPI").className = "badge text-bg-success font-weight-bold";   
   }else if(Math.round($nGLPI/$dl*100) >=75){
      document.getElementById("KPIGLPI").className = "badge text-bg-warning font-weight-bold";
   }else{
      document.getElementById("KPIGLPI").className = "badge text-bg-danger font-weight-bold";
   } 

   document.getElementById("KPISCCM").innerHTML = Math.round($nSCCM/$dl*1000)/10 + "%";
   if(Math.round($nSCCM/$dl*100) >=90){ 
      document.getElementById("KPISCCM").className = "badge text-bg-success font-weight-bold";   
   }else if(Math.round($nSCCM/$dl*100) >=75){
      document.getElementById("KPISCCM").className = "badge text-bg-warning font-weight-bold";
   }else{
      document.getElementById("KPISCCM").className = "badge text-bg-danger font-weight-bold";
   } 

   document.getElementById("KPIEPO").innerHTML = Math.round($nEPO/$dl*1000)/10 + "%";
   if(Math.round($nEPO/$dl*100) >=90){ 
      document.getElementById("KPIEPO").className = "badge text-bg-success font-weight-bold";   
   }else if(Math.round($nEPO/$dl*100) >=75){
      document.getElementById("KPIEPO").className = "badge text-bg-warning font-weight-bold";
   }else{
      document.getElementById("KPIEPO").className = "badge text-bg-danger font-weight-bold";
   } 

   document.getElementById("KPINBU").innerHTML = Math.round($nNBU/$dl*1000)/10 + "%";
   if(Math.round($nNBU/$dl*100) >=90){ 
      document.getElementById("KPINBU").className = "badge text-bg-success font-weight-bold";   
   }else if(Math.round($nNBU/$dl*100) >=75){
      document.getElementById("KPINBU").className = "badge text-bg-warning font-weight-bold";
   }else{
      document.getElementById("KPINBU").className = "badge text-bg-danger font-weight-bold";
   } 

   document.getElementById("KPIOS").innerHTML = Math.round($nOS/$dl*1000)/10 + "%";
   if(Math.round($nOS/$dl*100) >=90){ 
      document.getElementById("KPIOS").className = "badge text-bg-success font-weight-bold";   
   }else if(Math.round($nOS/$dl*100) >=75){
      document.getElementById("KPIOS").className = "badge text-bg-warning font-weight-bold";
   }else{
      document.getElementById("KPIOS").className = "badge text-bg-danger font-weight-bold";
   } 

   document.getElementById("KPISCOM").innerHTML = Math.round($nSCOM/$dl*1000)/10 + "%";
   if(Math.round($nSCOM/$dl*100) >=90){ 
      document.getElementById("KPISCOM").className = "badge text-bg-success font-weight-bold";   
   }else if(Math.round($nSCOM/$dl*100) >=75){
      document.getElementById("KPISCOM").className = "badge text-bg-warning font-weight-bold";
   }else{
      document.getElementById("KPISCOM").className = "badge text-bg-danger font-weight-bold";
   } 

   document.getElementById("KPIzabbix").innerHTML = Math.round($nZabbix/$dl*1000)/10 + "%";
   if(Math.round($nZabbix/$dl*100) >=90){ 
      document.getElementById("KPIzabbix").className = "badge text-bg-success font-weight-bold";   
   }else if(Math.round($nZabbix/$dl*100) >=75){
      document.getElementById("KPIzabbix").className = "badge text-bg-warning font-weight-bold";
   }else{
      document.getElementById("KPIzabbix").className = "badge text-bg-danger font-weight-bold";
   } 

   document.getElementById("KPIS1").innerHTML = Math.round($nS1/$dl*1000)/10 + "%";
   if(Math.round($nS1/$dl*100) >=90){ 
      document.getElementById("KPIS1").className = "badge text-bg-success font-weight-bold";   
   }else if(Math.round($nS1/$dl*100) >=75){
      document.getElementById("KPIS1").className = "badge text-bg-warning font-weight-bold";
   }else{
      document.getElementById("KPIS1").className = "badge text-bg-danger font-weight-bold";
   } 

   document.getElementById("KPIDEVICE").innerHTML = $dl;
})
})