<?php

require_once __DIR__ . '/db_config.php';

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

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

$responsible=$_POST["responsible"];

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


$milestoneQuery="SELECT * FROM tm_all_milestones WHERE projectid='$projectId' AND milestoneid IS NOT NULL " ;


if(!empty($mid)){
   $milestoneQuery=$milestoneQuery." AND milestoneid='$mid' ";
}else if($role!="admin" && $responsible == "no" ){
    $milestoneQuery=$milestoneQuery." AND employee='$mail'";
}

$milestoneQuery=$milestoneQuery." GROUP BY milestoneid";

$milestoneResult=mysqli_query($con,$milestoneQuery);

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

}

echo json_encode($response);


?>