<?php

$response = array();

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

    
// check for required fields
if (isset($_POST['data']))  {


$json=json_decode($_POST['data'],true);

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

$name=$json["name"];
$des=$json["des"];

$responsible=$json["responsible"];
$projectid=$json["id"];
$milestoneid=$json["milestone"];

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



$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());

$idQuery="SELECT generateTasklistId('$projectid') AS id";
$idResult=mysqli_query($con,$idQuery);
if(!empty($idResult) && mysqli_num_rows($idResult) > 0){
    $row = mysqli_fetch_array($idResult,MYSQLI_ASSOC);
    $tasklistId=$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"];
}




  
  $query="INSERT INTO task_listtask(l_companyname,l_emailid,l_type,project_id,milestone_id,listtask_id,listtask_name,responsible_id,l_description, l_datetime,l_created_by_mailid,l_created_by_type) VALUES ('$cname','$admin','$ctype','$projectid','$milestoneid','$tasklistId','$name','$responsible','$des','$dt','$mail','$role')";
  


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

?>