<?php

// array for JSON response
$response = array();
require_once __DIR__ . '/db_config.php';

// check for required fields
if (isset($_POST['cname'])) {
    
    date_default_timezone_set('Asia/Calcutta');
    $dt=date('Y-m-d H:i:s');
    $dt=date('Y-m-d');
    
    $companyname= $_POST['cname'];
    $dateandtime= date('Y-m-d H:i:s');
    $assignfor="Employee";
    $assigndate=date('Y-m-d');
    $eventtype="One Time";
    $customertype="own";
    $combinationtype="Service";
    $combinationid=$_POST['cid'];
    $eventtype="One Time";
    $empgroup="";
    $empsubgroup="";
    $empteam="";
    $empemailid=$_POST['empemail'];
    $adminemailid=$_POST['empemail'];
    $facilityid=$_POST['facility_id'];
    $c_datetime=date('Y-m-d H:i:s');
    $c_created_by=$_POST['empemail'];
    $c_creatd_type="employee";
    
    $scheduler_id="";
        $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());

    $idQuery="SELECT COUNT(*) as tot FROM dynamicschedule WHERE companyname='$companyname'";
    $result=mysqli_query($con,$idQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
        $scheduler_id="sch_".$companyname."0".$row['tot'];
    }
    
    $facilityQuery="SELECT id FROM atmdetails WHERE  atmid='$facilityid' and companyname='$companyname'";
    $result=mysqli_query($con,$facilityQuery);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
        $facilityid=$row['id'];
    }


    
    $query="INSERT INTO dynamicschedule (scheduler_id,companyname,adminemailid,customertype,customername,facilityid, 
   combinationtype,combinationid,eventtype, 
   assigndate,assignfor,empgroup,empsubgroup,empteam,empemailid,c_datetime, 
   c_created_by,c_creatd_type) VALUES ('$scheduler_id','$companyname','$adminemailid','$customertype',
   '$companyname','$facilityid','$combinationtype','$combinationid','$eventtype','$assigndate','$assignfor','$empgroup',
   '$empsubgroup','$empteam','$empemailid','$c_datetime','$c_created_by','$c_creatd_type')";
    $result=mysqli_query($con,$query);

    if ($result) {
        $response["s"] = 1;
        $response["sid"] = $scheduler_id."_0";
        $response["fid"] = $facilityid;
        $response["date"] = $dt;
    } else {
        $response["s"] = 0;
    }
    mysqli_close($con);
}else{
    $response["s"] = 2;
}
echo json_encode($response);
?>