<?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"];
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());

$idQuery="SELECT generateMilestoneId('$projectid') AS id";
$idResult=mysqli_query($con,$idQuery);
if(!empty($idResult) && mysqli_num_rows($idResult) > 0){
    $row = mysqli_fetch_array($idResult,MYSQLI_ASSOC);
    $milestoneId=$row["id"];
}

$query="INSERT INTO task_milestone(project_id,milestone_id,milestone_name,m_startdate,m_enddate,m_description,tag_id,responsible_id,m_created_by_mailid,m_created_by_type,m_datetime) VALUES ('$projectid','$milestoneId','$name','$start','$end','$des','$tag','$responsible','$mail','$role','$dt')";
  
if (mysqli_query($con,$query)) {
    $response["s"] = 1;
    $response["m"] = "Task Succesfully Rejected";
    mysqli_query($con,"CALL updateProjectDeadline('$projectid','$milestoneId','$end')");

} else {
    $response["s"] = 0;
    $response["m"] = "Oops! An error occurred.";
}
    mysqli_close($con);
} 
    echo json_encode($response);

?>