<?php

require_once __DIR__ . '/db_config.php';

$response = array();
$response["tasks"] = array();
$response["assigns"] = array();
$response["employees"] = array();
$response["projects"] = array();
$response["milestones"] = array();
$response["tasklists"] = array();
$response["progress"] = array();
$response["requested"] = false;

$tasks=array();
$employess=array();

if ( isset($_POST["taskid"])) {
 
$taskid= $_POST['taskid'];
$tdt=$_POST['tdt'];
$mdt=$_POST['mdt'];
$pdt=$_POST['pdt'];

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());



$taskQuery="SELECT * FROM tm_all_tasks WHERE task_id='$taskid'  GROUP BY task_id";

$taskResult=mysqli_query($con,$taskQuery);
if(!empty($taskResult) && mysqli_num_rows($taskResult) > 0){
    while ($row = mysqli_fetch_array($taskResult,MYSQLI_ASSOC)) {
        array_push($response["tasks"], $row);
        array_push($tasks, $row["task_id"]);
        array_push($employess,$row["employee"]);
        array_push($employess,$row["stalled_by"]);
        array_push($employess,$row["completed_by"]);
        array_push($employess,$row["t_created_by_mailid"]);
        $projectId=$row["t_project_id"];
        $tasklistId=$row["listtask_id"];
        $milestoneId=$row["t_milestone_id"];
    }
}

$assignQuery="SELECT * FROM tm_all_assign WHERE (a_task_id='".implode("' OR  a_task_id='",$tasks)."')";
$assignResult=mysqli_query($con,$assignQuery);
if(!empty($assignResult) && mysqli_num_rows($assignResult) > 0){
    while ($row = mysqli_fetch_array($assignResult,MYSQLI_ASSOC)) {
        array_push($response["assigns"], $row);
    }
}

$tasklsitQuery="SELECT * FROM tm_all_tasklists WHERE tasklistid='$tasklistId'  GROUP BY tasklistid";
$projectQuery="SELECT * FROM tm_all_projects WHERE projectid='$projectId'  GROUP BY projectid";
$milestoneQuery="SELECT * FROM tm_all_milestones WHERE milestoneid='$milestoneId' GROUP BY milestoneid";

$projectResult=mysqli_query($con,$projectQuery);
if(!empty($projectResult) && mysqli_num_rows($projectResult) > 0){
    while ($row = mysqli_fetch_array($projectResult,MYSQLI_ASSOC)) {
        array_push($response["projects"], $row);
    }
}

$milestoneResult=mysqli_query($con,$milestoneQuery);
if(!empty($milestoneResult) && mysqli_num_rows($milestoneResult) > 0){
    while ($row = mysqli_fetch_array($milestoneResult,MYSQLI_ASSOC)) {
        array_push($response["milestones"], $row);
    }
}

$tasklsitResult=mysqli_query($con,$tasklsitQuery);
if(!empty($tasklsitResult) && mysqli_num_rows($tasklsitResult) > 0){
    while ($row = mysqli_fetch_array($tasklsitResult,MYSQLI_ASSOC)) {
        array_push($response["tasklists"], $row);
    }
}

$progressQuery="SELECT task_id,progress_by_type,progress_by,employee_id,progress, progressImage,completed_activities,progress_date,completed_datetime as dateandtime FROM task_progress WHERE task_id='$taskid'";

$progressResult=mysqli_query($con,$progressQuery);
if(!empty($progressResult) && mysqli_num_rows($progressResult) > 0){
    while ($row = mysqli_fetch_array($progressResult,MYSQLI_ASSOC)) {
        array_push($response["progress"], $row);
    }
}

$requestQuery="SELECT id FROM task_request WHERE task_id='$taskid' AND r_status='pending' AND deleted='no' ";
$requestResult=mysqli_query($con,$requestQuery);
if(!empty($requestResult) && mysqli_num_rows($requestResult) > 0){
    $response["requested"] = true;
}

$employess=array_unique($employess);
$employeeQuery="SELECT employeename,emailid FROM signupdetails WHERE (emailid='".implode("' OR  emailid='",$employess)."')";
$employeeResult=mysqli_query($con,$employeeQuery);
if(!empty($employeeResult) && mysqli_num_rows($employeeResult) > 0){
    while ($row = mysqli_fetch_array($employeeResult,MYSQLI_ASSOC)) {
        array_push($response["employees"], $row);
    }
}

}

echo json_encode($response);


?>