<?php

require_once __DIR__ . '/db_config.php';

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

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

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


$projectQuery="SELECT * FROM tm_all_projects WHERE companyname='$cname' ";


if(!empty($id)){
   $projectQuery=$projectQuery." and projectid='$id' ";
}else if($role!="admin"){
    $projectQuery=$projectQuery." and (presponsible='$mail' OR employee='$mail')";
}

$projectQuery=$projectQuery." GROUP BY projectid";


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

}

echo json_encode($response);


?>