<?php
include('includes/config/config.php');
include('includes/functions.php');
include('log_entry.php');
error_reporting(E_ERROR | E_PARSE);

$ohc_type = $_REQUEST['ohc_location'];
$patient_category = $_REQUEST['patient_category'];
$designation = $_REQUEST['designation'];
$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'];
$year = implode(',', $_REQUEST['year']);
$month = implode(',', $_REQUEST['month']);

$year2 = implode(',', $_REQUEST['year']);

$year1 = explode(',', $year);

// $year1 = array(2020,2021,2018);


error_log("values::" . $ohc_location . "   " . $patient_category . "  " . $designation . "   " . $emp_designation . "   " . $emp_cadre . "  " . $employer_contractor . "   " . $gender . "   " . $start_date . "   " . $end_date . "  " . $year . "  " . $month);


$qry_filter1 = "";
$qry_filter = "";
$qry_date = "";
$qry_date1 = "";

if (isset($designation) && $designation != '') {
    $qry_filter1 .= " where designation_id ='" . $designation . "' ";
}


if (isset($ohc_type) && $ohc_type != '') {
    $qry_filter .= " and ohc_type_id ='" . $ohc_type . "' ";
}

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

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

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

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

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

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

// if (isset($month) && $month != '') {
//     $qry_date1 .= " and month(medical_entry_date) in (" . $month . ")";
// }


$i = 0;
$record = array();

if (isset($year) && $year != '' && sizeof($year1) > 1) {

    $designation_query = "select designation_id, designation_name from designation $qry_filter1";

    $result_designation = mysqli_query($conn, $designation_query);

    $percentage_emp_per_designation = 0;

    $total = 0;

    while ($designation_row = mysqli_fetch_assoc($result_designation)) {

        for ($j = 0; $j < sizeof($year1); $j++) {
            $qry_date = "";
            $qry_doj = "";

            $qry_date .= " and year(medical_entry_date) ='" . $year1[$j] . "'";
            $qry_doj .= " and year(doj) <= '" . $year1[$j] . "'";

            $count_query = "select count(*) as total FROM patient_master where status = 'Active' and designation_id = '" . $designation_row['designation_id'] . "' $qry_filter $qry_doj";

            $count_result = mysqli_query($conn, $count_query);

            error_log("count_query::" . $count_query);

            $row_count = mysqli_fetch_assoc($count_result);

            $count_audiometry = "select max(medical_exam_id),patient_id from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and designation_id = '" . $designation_row['designation_id'] . "' $qry_filter $qry_doj ORDER by id asc) $qry_date1 $qry_date and audio_findings NOT LIKE 'WNL%'  GROUP BY patient_id ORDER BY patient_id asc";

            $audiometry_result = mysqli_query($conn, $count_audiometry);
            error_log("count_audiometry::" . $count_audiometry);

            if (mysqli_num_rows($audiometry_result) > 0) {
                $total = mysqli_num_rows($audiometry_result);
            } else {
                $total = 0;
            }

            error_log("ftotal:" . $total);

            if ($total == 0 || $row_count['total'] == 0) {
                $record[$designation_row['designation_name']][$year1[$j]]['audiometry'] = 0.0;
            } else {

                $percentage_emp_per_designation = ($total / $row_count['total']) * 100;

                $record[$designation_row['designation_name']][$year1[$j]]['audiometry'] = round($percentage_emp_per_designation, 2);
            }
        }
    }
} else {
    if (isset($year2) && $year2 != '') {
        $qry_date .= " and year(medical_entry_date) ='" . $year2 . "'";
        $qry_filter .= " and year(doj) <= '" . $year2 . "'";
    }

    $designation_query = "select designation_id, designation_name from designation $qry_filter1";

    $result_designation = mysqli_query($conn, $designation_query);
    $total = 0;
    while ($designation_row = mysqli_fetch_assoc($result_designation)) {

        $count_query = "select count(*) as total FROM patient_master where status = 'Active' and designation_id = '" . $designation_row['designation_id'] . "' $qry_filter";

        $count_result = mysqli_query($conn, $count_query);

        error_log("count_query::" . $count_query);

        if (mysqli_num_rows($count_result) > 0) {
            $row_count = mysqli_fetch_assoc($count_result);
            $record[$i]['key'] = $designation_row['designation_name'];
            $record[$i]['total'] = $row_count['total'];
        }

        $count_audiometry = "select max(medical_exam_id),patient_id from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and designation_id = '" . $designation_row['designation_id'] . "' $qry_filter  ORDER by id asc) $qry_date1 $qry_date and audio_findings NOT LIKE 'WNL%' GROUP BY patient_id ORDER BY patient_id asc";

        $audiometry_result = mysqli_query($conn, $count_audiometry);
        error_log("count_audiometry::" . $count_audiometry);

        if (mysqli_num_rows($audiometry_result) > 0) {
            $total = mysqli_num_rows($audiometry_result);
        } else {
            $total = 0;
        }
        $record[$i]['audiometry_total'] = $total;
        $i++;
    }
}

echo json_encode($record);