<?php include 'connection.php';
session_start();
if (!isset($_SESSION['is_logged_in'])) {
    header("Location:login.php");
    die();
    }
	
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();



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;
}

date_default_timezone_set('Asia/Calcutta');
$today = date("Y-m-d");
$time = date("h:i A"); 
$currentyear = date("Y");
	
$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'];
}	
	
// Data to be inserted
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$age = $_POST['age'];
$city = $_POST['city'];
$mobile_no = $_POST['mobile_no'];
$health_issue = $_POST['health_issue'];
$how_to_consult = $_POST['how_to_consult'];
$reference = $_POST['reference'];
$tablename = $_POST['tablename'];


$patient_name = $first_name.' '.$last_name;


$query1 = "insert into  $tablename (first_name,last_name,age,mobile_no,city,how_to_consult,reference,health_issue,entry_by,entry_date,entry_time,unique_id) values ('$first_name','$last_name','$age','$mobile_no','$city','$how_to_consult','$reference','$health_issue','$user_id','$today','$time','$unique_id')";
//echo $query;
$result=mysqli_query($conn,$query1);


$queryconqf2 = "select * from  $tablename  where unique_id = '$unique_id'";
$resultconqf2 = mysqli_query($conn,$queryconqf2);
while($row=mysqli_fetch_array($resultconqf2))
{
$program_id = $row['ibp_id'];
}



if($_POST['appointment_date']!=''){

$call_type = 'Appointment';

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




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

$query2 = "insert into patient_register (unique_id,patient_name,mobile_no,location,call_follow_up_id) values ('$unique_id','$patient_name','$mobile_no','$city','$call_follow_up_id')";
$result2=mysqli_query($conn,$query2);

$queryconqf = "select * from  patient_register where unique_id = '$unique_id'";
$resultconqf = mysqli_query($conn,$queryconqf);
while($row=mysqli_fetch_array($resultconqf))
{
$registration_id = $row['registration_id'];
}

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



$queryleadqf="update $tablename set registration_id='$registration_id',call_follow_up_id='$call_follow_up_id' where ibp_id = '$program_id'";
$resultleadqf=mysqli_query($conn,$queryleadqf);


$doctor_id = $_POST['doctor_id'];
$appointment_date = $_POST['appointment_date'];
$appointment_time = $_POST['appointment_time'];
$branch_id = $_POST['how_to_consult'];





if(is_numeric($branch_id)){

$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'];
}
}else{
$branch_name = $branch_id;
}

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


$query5 = "insert into appointment_details (unique_id,appointment_date,appointment_time,doctor_name,patient_name,location,program,doctor_id,branch_id,branch_name,mobile_no,call_follow_up_id,registration_id) values ('$unique_id','$appointment_date','$appointment_time','$doctor_name','$patient_name','$location','$program','$doctor_id','$branch_id','$branch_name','$mobile_no','$call_follow_up_id','$registration_id')";
//echo $query;
$result5=mysqli_query($conn,$query5);

//echo "<br>";

$queryconqf = "select * from  appointment_details where unique_id = '$unique_id'";
$resultconqf = mysqli_query($conn,$queryconqf);
while($row=mysqli_fetch_array($resultconqf))
{
$appointment_id = $row['appointment_id'];
}


    $time = $_POST['appointment_time'];
    $amPm = getAmPm($time);
    $appointment_time01 = $time.' '.$amPm;

//echo "<br>";

$querycall="update $tablename set appointment='$appointment_id' where ibp_id = '$program_id'";
$resultcall=mysqli_query($conn,$querycall);

//echo "<br>";

$querycf="update call_follow_up set follow_up_status = '$call_type',registration_id='$registration_id',appointment_date='$appointment_date',note='$note' where call_follow_up_id = '$call_follow_up_id'";
$resultcf=mysqli_query($conn,$querycf);

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

//$mobile_nosa1 = "919224328435";

$message1a=nl2br("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;
}



$query = "insert into quotation_follow_up (unique_id,call_follow_up_id,patient_name,conversation_date,conversation_time,conversation_details,Nreminder_date,Nreminder_time,appointment_date,appointment_time,call_type,program_id,program_name,registration_id,branch_name,branch_id,appointment_id,note,doctor_id,doctor_name) values ('$unique_id','$call_follow_up_id','$patient_name','$today','$time','$note','$Nreminder_date','$Nreminder_time','$appointment_date','$appointment_time','$call_type','$program_id','$program_name','$registration_id','$branch_name','$branch_id','$appointment_id','$note','$doctor_id','$doctor_name')";
//echo $query;
$resulr=mysqli_query($conn,$query);

}

}

//exit();


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