<?php

require_once __DIR__ . '/db_config.php';
require_once __DIR__ . '/notify.php';

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

if (isset($_POST["mail"]) ) {


$noity=new notify();

$cname=$_POST["cname"];
$id=$_POST["id"];
$mail=$_POST["mail"];
$role=$_POST["role"];
$responseText=$_POST["response"];
$enddate=$_POST["enddate"];
$status=$_POST["status"];
$tid=$_POST["tid"];
$milestone=$_POST["milestone"];
$project=$_POST["project"];
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());

    $query="UPDATE task_request SET response='$responseText',r_status='$status',response_by='$mail',response_by_type='$role' WHERE id='$id' ";
    $response["s"]=mysqli_query($con,$query);
   
    $companyQuery="SELECT employee_id FROM task_request WHERE id='$id'";
    $companyResult=mysqli_query($con,$companyQuery);
    if(!empty($companyResult) && mysqli_num_rows($companyResult) > 0){
        $row = mysqli_fetch_array($companyResult,MYSQLI_ASSOC);
        $employee=$row["employee_id"];
    }
    
    if($response["s"] && $status=="accepted"){
        $query="UPDATE task_task SET t_end_date='$enddate' WHERE task_id='$tid'";
        
        $noity->send($con,$employee,$id,"Request has been acccepted","request");
        mysqli_query($con,"CALL updateMilestoneDeadline('$milestone','$tid','$enddate')");
        mysqli_query($con,"CALL updateProjectDeadline('$project','$milestone','$enddate')");
        mysqli_query($con,$query);
        
    }else if($response["s"]){
                $noity->send($con,$employee,$id,"Request has been declined","request");
    }
    mysqli_close($con);
}

echo json_encode($response);


?>

