<?php

require_once __DIR__ . '/db_config.php';

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

if (isset($_POST["mail"]) ) {
 
$cname=$_POST["cname"];
$projectId=$_POST["project_id"];
$mail=$_POST["mail"];
$role=$_POST["role"];

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());
    $query="SELECT task_request.id,t_milestone_id,p.project_id,task_request.task_id,task_request.increasedays,task_task.task_name,m_enddate,signupdetails.employeename,COUNT(*)-1 as counts,task_task.t_end_date,p_end_date,DATE_ADD(t_end_date,INTERVAL task_request.increasedays DAY) as newd,(CASE WHEN DATE_ADD(t_end_date,INTERVAL task_request.increasedays DAY)<=m_enddate THEN 'false' ELSE 'true' END) as afterm,(CASE WHEN DATE_ADD(t_end_date,INTERVAL task_request.increasedays DAY)<=p_end_date THEN 'false' ELSE 'true' END) as afterp FROM `task_request` INNER JOIN task_task ON task_task.task_id=task_request.task_id AND task_task.t_deleted='no' INNER JOIN signupdetails ON signupdetails.emailid=task_request.employee_id 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' INNER JOIN task_listtask l ON task_task.listtask_id=l.listtask_id AND l.l_deleted='no' LEFT JOIN task_request tr1 ON tr1.task_id=task_request.task_id AND tr1.employee_id=task_request.employee_id AND tr1.deleted='no' WHERE task_request.r_status='pending' AND task_request.deleted='no' AND task_task.t_project_id='$projectId' ";

if($role=="employee"){
    $query=$query." AND task_request.employee_id='$mail' ";
}

$query=$query." GROUP BY task_request.task_id,task_request.employee_id";

$result=mysqli_query($con,$query);
if(!empty($result) && mysqli_num_rows($result) > 0){
    while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
        array_push($response["requests"], $row);
    }
}
    mysqli_close($con);

}

echo json_encode($response);


?>

