<?php

require_once __DIR__ . '/db_config.php';

$response = array();
$response["counts"] = array();

if (isset($_POST["id"]) ) {
 
$projectId=$_POST["id"];
$employee=$_POST["employee"];

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());
date_default_timezone_set('Asia/Calcutta');
$date=date('Y-m-d');

$query="SELECT SUM(CASE WHEN t_status='underprogress' AND t_end_date>='$date' THEN 1 ELSE 0 END) AS 'active',SUM(CASE WHEN t_status='underprogress' AND t_end_date<'$date' THEN 1 ELSE 0 END) AS 'late',SUM(CASE WHEN t_status='completed' OR t_progress=90 THEN 1 ELSE 0 END) AS 'completed',SUM(CASE WHEN t_status='stalled' THEN 1 ELSE 0 END) AS 'stalled',SUM(CASE WHEN t_status='stopped' THEN 1 ELSE 0 END) AS 'stopped',SUM(CASE WHEN t_status='unassigned' THEN 1 ELSE 0 END) AS 'unassigned',SUM(CASE WHEN t_status='assigned' THEN 1 ELSE 0 END) AS 'assigned' FROM task_task LEFT JOIN task_assign ON task_assign.task_id=task_task.task_id AND task_assign.a_deleted='no' AND task_assign.a_status!=('declined' OR 'stopped') WHERE  task_task.t_project_id='$projectId'";

 


$query="SELECT SUM(CASE WHEN t_status='underprogress' AND t_end_date>='$date' THEN 1 ELSE 0 END) AS 'active',SUM(CASE WHEN t_status='underprogress' AND t_end_date<'$date' THEN 1 ELSE 0 END) AS 'late',SUM(CASE WHEN t_status='completed' OR t_progress=90 THEN 1 ELSE 0 END) AS 'completed',SUM(CASE WHEN t_status='stalled' THEN 1 ELSE 0 END) AS 'stalled',SUM(CASE WHEN t_status='stopped' THEN 1 ELSE 0 END) AS 'stopped',SUM(CASE WHEN t_status='unassigned' THEN 1 ELSE 0 END) AS 'unassigned',SUM(CASE WHEN t_status='assigned' THEN 1 ELSE 0 END) AS 'assigned' FROM task_task LEFT JOIN task_assign ON task_assign.task_id=task_task.task_id AND task_assign.a_deleted='no' AND task_assign.a_status!=('declined' OR 'stopped') INNER JOIN task_listtask ON task_listtask.listtask_id=task_task.listtask_id AND task_listtask.l_deleted='no' INNER JOIN task_milestone m ON m.milestone_id=task_task.t_milestone_id AND m.m_deleted='no' INNER JOIN task_project p ON p.project_id=task_task.t_project_id AND p.p_deleted='no'  WHERE  task_task.t_project_id='$projectId' and t_deleted='no'";


if(!empty($employee)){
    $query=$query." and task_assign.employee_id='$employee'";
}
$result=mysqli_query($con,$query);
if(!empty($result) && mysqli_num_rows($result) > 0){
    $response["s"]=true;
    while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
        array_push($response["counts"], $row);
    }
}else{
        $response["s"]=false;
}
    mysqli_close($con);

}

echo json_encode($response);


?>