<?php

require_once __DIR__ . '/db_config.php';

$response = array();
$response["tasks"] = array();
$response["assigns"] = array();
$response["employees"] = array();
$tasks=array();
$employess=array();

if ( isset($_POST["mail"]) && isset($_POST["cname"]) && isset($_POST["role"]) ) {
 
$mail= $_POST['mail'];
$cname=$_POST["cname"];
$role=$_POST["role"];
$id=$_POST["id"];
$tid=$_POST["tid"];

$responsible=$_POST["responsible"];

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



$taskQuery="SELECT * FROM tm_all_tasks WHERE listtask_id='$id'";
$assignQuery="SELECT * FROM tm_all_assign ";

if($role!="admin" && $responsible == "no" ){
    $taskQuery=$taskQuery." AND employee='$mail' ";
}

$taskQuery=$taskQuery." 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"]);
    }
}

$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);

    }
}

$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);


?>