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

$ohc_location = $_REQUEST['ohc_location'];
//echo "new ohc location.......".$ohc_location;
$patient_category = $_REQUEST['patient_category'];
$dept = $_REQUEST['dept_id'];
//echo $dept;
$emp_designation = $_REQUEST['emp_designation'];
$emp_cadre = $_REQUEST['emp_cadre'];
$employer_contractor = $_REQUEST['employer_contractor'];
$gender = $_REQUEST['gender'];
$doj = $_REQUEST['doj'];






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



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


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

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

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

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

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

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


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




$data = array();


$query="SELECT count(*) as total,night_blind FROM patient_master where YEAR(CURDATE())-year(dob)>=18 and YEAR(CURDATE())-year(dob)<=25 and night_blind='1' $qry_date";
//echo $query.$qry_bu;
if (!$result = @mysqli_query($conn,$query.$qry_bu)) {
	
	exit(mysqli_error($conn));
}




if(mysqli_num_rows($result) > 0) {
	if ($row = mysqli_fetch_assoc($result)) {
		$data['age_18_25'] = $row;
		$data['night_blind'] = $row;
	}
}

$query="SELECT count(*) as total,night_blind FROM patient_master where YEAR(CURDATE())-year(dob)>=26 and YEAR(CURDATE())-year(dob)<=30 and night_blind='1' $qry_date";

if (!$result = @mysqli_query($conn,$query.$qry_bu)) {
	exit(mysqli_error($conn));
}

if(mysqli_num_rows($result) > 0) {
	if ($row = mysqli_fetch_assoc($result)) {
		$data['age_26_30'] = $row;
		$data['night_blind'] = $row;
	}
}


$query="SELECT count(*) as total,night_blind FROM patient_master where YEAR(CURDATE())-year(dob)>=31 and YEAR(CURDATE())-year(dob)<=40 and night_blind='1' $qry_date";

if (!$result = @mysqli_query($conn,$query.$qry_bu)) {
	exit(mysqli_error($conn));
}

if(mysqli_num_rows($result) > 0) {
	if ($row = mysqli_fetch_assoc($result)) {
		$data['age_31_40'] = $row;
		$data['night_blind'] = $row;
	}
}

$query="SELECT count(*) as total,night_blind FROM patient_master where YEAR(CURDATE())-year(dob)>=41 and YEAR(CURDATE())-year(dob)<=50 and night_blind='1' $qry_date";

if (!$result = @mysqli_query($conn,$query.$qry_bu)) {
	exit(mysqli_error($conn));
}

if(mysqli_num_rows($result) > 0) {
	if ($row = mysqli_fetch_assoc($result)) {
		$data['age_41_50'] = $row;
		$data['night_blind'] = $row;
	}
}

$query="SELECT count(*) as total ,night_blind FROM patient_master where YEAR(CURDATE())-year(dob)>50 and night_blind='1' $qry_date";

if (!$result = @mysqli_query($conn,$query.$qry_bu)) {
	exit(mysqli_error($conn));
}

if(mysqli_num_rows($result) > 0) {
	if ($row = mysqli_fetch_assoc($result)) {
		$data['age_more50'] = $row;
		$data['night_blind'] = $row;
	}
}


$query="SELECT count(*) as total FROM patient_master ";
$qry_bu=preg_replace('/and/', 'where', $qry_bu, 1);
//echo $query.$qry_bu;
if (!$result = @mysqli_query($conn,$query.$qry_bu)) {
	exit(mysqli_error($conn));
}

if(mysqli_num_rows($result) > 0) {
	if ($row = mysqli_fetch_assoc($result)) {
		$data['total_count'] = $row;
	}
}


echo json_encode($data);
?>