<?php

$response = array();

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

    
// check for required fields
if (isset($_POST['data']))  {
    
$json=json_decode($_POST['data'],true);

$mail= $json['mail'];
$cname=$json["cname"];
$role=$json["role"];
$name=$json["name"];
$des=$json["des"];
$tag=$json["tag"];
$responsible=$json["responsible"];
$start=$json["start"];
$end=$json["end"];
$projectid=$json["id"];
$milestoneId=$json["milestoneid"];
date_default_timezone_set('Asia/Calcutta');
$dt=date('Y-m-d H:i:s');

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

$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 task deadlines came before the selected milestone deadline";
}else{
   $query="UPDATE task_milestone SET milestone_name='$name',m_startdate='$start',m_enddate='$end',m_description='$des',tag_id='$tag',responsible_id='$responsible',m_u_created_by_mailid='$mail',m_u_created_by_type='$role',m_u_datetime='$dt',m_updatedateandtime='$dt' WHERE milestone_id='$milestoneId'";

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

?>