<?php
$start_date=$_GET['start_date'];
$end_date=$_GET['end_date'];
$region=$_GET['region'];

include 'config.php';
$region = $con -> real_escape_string($region);
$rows['data'][] = '0-6M';
$rows['data'][] = '7M-2Y';
$rows['data'][] = '3Y-5Y';
$rows['data'][] = '6Y-10Y';
$rows['data'][] = '>10Y';
$rows['data'][] = 'Unspecified';
//0-6M
$sth = mysqli_query($con,"SELECT agegroup,coalesce(sum(voluntary+involuntary),0) as involuntary from duration where region='$region' and registration_date between '$start_date' and '$end_date' and agegroup='0-6M'");
$rows1 = array();
$rows1['name'] = 'Duration';
$rows1['color'] = '#0033a1';
while($rr = mysqli_fetch_array($sth)) {
$sixmonths= $rr['involuntary'];
}
//7M-2Y
$sth = mysqli_query($con,"SELECT agegroup,coalesce(sum(voluntary+involuntary),0) as involuntary from duration where region='$region' and registration_date between '$start_date' and '$end_date' and agegroup='7M-2Y'");
$rows2 = array();
while($rr = mysqli_fetch_array($sth)) {
$twoyears = $rr['involuntary'];
}
//3Y-5Y
$sth = mysqli_query($con,"SELECT agegroup,coalesce(sum(voluntary+involuntary),0) as involuntary from duration where region='$region' and registration_date between '$start_date' and '$end_date' and agegroup='3Y-5Y'");
$rows2 = array();
while($rr = mysqli_fetch_array($sth)) {
$fiveyears = $rr['involuntary'];
}
//6Y-10Y
$sth = mysqli_query($con,"SELECT agegroup,coalesce(sum(voluntary+involuntary),0) as involuntary from duration where region='$region' and registration_date between '$start_date' and '$end_date' and agegroup='6Y-10Y'");
$rows2 = array();
while($rr = mysqli_fetch_array($sth)) {
$tenyears = $rr['involuntary'];
}
//>10Y
$sth = mysqli_query($con,"SELECT agegroup,coalesce(sum(voluntary+involuntary),0) as involuntary from duration where region='$region' and registration_date between '$start_date' and '$end_date' and agegroup='Unpecified'");
$rows2 = array();
while($rr = mysqli_fetch_array($sth)) {
$unspecified = $rr['involuntary'];
}
//Unspecified
$sth = mysqli_query($con,"SELECT agegroup,coalesce(sum(voluntary+involuntary),0) as involuntary from duration where region='$region' and registration_date between '$start_date' and '$end_date' and agegroup='>10Y'");
$rows2 = array();
while($rr = mysqli_fetch_array($sth)) {
$greaterthantenyears = $rr['involuntary'];
}

$rows1['data'][] = $sixmonths;
$rows1['data'][] = $twoyears;
$rows1['data'][] = $fiveyears;
$rows1['data'][] = $tenyears;
$rows1['data'][] = $greaterthantenyears;
$rows1['data'][] = $unspecified;
$result = array();
array_push($result,$rows);
array_push($result,$rows1);
print json_encode($result, JSON_NUMERIC_CHECK);
mysqli_close($con);
?>