<?php
error_reporting(0);
include "../includes/config/config.php";
include "../includes/functions.php";
include 'log_entry.php';
// include "mailend.php";



$response = array();

$date = date_format(date_create($_REQUEST["date"]), "Y-m-d");

// $d=date_format($_REQUEST["date"] , "Y/m/d");

$time = $_REQUEST["time"];

$emp_id = $_REQUEST["emp_id"];

$ohc_type_id = $_REQUEST["ohc_type_id"];

$day = $_REQUEST["day"];

$doctor_id = $_REQUEST["doctor_id"];
$type = $_REQUEST["type"];
$id = $_REQUEST["id"];

// $query="INSERT INTO `appointment_details`( `date`, `time`, `day`,`doctor_id`,`emp_id`) VALUES ('$date','$time','$day','$doctor_id','$emp_id')";
// $exe=mysqli_query($conn,$query);
// $arr=[];
// if($exe){
// $arr["sucess"]="true";
// }else{
//     $arr["sucess"]="false";
// }
// print(json_encode($arr))
$sql = "SELECT * FROM appointment_details WHERE emp_id='$emp_id' AND doctor_id='$doctor_id' AND `date` = '$date' AND `time` = '$time'";
error_log("API LOGIN DM : " . $sql);
$check = mysqli_query($conn, $sql);

if ($check->num_rows > 0) {
    if ($check) {
        $arr = "false";
    }
} else {
    if ($id) {
        $insertSql = "UPDATE `appointment_details` SET doctor_id='$doctor_id' , time='$time',date='$date' WHERE id='$id'";
        
    } else {
        $insertSql = "INSERT INTO `appointment_details`( `date`, `time`, `day`,`doctor_id`,`emp_id` ,`ohc_type_id` , `status` ,`app_type`) VALUES ('$date','$time','$day','$doctor_id','$emp_id' , '$ohc_type_id' , 'BOOKED' , '$type')";
        
    }
    // If not, save the date and time to the database
    error_log("API LOGIN DM : " . $insertSql);
    $exe = mysqli_query($conn, $insertSql);
    if ($id) {
        $new_id=$id;
    }else{
        $new_id=mysqli_insert_id($conn);
    }
    if ($exe) {
        $sql1 = "SELECT * from appointment_details where id = '$new_id'";
		error_log('apps' . $sql1);
		$query1 = mysqli_query($conn, $sql1);
        $Update_Doc_Status = "UPDATE `doc_slots` SET `status` = 'BOOKED' WHERE doc_slot_id = '$time' AND ohc_type_id = '$ohc_type_id'";
        error_log("Update_Doc_Status: " . $Update_Doc_Status);
        $update_exe = mysqli_query($conn, $Update_Doc_Status);
        $arr = "true";
        // while ($fetch = mysqli_fetch_assoc($query1)) {
		// 	$emailContent = "<p>Dear Sir/Madam, </p>";
		// 	$status = '';
		// 	$sub_status = '';
		// 	if ($fetch['status'] == 'BOOKED') {
		// 		$sub_status = 'confirmed';
		// 		$status = "<font color=green>confirmed</font> ";
		// 	} else if ($fetch['status'] == 'DRP') {
		// 		$sub_status = 'pending';
		// 		$status = "<font color=red>confirmed</font>";
		// 	} else if ($fetch['status'] == 'DRA') {
		// 		$sub_status = 'completed';
		// 		$status = "<font color=green>completed</font> ";
		// 	} else if ($fetch['status'] == 'CANCELLED') {
		// 		$sub_status = 'cancelled';
		// 		$status = " <font color=red>cancelled</font>";
		// 	}
		// 	if ($fetch['app_type'] == 'FIT') {
		// 		$fetch['app_type'] = 'sickness/fitness';
		// 	} else if ($fetch['app_type'] == 'MED') {
		// 		$fetch['app_type'] = 'periodic health checkup';
		// 	}
		// 	$email_list_to = getTableFieldValue('patient_master', 'offiial_email_id', 'id', '"' . $fetch['emp_id'] . '"');
		// 	$email_list_cc = getTableFieldValue('patient_master', 'offiial_email_id', 'id', '"' . $fetch['doctor_id'] . '"');

		// 	$ref_type = $fetch['app_type'] . " Appointment";

		// 	$subject = 'Your appointment got ' . $sub_status . ' in ' . getTableFieldValue('ohc_type', 'ohc_type_name', 'ohc_type_id', '"' . $fetch['ohc_type_id'] . '"') . ' OHC on ' .
		// 		date_format(date_create($fetch['date']), "d-m-Y ") . '.';
		// 	// if ($type != 'del') {

		// 	//     emailContent += "<p>Your appointment got " + status + " in " + data.ohc_type + " OHC on " +
		// 	//         data.date +
		// 	//         " between " +
		// 	//         data.slot +
		// 	//         " - " +
		// 	//         data.slot_end +
		// 	//         ".</p>";
		// 	//     emailContent +=
		// 	//         "<p>Kindly make sure 12 hours fasting is required (You can consume water) before this health checkup. </p><p> Regards,</p><p> Team OHC</p>";
		// 	// } else {

		// 	$emailContent .= "<p>You have booked your health checkup in " . getTableFieldValue('ohc_type', 'ohc_type_name', 'ohc_type_id', '"' . $fetch['ohc_type_id'] . '"') ." OHC got <font color=red>Cancelled</font> .</p>";
		// 	$emailContent .= "<p> Regards,</p><p> Team OHC</p>";

		// 	// }
		// 	// emailContent += "<br><a href=`" + link +
		// 	//     "`> Details</a> ";
		// 	$message = $emailContent;


		// 	$id = $appointmentId;
		// 	$message = $emailContent;
		// 	send_mail(
		// 		$email_list_to,
		// 		$subject,
		// 		$message,
		// 		"",
		// 	);
		// 	//send_email($appointmentId,  getTableFieldValue('ohc_type', 'ohc_type_name', 'ohc_type_id', '"' . $fetch['ohc_type_id'] . '"'),  getTableFieldValue('ohc_type', 'primary_email', 'ohc_type_id', '"' . $fetch['ohc_type_id'] . '"'), date_format(date_create($fetch['date']), "d-m-Y "), getTableFieldValue('patient_master', 'patient_name', 'id', '"' . $fetch['emp_id'] . '"'), $fetch['app_type'], $link, $type);
		// }
    } else {
        $arr = "false";
    }
}
echo json_encode($arr);