<?php include 'connection.php';
date_default_timezone_set('Asia/Calcutta');
$today = date("Y-m-d");
$time = date("h:i A"); 

function getAmPm($time) {
    // Convert the time string to a DateTime object
    $dateTime = DateTime::createFromFormat('H:i', $time);

    // Check if the conversion was successful
    if ($dateTime === false) {
        return "Invalid time format";
    }

    // Format the DateTime object to get the AM/PM value
    $amPm = $dateTime->format('A');

    return $amPm;
}



$time_09 = '06:00:00'; // Example time

// Create a DateTime object from the given time
$dateTime09 = new DateTime($time_09);

// Format the DateTime object to extract the hour and minute
$hour = $dateTime09->format('H');
$minute = $dateTime09->format('i');

// Check if the time is exactly 6 AM
if ($hour == '06' && $minute == '00') {

$query1a = "select * from appointment_details where reminder_done='DONE' and mreminder_done=''";
$result1a = mysqli_query($conn,$query1a);
while($row=mysqli_fetch_array($result1a))
{
$appointment_date = $row['appointment_date'];
$doctor_id = $row['doctor_id'];
$branch_id = $row['branch_id'];
$patient_name = $row['patient_name'];
$time = $row['appointment_time'];
$mobile_no = $row['mobile_no'];
$appointment_id  = $row['appointment_id'];

$date = new DateTime($appointment_date);

// Subtract one day from the date
$date->modify('-1 day');

// Format the date back to a string
$previous_date = $date->format('Y-m-d');

if($today==$appointment_date)
{


    $amPm = getAmPm($time);
    $appointment_time01 = $time.' '.$amPm;

$querycon = "select * from  branch_master where branch_id = '$branch_id'";
$resultcon = mysqli_query($conn,$querycon);
while($row=mysqli_fetch_array($resultcon))
{
$branch_name = $row['branch_name'];
$branch_address = $row['branch_address'];
$helpline = $row['helpline'];
}



$querycond = "select * from  doctor_master where doctor_id = '$doctor_id'";
$resultcond = mysqli_query($conn,$querycond);
while($row=mysqli_fetch_array($resultcond))
{
$doctor_name = $row['doctor_name'];
$degree = $row['degree'];
}

if($mobile_no!=''){
$mobile_nosa1 = "91".$mobile_no;

//$mobile_nosa1 = "919224328435";

$message1a=nl2br("Your follow Up appointment details".'\n\n'."Welcome To Kanakaveda Ayurveda Your appointment  details. ".'\n\n'."Name Of Patient: ".$patient_name.'\n\n'."Appointment Date: ".$appointment_date.'\n\n'."Appointment Time: ".$appointment_time01.'\n\n'."Appointment with:".'\n'.$doctor_name.'\n'.$degree.'\n\n'."Branch: ".$branch_name.'\n\n'."Address: ".$branch_address.'\n\n'."Helpline:".$helpline.'\n\n'."Website:".'\n'."www.kanakavedaayurveda.com");

$curlmessage = curl_init();
curl_setopt_array($curlmessage, [
CURLOPT_URL => "https://waapi.app/api/v1/instances/11867/client/action/send-message",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",


CURLOPT_POSTFIELDS => "{\"chatId\":\"".$mobile_nosa1."@c.us\",\"message\":\"".$message1a."\",\"mentions\":[\"123456789@c.us\",\"987654321@c.us\"]}",

CURLOPT_HTTPHEADER => [
"Authorization: Bearer vJgrDNPuVKpht6lctgqJBQdjSDJsZMwWtcYlHEE6b1f275a0",
"content-type: application/json"
],
]);
$response = curl_exec($curlmessage);
$err = curl_error($curlmessage);
curl_close($curlmessage);
if ($err) {
//echo "cURL Error #:" . $err;
} else {
//echo $response;
}


}



}

}
$queryleadqf="update appointment_details set mreminder_done='DONE' where appointment_id = '$appointment_id'";
$resultleadqf=mysqli_query($conn,$queryleadqf);
}

?>