<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
require_once "db_connect.php";

$start = $_GET['start_date'];
$end = $_GET['end_date'];
$province = $_GET['province'];

$output = array();


$queryIs = "select dc.code as code, ld.title, count(rl.district_id) as count,COUNT(CASE WHEN rd.violent_nonviolent ='violent' THEN 1 END) AS violent,COUNT(CASE WHEN rd.violent_nonviolent ='nonviolent' THEN 1 END) AS nonviolent from reports_details rd join reports_tags rt on rd.report_id = rt.report_id join reports r on r.id = rd.report_id join reports_locations rl on rl.report_id = r.id join location_districts ld on ld.id = rl.district_id join districts_codes dc on dc.district_id = ld.id where rt.tag_id = 20 and r.approve_status = 'approved' and r.eventdate between $start and $end and rl.province_id = $province";
if($_GET['type'] == 'lock'){
 
   $queryIs = $queryIs." and rd.secondary_cause_code='Solidarity'";
} else if($_GET['type'] == 'qua'){
  
   $queryIs =  $queryIs." and rd.secondary_cause_code='Dialogue'";
} else if($_GET['type'] == 'rel'){
  
   $queryIs =  $queryIs." and rd.secondary_cause_code='Safety'";
} else if($_GET['type'] == 'irr'){
 
   $queryIs =  $queryIs." and rd.secondary_cause_code='Association'";
} else if($_GET['type'] == 'pre'){
   
   $queryIs =  $queryIs." and rd.secondary_cause_code='Assembly'";
} else if($_GET['type'] == 'hea'){
   
   $queryIs =  $queryIs." and rd.secondary_cause_code='Expression'";
}
$queryIs = $queryIs." group by ld.title";
$query = mysqli_query($db,$queryIs);

$province_name = "";
while($row = mysqli_fetch_assoc($query)){
  $output[] = ['district' => (int)$row['code'], 'district_name' => $row['title'], 'count' => (int)$row['count'], 'violent' => (int)$row['violent'], 'nonviolent'=> (int)$row['nonviolent']];
}

$myObj = (object) null;
$myObj->results = $output;
$myObj->type = $_GET['type'];

echo json_encode($myObj);

?>
