<?php  
include('includes/config/config.php');
include('log_entry.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 checkup_date >='".$start_date."' ";
//  $qry_date.=" and 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') ";
// }
$i=0;
$record =array();

// get the last 30 days of from today
$i = 0;
 $week_count  = 10;
$record = Array();
$today     = new DateTime(); // today
$begin     = $today->sub(new DateInterval('P10W')); //created 30 days interval back
$end       = new DateTime();
$end       = $end->modify('+7 day'); // interval generates upto last day
$interval  = new DateInterval('P1W'); // 1d interval range
$daterange = new DatePeriod($begin, $interval, $end); // it always runs forwards in date

foreach ($daterange as $date) { // date object
    $new_week = date("W", strtotime($date->format("Y-m-d"))); // your date
   
    // get covid confirm case in a week
    $query="SELECT count(a.emp_id) as confirm_case FROM covid_monitoring a left join patient_master b on a.emp_id = b.id WHERE YEARWEEK(a.date_added) = YEARWEEK(NOW() - INTERVAL ".$week_count." WEEK) and a.covid_test_result_id ='2' $qry_bu group by week(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]['confirm_case']=$row['confirm_case'];
            $record[$i]['week'] = $new_week;   
        }
    } else {
        $record[$i]['confirm_case'] = 0;
        $record[$i]['week'] = $new_week;
    }
    $quarantine_query="SELECT count(a.emp_id) as quarantine_case FROM covid_monitoring  a left join patient_master b on a.emp_id = b.id  WHERE YEARWEEK(a.date_added) = YEARWEEK(NOW() - INTERVAL $week_count WEEK) $qry_bu group by week(a.date_added)";
    // echo $query;
    // error_log('query: '.$query);
    if (!$quarantine_result = @mysqli_query($conn,$quarantine_query)) {
        die(mysqli_error($conn));
    }
    
    if(mysqli_num_rows($quarantine_result) > 0) {
        while ($row = @mysqli_fetch_assoc($quarantine_result)) {
            $record[$i]['quarantine_case']=$row['quarantine_case'];
            $record[$i]['week'] = $new_week;   
        }
    } else {
        $record[$i]['quarantine_case'] = 0;
        $record[$i]['week'] = $new_week;
    }
    $discharge_query="SELECT count(a.emp_id) as discharge FROM covid_monitoring a left join patient_master b on a.emp_id = b.id  WHERE YEARWEEK(a.discharge_on) = YEARWEEK(NOW() - INTERVAL $week_count WEEK) $qry_bu group by week(a.discharge_on)";
    // echo $discharge_query;
    // error_log('discharge_query: '.$discharge_query);
    if (!$discharge_result = @mysqli_query($conn,$discharge_query)) {
        die(mysqli_error($conn));
    }
    
    if(mysqli_num_rows($discharge_result) > 0) {
        while ($row = @mysqli_fetch_assoc($discharge_result)) {
            $record[$i]['discharge']=$row['discharge'];
            $record[$i]['week'] = $new_week;   
        }
    } else {
        $record[$i]['discharge'] = 0;
        $record[$i]['week'] = $new_week;
    }
    
    $week_count--;  
    $i++;
}   // error_log(print_r($record, true));
    
 echo json_encode($record);
?>