<?php
require_once __DIR__ . '/db_config.php';

$response = array();
  
// check for required fields
if (isset($_POST['data']))  {
    
$json=json_decode($_POST['data'],true);

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

$place=$json["place"];
$travel_type=$json["travel_type"];
$billable=$json["billable"];
$customer=$json["customer"];
$purpose=$json["purpose"];
$start=$json["start"];
$end=$json["end"];
$duration=$json["duration"];
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 generateTravelId('$cname') AS id";
$idResult=mysqli_query($con,$idQuery);
if(!empty($idResult) && mysqli_num_rows($idResult) > 0){
    $row = mysqli_fetch_array($idResult,MYSQLI_ASSOC);
    $travelId=$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 hcm_travel(company,tr_emailid,tr_id,tr_place_to_visit, tr_start,tr_end,tr_duration,tr_purpose_visit,tr_customer_name,tr_billable, tr_travel_type,t_datetime,createdby,createdtype) VALUES ('$cname','$mail','$travelId','$place','$start','$end','$duration','$purpose','$customer','$billable','$travel_type','$dt','$admin','$role')";
  $response["s"]=mysqli_query($con,$query);
    mysqli_close($con);
} 
    echo json_encode($response);

?>