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

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

$year1 = explode(',', $year);
// $year1 = array(2020,2021);


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



$qry_filter = "";

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


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

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


$data = array();

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

    for ($j = 0; $j < count($year1); $j++) {

        error_log("year:" . $year1[$j] . " size:" . count($year1));
        $qry_date = "";
        $qry_dob = "";

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

        //for 0 to 2
        $total = 0;

        $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=0 and (datediff(CURRENT_DATE,doj)/365)<2 and status='Active' $qry_filter $qry_dob";

        $pat_result = mysqli_query($conn, $pat_query);
        if (mysqli_num_rows($pat_result)) {
            $total = mysqli_num_rows($pat_result);
        } else {
            $total = 0;
        }

        $per_for_0_to_2 = [];
        $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=0 and (datediff(CURRENT_DATE,doj)/365)<2 and status='Active' $qry_filter $qry_dob) $qry_date1 $qry_date GROUP by emp_id";

        error_log("count health_index:" . $count_health_index);
        $health_index_result = mysqli_query($conn, $count_health_index);

        $i = 0;
        if (mysqli_num_rows($health_index_result)) {
            while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
                $per_for_0_to_2[$i] = $row_health_index['health_index'];
                $i++;
            }
        }

        $avg = 0;
        if (count($per_for_0_to_2)) {
            $avg = round(array_sum($per_for_0_to_2) / count($per_for_0_to_2), 2);
        }

        $data['0-2'][$year1[$j]]['total'] = $avg;

        // for 2 to 5
        $total = 0;

        $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=2 and (datediff(CURRENT_DATE,doj)/365)<5 and status='Active' $qry_filter $qry_dob";

        $pat_result = mysqli_query($conn, $pat_query);
        if (mysqli_num_rows($pat_result)) {
            $total = mysqli_num_rows($pat_result);
        } else {
            $total = 0;
        }

        $per_for_2_to_5 = [];
        $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=2 and (datediff(CURRENT_DATE,doj)/365)<5 and status='Active' $qry_filter $qry_dob) $qry_date1 $qry_date GROUP by emp_id";

        error_log("count health_index:" . $count_health_index);
        $health_index_result = mysqli_query($conn, $count_health_index);

        $i = 0;
        if (mysqli_num_rows($health_index_result)) {
            while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
                $per_for_2_to_5[$i] = $row_health_index['health_index'];
                $i++;
            }
        }

        $avg = 0;
        if (count($per_for_2_to_5)) {
            $avg = round(array_sum($per_for_2_to_5) / count($per_for_2_to_5), 2);
        }

        $data['2-5'][$year1[$j]]['total'] = $avg;

        // for 5 to 10
        $total = 0;

        $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=5 and (datediff(CURRENT_DATE,doj)/365)<10 and status='Active' $qry_filter $qry_dob";

        $pat_result = mysqli_query($conn, $pat_query);
        if (mysqli_num_rows($pat_result)) {
            $total = mysqli_num_rows($pat_result);
        } else {
            $total = 0;
        }

        $per_for_5_to_10 = [];
        $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=5 and (datediff(CURRENT_DATE,doj)/365)<10 and status='Active' $qry_filter $qry_dob) $qry_date1 $qry_date GROUP by emp_id";

        error_log("count health_index:" . $count_health_index);
        $health_index_result = mysqli_query($conn, $count_health_index);

        $i = 0;
        if (mysqli_num_rows($health_index_result)) {
            while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
                $per_for_5_to_10[$i] = $row_health_index['health_index'];
                $i++;
            }
        }

        $avg = 0;
        if (count($per_for_5_to_10)) {
            $avg = round(array_sum($per_for_5_to_10) / count($per_for_5_to_10), 2);
        }
        $data['5-10'][$year1[$j]]['total'] = $avg;

        //for 10 to 15

        $total = 0;

        $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=10 and (datediff(CURRENT_DATE,doj)/365)<15 and status='Active' $qry_filter $qry_dob";

        $pat_result = mysqli_query($conn, $pat_query);
        if (mysqli_num_rows($pat_result)) {
            $total = mysqli_num_rows($pat_result);
        } else {
            $total = 0;
        }

        $per_for_10_to_15 = [];
        $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=10 and (datediff(CURRENT_DATE,doj)/365)<15 and status='Active' $qry_filter $qry_dob) $qry_date1 $qry_date GROUP by emp_id";

        error_log("count health_index:" . $count_health_index);
        $health_index_result = mysqli_query($conn, $count_health_index);

        $i = 0;
        if (mysqli_num_rows($health_index_result)) {
            while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
                $per_for_10_to_15[$i] = $row_health_index['health_index'];
                $i++;
            }
        }

        $avg = 0;
        if (count($per_for_10_to_15)) {
            $avg = round(array_sum($per_for_10_to_15) / count($per_for_10_to_15), 2);
        }
        $data['10-15'][$year1[$j]]['total'] = $avg;

        // for 15 to 20

        $total = 0;

        $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=15 and (datediff(CURRENT_DATE,doj)/365)<20 and status='Active' $qry_filter $qry_dob";

        $pat_result = mysqli_query($conn, $pat_query);
        if (mysqli_num_rows($pat_result)) {
            $total = mysqli_num_rows($pat_result);
        } else {
            $total = 0;
        }

        $per_for_15_to_20 = [];
        $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=15 and (datediff(CURRENT_DATE,doj)/365)<20 and status='Active' $qry_filter $qry_dob) $qry_date1 $qry_date GROUP by emp_id";

        error_log("count health_index:" . $count_health_index);
        $health_index_result = mysqli_query($conn, $count_health_index);

        $i = 0;
        if (mysqli_num_rows($health_index_result)) {
            while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
                $per_for_15_to_20[$i] = $row_health_index['health_index'];
                $i++;
            }
        }

        $avg = 0;
        if (count($per_for_15_to_20)) {
            $avg = round(array_sum($per_for_15_to_20) / count($per_for_15_to_20), 2);
        }
        $data['15-20'][$year1[$j]]['total'] = $avg;

        // for 20 to 25

        $total = 0;

        $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=20 and (datediff(CURRENT_DATE,doj)/365)<25 and status='Active' $qry_filter $qry_dob";

        $pat_result = mysqli_query($conn, $pat_query);
        if (mysqli_num_rows($pat_result)) {
            $total = mysqli_num_rows($pat_result);
        } else {
            $total = 0;
        }

        $per_for_20_to_25 = [];
        $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=20 and (datediff(CURRENT_DATE,doj)/365)<25 and status='Active' $qry_filter $qry_dob) $qry_date1 $qry_date GROUP by emp_id";

        error_log("count health_index:" . $count_health_index);
        $health_index_result = mysqli_query($conn, $count_health_index);

        $i = 0;
        if (mysqli_num_rows($health_index_result)) {
            while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
                $per_for_20_to_25[$i] = $row_health_index['health_index'];
                $i++;
            }
        }

        $avg = 0;
        if (count($per_for_20_to_25)) {
            $avg = round(array_sum($per_for_20_to_25) / count($per_for_20_to_25), 2);
        }

        $data['20-25'][$year1[$j]]['total'] = $avg;


        //for greater_than_equal_to_25
        $total = 0;

        $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=25 and status='Active' $qry_filter $qry_dob";

        $pat_result = mysqli_query($conn, $pat_query);
        if (mysqli_num_rows($pat_result)) {
            $total = mysqli_num_rows($pat_result);
        } else {
            $total = 0;
        }

        $per_for_greater_than_equal_to_25 = [];
        $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=25 and status='Active' $qry_filter $qry_dob) $qry_date1 $qry_date GROUP by emp_id";

        error_log("count health_index:" . $count_health_index);
        $health_index_result = mysqli_query($conn, $count_health_index);

        $i = 0;
        if (mysqli_num_rows($health_index_result)) {
            while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
                $per_for_greater_than_equal_to_25[$i] = $row_health_index['health_index'];
                $i++;
            }
        }

        $avg = 0;
        if (count($per_for_greater_than_equal_to_25)) {
            $avg = round(array_sum($per_for_greater_than_equal_to_25) / count($per_for_greater_than_equal_to_25), 2);
        }
        $data['>=50'][$year1[$j]]['total'] = $avg;
    }
} else {
    if (isset($year2) && $year2 != '') {
        $qry_date1 .= " and year(checkup_date) ='" . $year2 . "'";
        $qry_dob .= "and year(doj) <= '" . $year2 . "'";
    }

    //for 0 to 2
    $total = 0;

    $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=0 and (datediff(CURRENT_DATE,doj)/365)<2 and status='Active' $qry_filter $qry_dob";

    $pat_result = mysqli_query($conn, $pat_query);
    if (mysqli_num_rows($pat_result)) {
        $total = mysqli_num_rows($pat_result);
    } else {
        $total = 0;
    }

    $per_for_0_to_2 = [];
    $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=0 and (datediff(CURRENT_DATE,doj)/365)<2 and status='Active' $qry_filter $qry_dob) $qry_date1 GROUP by emp_id";

    error_log("count health_index:" . $count_health_index);
    $health_index_result = mysqli_query($conn, $count_health_index);

    $i = 0;
    if (mysqli_num_rows($health_index_result)) {
        while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
            $per_for_0_to_2[$i] = $row_health_index['health_index'];
            $i++;
        }
    }

    $avg = 0;
    if (count($per_for_0_to_2)) {
        $avg = round(array_sum($per_for_0_to_2) / count($per_for_0_to_2), 2);
    }

    $data['exp_0_2']['avg'] = $avg;
    $data['exp_0_2']['gtotal'] = $total;

    // for 2 to 5
    $total = 0;

    $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=2 and (datediff(CURRENT_DATE,doj)/365)<5 and status='Active' $qry_filter $qry_dob";

    $pat_result = mysqli_query($conn, $pat_query);
    if (mysqli_num_rows($pat_result)) {
        $total = mysqli_num_rows($pat_result);
    } else {
        $total = 0;
    }

    $per_for_2_to_5 = [];
    $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=2 and (datediff(CURRENT_DATE,doj)/365)<5 and status='Active' $qry_filter $qry_dob) $qry_date1 GROUP by emp_id";

    error_log("count health_index:" . $count_health_index);
    $health_index_result = mysqli_query($conn, $count_health_index);

    $i = 0;
    if (mysqli_num_rows($health_index_result)) {
        while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
            $per_for_2_to_5[$i] = $row_health_index['health_index'];
            $i++;
        }
    }

    $avg = 0;
    if (count($per_for_2_to_5)) {
        $avg = round(array_sum($per_for_2_to_5) / count($per_for_2_to_5), 2);
    }

    $data['exp_2_5']['avg'] = $avg;
    $data['exp_2_5']['gtotal'] = $total;


    // for 5 to 10
    $total = 0;

    $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=5 and (datediff(CURRENT_DATE,doj)/365)<10 and status='Active' $qry_filter $qry_dob";

    $pat_result = mysqli_query($conn, $pat_query);
    if (mysqli_num_rows($pat_result)) {
        $total = mysqli_num_rows($pat_result);
    } else {
        $total = 0;
    }

    $per_for_5_to_10 = [];
    $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=5 and (datediff(CURRENT_DATE,doj)/365)<10 and status='Active' $qry_filter $qry_dob) $qry_date1 GROUP by emp_id";

    error_log("count health_index:" . $count_health_index);
    $health_index_result = mysqli_query($conn, $count_health_index);

    $i = 0;
    if (mysqli_num_rows($health_index_result)) {
        while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
            $per_for_5_to_10[$i] = $row_health_index['health_index'];
            $i++;
        }
    }

    $avg = 0;
    if (count($per_for_5_to_10)) {
        $avg = round(array_sum($per_for_5_to_10) / count($per_for_5_to_10), 2);
    }

    $data['exp_5_10']['avg'] = $avg;
    $data['exp_5_10']['gtotal'] = $total;

    //for 10 to 15

    $total = 0;

    $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=10 and (datediff(CURRENT_DATE,doj)/365)<15 and status='Active' $qry_filter $qry_dob";

    $pat_result = mysqli_query($conn, $pat_query);
    if (mysqli_num_rows($pat_result)) {
        $total = mysqli_num_rows($pat_result);
    } else {
        $total = 0;
    }

    $per_for_10_to_15 = [];
    $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=10 and (datediff(CURRENT_DATE,doj)/365)<15 and status='Active' $qry_filter $qry_dob) $qry_date1 GROUP by emp_id";

    error_log("count health_index:" . $count_health_index);
    $health_index_result = mysqli_query($conn, $count_health_index);

    $i = 0;
    if (mysqli_num_rows($health_index_result)) {
        while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
            $per_for_10_to_15[$i] = $row_health_index['health_index'];
            $i++;
        }
    }

    $avg = 0;
    if (count($per_for_10_to_15)) {
        $avg = round(array_sum($per_for_10_to_15) / count($per_for_10_to_15), 2);
    }

    $data['exp_10_15']['avg'] = $avg;
    $data['exp_10_15']['gtotal'] = $total;

    // for 15 to 20

    $total = 0;

    $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=15 and (datediff(CURRENT_DATE,doj)/365)<20 and status='Active' $qry_filter $qry_dob";

    $pat_result = mysqli_query($conn, $pat_query);
    if (mysqli_num_rows($pat_result)) {
        $total = mysqli_num_rows($pat_result);
    } else {
        $total = 0;
    }

    $per_for_15_to_20 = [];
    $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=15 and (datediff(CURRENT_DATE,doj)/365)<20 and status='Active' $qry_filter $qry_dob) $qry_date1 GROUP by emp_id";

    error_log("count health_index:" . $count_health_index);
    $health_index_result = mysqli_query($conn, $count_health_index);

    $i = 0;
    if (mysqli_num_rows($health_index_result)) {
        while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
            $per_for_15_to_20[$i] = $row_health_index['health_index'];
            $i++;
        }
    }

    $avg = 0;
    if (count($per_for_15_to_20)) {
        $avg = round(array_sum($per_for_15_to_20) / count($per_for_15_to_20), 2);
    }

    $data['exp_15_20']['avg'] = $avg;
    $data['exp_15_20']['gtotal'] = $total;

    // for 20 to 25

    $total = 0;

    $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=20 and (datediff(CURRENT_DATE,doj)/365)<25 and status='Active' $qry_filter $qry_dob";

    $pat_result = mysqli_query($conn, $pat_query);
    if (mysqli_num_rows($pat_result)) {
        $total = mysqli_num_rows($pat_result);
    } else {
        $total = 0;
    }

    $per_for_20_to_25 = [];
    $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=20 and (datediff(CURRENT_DATE,doj)/365)<25 and status='Active' $qry_filter $qry_dob) $qry_date1 GROUP by emp_id";

    error_log("count health_index:" . $count_health_index);
    $health_index_result = mysqli_query($conn, $count_health_index);

    $i = 0;
    if (mysqli_num_rows($health_index_result)) {
        while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
            $per_for_20_to_25[$i] = $row_health_index['health_index'];
            $i++;
        }
    }

    $avg = 0;
    if (count($per_for_20_to_25)) {
        $avg = round(array_sum($per_for_20_to_25) / count($per_for_20_to_25), 2);
    }

    $data['exp_20_25']['avg'] = $avg;
    $data['exp_20_25']['gtotal'] = $total;


    //for greater_than_equal_to_25
    $total = 0;

    $pat_query = "SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=25 and status='Active' $qry_filter $qry_dob";

    $pat_result = mysqli_query($conn, $pat_query);
    if (mysqli_num_rows($pat_result)) {
        $total = mysqli_num_rows($pat_result);
    } else {
        $total = 0;
    }

    $per_for_greater_than_equal_to_25 = [];
    $count_health_index = "SELECT max(checkup_id),health_index from checkup_form where emp_id in (SELECT id FROM patient_master where (datediff(CURRENT_DATE,doj)/365)>=25 and status='Active' $qry_filter $qry_dob) $qry_date1 GROUP by emp_id";

    error_log("count health_index:" . $count_health_index);
    $health_index_result = mysqli_query($conn, $count_health_index);

    $i = 0;
    if (mysqli_num_rows($health_index_result)) {
        while ($row_health_index = mysqli_fetch_assoc($health_index_result)) {
            $per_for_greater_than_equal_to_25[$i] = $row_health_index['health_index'];
            $i++;
        }
    }

    $avg = 0;
    if (count($per_for_greater_than_equal_to_25)) {
        $avg = round(array_sum($per_for_greater_than_equal_to_25) / count($per_for_greater_than_equal_to_25), 2);
    }

    $data['exp_greater_than_equal_to_25']['avg'] = $avg;
    $data['exp_greater_than_equal_to_25']['gtotal'] = $total;
}
echo json_encode($data);