<?php

$response = array();

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

    
if (isset($_POST['data']))  {

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

$mail= $json['mail'];
$cname=$json["cname"];
$role=$json["role"];
$taskId=$json["taskId"];
$assignedTo=$json["assignedTo"];

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

$query="UPDATE task_assign set a_deleted='yes' where task_id='$taskId'";

if(mysqli_query($con,$query)){

    $idQuery="SELECT generateAssignId('$taskId') AS id";
    $idResult=mysqli_query($con,$idQuery);
    if(!empty($idResult) && mysqli_num_rows($idResult) > 0){
        $row = mysqli_fetch_array($idResult,MYSQLI_ASSOC);
        $assignid=$row["id"];
    }
    
    $query="INSERT INTO task_assign (task_id,assign_id,employee_id,a_datetime) VALUES('$taskId','$assignid','$assignedTo','$dt');";
    $response["s"] =mysqli_query($con,$query);
    mysqli_query($con,"UPDATE task_task SET task_task.t_status='assigned' WHERE task_id='$taskId'");
    if($response["s"]){
    
    $noity->send($con,$assignedTo,$taskId,"New Task Assigned","new");
    }
}else{
    $response["s"] =false;
}
    mysqli_close($con);
} 
    echo json_encode($response);

?>