<?php

$response = array();

// include db connect class
require_once __DIR__ . '/db_config.php';

    
// check for required fields
if (isset($_POST['data']))  {
    
$mail= $_POST['mail'];
$cname=$_POST["cname"];
$role=$_POST["role"];
$projectId=$_POST["id"];

$json=json_decode($_POST['data'],true);
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());


$name=$json["name"];
$des=$json["des"];
$client=$json["client"];
$companyid=$json["companyid"];
$cat=$json["cat"];
$tag=$json["tag"];
$responsible=$json["responsible"];
$start=$json["start"];
$end=$json["end"];

date_default_timezone_set('Asia/Calcutta');
$dt=date('Y-m-d H:i:s');

$checkQuery="SELECT t_milestone_id FROM task_task WHERE t_milestone_id='$milestoneId' AND t_end_date<='$end'";
$checkResult=mysqli_query($con,$checkQuery);

if($checkResult && mysqli_num_rows($checkResult) > 0){
    $response["s"]=false;
    $response["m"]="Some milestone deadlines came before the selected project deadline";
}else{
    $query="UPDATE task_project SET project_name='$name',p_description='$des',client='$client',c_company_id='$companyid',cat_id='$cat',tag_id='$tag',responsible_id='$responsible',p_start_date='$start',p_end_date='$end',p_u_created_by_mailid='$mail',p_u_created_by_type='$role',p_u_datetime='$dt',P_update_time='$dt' WHERE project_id='$projectId'";
    $response["s"] =mysqli_query($con,$query);
    if($response["s"]){
            mysqli_query($con,"CALL updateProjectDeadline('$projectid','$milestoneId','$end')");
    }else{
        $response["m"]="Unable to Update";
    }
}

  
  
    mysqli_close($con);
} 
    echo json_encode($response);

?>