<?php  
// include('pdf_header.php');
include ('includes/config/config.php');
include ('includes/functions.php');
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename=excel_covid_active_new.xls');
?>
<body>
<table border="1" width="100%">
<?php  
include('includes/config/config.php');
error_reporting(E_ERROR | E_PARSE);
$ohc_location = $_REQUEST['ohc_location'];
$patient_category = $_REQUEST['patient_category'];
$dept = $_REQUEST['dept'];
$emp_designation = $_REQUEST['emp_designation'];
$emp_cadre = $_REQUEST['emp_cadre'];
$employer_contractor = $_REQUEST['employer_contractor'];
$gender = $_REQUEST['gender'];


$start_date = $_REQUEST['startDate'];
$end_date= $_REQUEST['endDate'];



$qry_bu ="";
$qry_date="";

if(isset($ohc_location) && $ohc_location!=''){
	$qry_bu.=" and b.ohc_type_id ='".$ohc_location."' ";
}


if(isset($patient_category) && $patient_category!=''){
	$qry_bu.=" and b.emp_cat_id ='".$patient_category."' ";
}

if(isset($dept) && $dept!=''){
	$qry_bu.=" and b.dept_id ='".$dept."' ";
}

if(isset($emp_designation) && $emp_designation!=''){
	$qry_bu.=" and b.designation_id ='".$emp_designation."' ";
}

if(isset($emp_cadre) && $emp_cadre!=''){
	$qry_bu.=" and b.emp_cadre ='".$emp_cadre."' ";
}

if(isset($employer_contractor) && $employer_contractor!=''){
	$qry_bu.=" and b.employer_contractor_id ='".$employer_contractor."' ";
}

if(isset($gender) && $gender!=''){
	$qry_bu.=" and b.gender ='".$gender."' ";
}





if(isset($start_date) && $start_date!=''){
	//$qry_date.=" and b.checkup_date >='".$start_date."' ";
	$qry_date.=" and b.medical_entry_date >=str_to_date('".$start_date."','%d-%m-%Y') ";
	
	
	
}
if(isset($end_date) && $end_date!=''){
	//$qry_date.=" and checkup_date <='".$end_date."' ";
$qry_date.=" and medical_entry_date <=str_to_date('".$end_date."','%d-%m-%Y') ";
}


    
	
	?>
	
	<tr>
			<div style="font-size: 12px">
				<td width="25%" align="left"> Run Date : <?php   echo date("d-M-Y"); ?></td>
				<td width="50%" align="center"><strong>Covid Active vs New Case</strong></td>
				<td  align="left"> User : <?php   echo $username ?></td>
				
		</tr>

		<tr style="background: #eeeeee;"><td><strong>Date</strong></td><td><strong>Total Patient</strong></td><td><strong>New Patient</strong></td></tr>


	<?php  
 // }
$i=0;
$record =array();

// get the last 30 days of from today
$today     = new DateTime(); // today
$begin     = $today->sub(new DateInterval('P7D')); //created 30 days interval back
$end       = new DateTime();
$end       = $end->modify('+1 day'); // interval generates upto last day
$interval  = new DateInterval('P1D'); // 1d interval range
$daterange = new DatePeriod($begin, $interval, $end); // it always runs forwards in date
$total_patient_count = 0;
foreach ($daterange as $date) { // date object
    $new_date = $date->format("Y-m-d"); // your date
    $query="select count(a.emp_id) as todays_case from covid_monitoring a left join patient_master b on a.emp_id = b.id where a.covid_test_result_id = '2' and a.date_added = '$new_date' $qry_bu group by a.date_added ";
    // echo $query;
    error_log('query: '.$query);
    if (!$result = @mysqli_query($conn,$query)) {
        die(mysqli_error($conn));
    }
    
    if(mysqli_num_rows($result) > 0) {
        while ($row = @mysqli_fetch_assoc($result)) {
            $record[$i]['todays_case']=$row['todays_case'];
            $total_patient_count += $row['todays_case'];
            $record[$i]['total_case']=$total_patient_count;
            $record[$i]['date'] = $date->format("d-m-Y");
            ?><tr><td><?php  echo $date->format("d-m-Y");?></td><td><?php  echo $total_patient_count?></td><td><?php  echo $row['todays_case']?></td></tr><?php  
            $i++;
        }
    } else {
        $record[$i]['todays_case'] = 0;
        $total_patient_count += 0;
        $record[$i]['total_case']=$total_patient_count;
        $record[$i]['date'] = $date->format("d-m-Y");
        $i++; 
        ?><tr><td><?php  echo $date->format("d-m-Y");?></td><td><?php  echo $total_patient_count?></td><td>0</td></tr><?php  
    }
    

}

?>

</table>
</body>