<?php

$response = array();

// include db connect class
require_once __DIR__ . '/db_config.php';
require_once __DIR__ . '/notify.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);
$noity=new notify();

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

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

$query="UPDATE task_task SET t_status='completed',completed_datetime='$dt',completed_by_type='$role',completed_by='$mail',t_progress=100 WHERE task_id='$taskId'";

$response["s"] =mysqli_query($con,$query);
mysqli_query($con,"UPDATE task_assign set a_status='completed' where assign_id='$assignId'");

if($response["s"]){
    $companyQuery="SELECT employee_id FROM `task_assign` WHERE assign_id='$assignId'";
    $companyResult=mysqli_query($con,$companyQuery);
    if(!empty($companyResult) && mysqli_num_rows($companyResult) > 0){
        $row = mysqli_fetch_array($companyResult,MYSQLI_ASSOC);
        $assignedTo=$row["employee_id"];
    }
    $noity->send($con,$assignedTo,$taskId,"Task Completed","completed");
}


mysqli_close($con);
} 
    echo json_encode($response);

?>