<?php

$response = array();

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

    
// check for required fields
if (isset($_POST['data']))  {
    
$mail= $_POST['mail'];
$cname=$_POST["cname"];
$role=$_POST["role"];

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

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

$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"];
}


$name=$json["name"];
$des=$json["des"];
$client=$json["client"];
$companyid=$json["companyid"];
$cat=$json["cat"];
$tag=$json["tag"];
$responsible=$json["responsible"];
$start=$json["start"];
$end=$json["end"];

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

  
  $query="INSERT INTO task_project(p_companyname,p_emailid,p_type,project_id,project_name,p_description,client,c_company_id,cat_id,tag_id,responsible_id,p_created_by_mailid,p_created_by_type,p_start_date,p_end_date,p_date_time) VALUES ('$cname','$admin','$ctype','$projectId','$name','$des','$client','$companyid','$cat','$tag','$responsible','$mail','$role','$start','$end','$dt')";
  


    if (mysqli_query($con,$query)) {
        $response["s"] = 1;
        $response["m"] = "Task Succesfully Rejected";
    } else {
        $response["s"] = 0;
        $response["m"] = "Oops! An error occurred.";
    }
    mysqli_close($con);
} 
    echo json_encode($response);

?>