<?php

$response = array();

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

    
// check for required fields
if (isset($_POST['data']))  {
    
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());
$json=json_decode($_POST['data'],true);


$mail= $json['mail'];
$cname=$json["cname"];
$role=$json["role"];
$name=$json["name"];
$des=$json["des"];
$project=$json["project"];
$tasklist=$json["tasklist"];
$cat=$json["cat"];
$tag=$json["tag"];
$assignedTo=$json["assignedTo"];
$responsible=$json["responsible"];
$priority=$json["priority"];
$start=$json["start"];
$end=$json["end"];
$milestone=$json["milestone"];


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

$companyQuery="SELECT adminemailid,CompanyType FROM b2lcustomers WHERE companyname='$cname'";
$companyResult=mysqli_query($con,$companyQuery);
if(!empty($companyResult) && mysqli_num_rows($companyResult) > 0){
    $row = mysqli_fetch_array($companyResult,MYSQLI_ASSOC);
    $admin=$row["adminemailid"];
    $ctype=$row["CompanyType"];
}



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

$query="INSERT INTO task_task(t_companyname,t_emailid,t_type,t_project_id, t_milestone_id, listtask_id, task_id, task_name, t_description,t_start_date, t_end_date,t_priority,t_tag,t_created_by_mailid, t_created_by_type, t_datetime) VALUES ('$cname','$admin','$ctype','$project','$milestone','$tasklist','$taskId','$name','$des','$start','$end','$priority','$tag','$mail','$role','$dt')";
 
 
    if (mysqli_query($con,$query)) {
        $response["s"] = 1;
        $response["id"] = $taskId;
        $response["m"] = "Task Succesfully Rejected";
        mysqli_query($con,"CALL updateMilestoneDeadline('$milestone','$taskId','$end')");
        mysqli_query($con,"CALL updateProjectDeadline('$project','$milestone','$end')");
    } else {
        $response["s"] = 0;
        $response["m"] = "Oops! An error occurred.";
    }
    mysqli_close($con);
} 
    echo json_encode($response);

?>