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

$mobile_nob = $_POST['mobile_nob'];
$appointment = $_REQUEST['appointment'];
$case_form = $_POST['case_form'];
$registration_id = $_REQUEST['registration_id'];


$querynp = "select * from  case_form_master where case_form = '$case_form'";
$resultmp = mysqli_query($conn,$querynp);
while($row=mysqli_fetch_array($resultmp))
{
$case_form_link = $row['case_form_link'];

}

$case_study_url = "https://crmsahavas.co.in/kanakveda/$case_form_link?appointment=$appointment&&username=$mobile_nob&&password=$unique_id&&case_form=$case_form&&registration_id=$registration_id";

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

//$mobile_nosa1 = "919224328435";

$message1a=nl2br("Welcome To Kanakaveda Ayurveda Your appointment is confirm, please Fill-up the Case Study form".'\n\n'.$case_study_url);

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



}

$tablename = $_REQUEST['tablename'];
$program_id = $_REQUEST['program_id'];

$querycall="update  $tablename set username = '$mobile_nob',password='$unique_id' where ibp_id = '$program_id'";
$resultcall=mysqli_query($conn,$querycf);


$queryapp="update  appointment_details set username = '$mobile_nob',password='$unique_id' where appointment_id  = '".$_REQUEST['appointment']."'";
$resultapp=mysqli_query($conn,$queryapp);

$queryapp1="update   patient_register set case_form = '$case_form'  where registration_id   = '".$_REQUEST['registration_id ']."'";
$resultapp1=mysqli_query($conn,$queryapp1);

//exit();

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