<?php include 'connection.php';
session_start();
if (!isset($_SESSION['is_logged_in'])) {
    header("Location:login.php");
    die();
    }
	
date_default_timezone_set('Asia/Calcutta');
$today = date("d-m-Y");
$time = date("h:i A"); 
$currentyear = date("Y");

function randomPassword() {
    $alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 5; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}
$unique_id = randomPassword();


?>
<?php 
$querylogin = "select * from  users where username = '". $_SESSION["LOGIN_NAME"]."' AND password = '".$_SESSION["EMPLOYEE_PASSWORD"]."'";
$resultlogin = mysqli_query($conn,$querylogin);
while($row=mysqli_fetch_array($resultlogin))
{
$user_id = $row['user_id'];
$first_name = $row['first_name'];
$login_type=$row['login_type'];
}

?>



  
<?php
$date = $_POST['day'];
$timing = $_POST['timing'];
$unit = $_POST['unit'];
$price = $_POST['price'];


$dayOfWeek = date('l', strtotime($date));


$query1 = "insert into therapy_chart(date,day,timing,unique_id) values ('$date','$dayOfWeek','$timing','$unique_id')";
//echo $query;
$result=mysqli_query($conn,$query1);


$qry9 = "select * from therapy_chart where unique_id = '$unique_id'";
$re9 = mysqli_query($conn,$qry9);
while($row=mysqli_fetch_array($re9))
{  
$TchartID   = $row['TchartID'];
}



$therapist_id = $_POST['therapist_id'];
$patient_name = $_POST['patient_name'];
$patient_contact_no = $_POST['patient_contact_no'];
$therapyID = $_POST['therapyID'];
$branch_id = $_POST['branch_id'];
$room_no = $_POST['room_no'];

$querynp1 = "select * from therapist_master where therapist_id='$therapist_id'";
$resultmp1 = mysqli_query($conn,$querynp1);
while($row=mysqli_fetch_array($resultmp1))
{
$therapist_name = $row['therapist_name'];
$mobile_nob = $row['mobile_no'];
}



$querynp2 = "select * from therapy_master where therapyID ='$therapyID'";
$resultmp2 = mysqli_query($conn,$querynp2);
while($row=mysqli_fetch_array($resultmp2))
{
$therapy_name = $row['therapy_name'];
}

//echo "<br>";

$qrycallan = "insert into  therapy_chart_more (TchartID,therapist_name,therapist_id,patient_name,therapy_name,therapyID,appointment_date,appointment_day,appointment_time,entry_date,entry_time,entry_by,location,patient_contact_no,room_no) values ('$TchartID','$therapist_name','$therapist_id','$patient_name','$therapy_name','$therapyID','$date','$dayOfWeek','$timing','$today','$time','$user_id','$branch_id','$patient_contact_no','$room_no')";

$rcallan=mysqli_query($conn,$qrycallan);

$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'];
}


//echo "<br>";

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

//$mobile_nosa1 = "919224328435";

$message1a=nl2br("Welcome To Kanakaveda Ayurveda Your appointment is confirm,".'\n\n'."Your appointment details are ".'\n\n'."Appointment Date: ".$date.'\n\n'."Appointment Time: ".$timing.'\n\n'."Appointment with: ".$therapist_name.'\n\n'."Therapy Name: ".$therapy_name.'\n\n'."Location: ".$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;
}



}



if($mobile_nob!=''){
$mobile_nosa2 = "91".$mobile_nob;

//$mobile_nosa1 = "919224328435";

$message1a2=nl2br("Your appointment is confirm,".'\n\n'."Your appointment details are ".'\n\n'."Appointment Date: ".$date.'\n\n'."Appointment Time: ".$timing.'\n\n'."Patient Name: ".$patient_name.'\n\n'."Therapy Name: ".$therapy_name.'\n\n'."Location: ".$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_nosa2."@c.us\",\"message\":\"".$message1a2 ."\",\"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;
}



}



//exit();

?>
<script>
javascript:window.location.href="therapy_chart.php";
</script>
