<?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

if($_REQUEST['call_follow_up_id']==''){

$qrycalz4 = "insert into call_follow_up(registration_id,patient_name,step,follow_up_status,entry_date,entry_time,unique_id,entry_by) values ('$registration_id','$patient_name','1','$call_type','$today','$time','$unique_id','$user_id')";
$rcalz4=mysqli_query($conn,$qrycalz4);
//echo $qrycalz4;
$qry4z = "select * from call_follow_up where  unique_id='$unique_id'";
$re4z= mysqli_query($conn,$qry4z);
while($row=mysqli_fetch_array($re4z))
{  
$call_follow_up_id = $row['call_follow_up_id'];
}

}else {
$call_follow_up_id = $_REQUEST['call_follow_up_id'];
}

$patient_contact_no = $_POST['mobile_no'];
$patient_name = $_POST['patient_name'];
$prescription_date = $_POST['prescription_date'];
$height = $_POST['height'];
$age = $_POST['age'];
$weight = $_POST['weight'];
$pulse = $_POST['pulse'];
$HO = $_POST['HO'];
$CO = $_POST['CO'];
$KP_Drops = $_POST['KP_Drops'];
$dose_name = $_POST['dose_name'];
$dose = $_POST['dose'];
$dose_time = $_POST['dose_time'];
$case_id = $_REQUEST['case_id'];
$registration_id= $_REQUEST['registration_id'];
$appointment_id = $_REQUEST['appointment_id'];
$tablename = $_REQUEST['tablename'];

$dose_at= $_POST['dose_at'];
$next_followup_on = $_POST['next_followup_on'];
$next_followup_at = $_POST['next_followup_at'];


$query1 = "insert into  prescription (prescription_date,height,age,weight,pulse,HO,CO,KP_Drops,dose_name,dose,unique_id,dose_time,case_id,registration_id,call_follow_up_id,appointment_id,dose_at,next_followup_on,next_followup_at,tablename) values ('$prescription_date','$height','$age','$weight','$pulse','$HO','$CO','$KP_Drops','$dose_name','$dose','$unique_id','$dose_time','$case_id','$registration_id','$call_follow_up_id','$appointment_id','$dose_at','$next_followup_on','$next_followup_at','$tablename')";
//echo $query;
$result=mysqli_query($conn,$query1);

//exit();

//echo "<br>";

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



$max_contact2N = $_POST['cntcount2'];
if($max_contact2N!=''){
for ($n = 1; $n <= $max_contact2N; $n++) {	 
$herbs = $_POST['herbs'.$n];
$morning = $_POST['morning'.$n];
$noon = $_POST['noon'.$n];
$evening = $_POST['evening'.$n];
$night = $_POST['night'.$n];
$bedtime = $_POST['bedtime'.$n];
$anupan_guidelines = $_POST['anupan_guidelines'.$n];

$qrycallan = "insert into more_prescription_gw(herbs,morning,lunch,evening,dinner,anupan_guidelines,prescription_id,case_id,call_follow_up_id,registration_id,prescription_date) values ('$herbs','$morning','$lunch','$evening','$dinner','$anupan_guidelines','$prescription_id','$case_id','$call_follow_up_id','$registration_id','$prescription_date')";
$rcallan=mysqli_query($conn,$qrycallan);


//echo "<br>";
}

}


?>
<?php



$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/11656/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/11656/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;
}



}



$query2="update prescription set TchartID='$TchartID'  where prescription_id = '$prescription_id'";
$result2=mysqli_query($conn,$query2);


$query3="update patient_activity set prescription_id='$prescription_id'  where program_id = '".$_REQUEST['program_id']."'";
$result3=mysqli_query($conn,$query3);



$query4="update $tablename set prescription_status='DONE' where ibp_id = '".$_REQUEST['program_id']."'";
$result4=mysqli_query($conn,$query4);

//exit();

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