<?php
include_once './config/database.php';
require "../vendor/autoload.php";
use \Firebase\JWT\JWT;

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
//header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, x-token, qqq");
//header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, x-token, X-Requested-With");
//header("Authorization: *");

$kodebooking = '';
$keterangan = '';


$secret_key = "HOSMIS";
$jwt = null;
$databaseService = new DatabaseService();
$conn = $databaseService->getConnection();

$data = json_decode(file_get_contents("php://input"));

	
$kodebooking = $data->kodebooking;
$keterangan = $data->keterangan;

if($kodebooking == ""){
	echo json_encode(
            array(
                "metadata" => array("code" => 201, "message" => "kodebooking Kosong"),
				"response" => null
				));
			exit;
}


//cek antrian
$querycek="(select kodebooking from antrian where kodebooking = :kodebooking)";
		$stmt = $conn->prepare( $querycek );
		$stmt->bindParam(':kodebooking', $kodebooking, PDO::PARAM_STR, 50);
		$stmt->execute();
		$num = $stmt->rowCount();
		if($num < 1){
			echo json_encode(
            array(
                "metadata" => array("code" => 201, "message" => "Antrean Tidak Ditemukan"),
				"response" => null
				));
				exit;
		}

$querycek="(select kodebooking from antrian where kodebooking = :kodebooking and upd = 'N')";
		$stmt = $conn->prepare( $querycek );
		$stmt->bindParam(':kodebooking', $kodebooking, PDO::PARAM_STR, 50);
		$stmt->execute();
		$num = $stmt->rowCount();
		if($num < 1){
			echo json_encode(
            array(
                "metadata" => array("code" => 201, "message" => "Pasien Sudah Dilayani, Antrean Tidak Dapat Dibatalkan"),
				"response" => null
				));
				exit;
		}




 $headers = array();
    foreach($_SERVER as $key => $value) {
		if ($key == 'HTTP_X_TOKEN') {
			$headers[0] = $value;
			break;
        }
    }
$authHeader = trim($headers[0]);
//echo json_encode(array("message" => $authHeader));
$arr = explode(" ", $authHeader);


/* echo json_encode(array(
    "message" => "sd" .$arr[1]. "/". $authHeader
)); */

$jwt = $arr[0];

if($jwt){

$nomorantri="";
$tglperiksa= "";
    try {
		$query = "SELECT nomorantrean, tglperiksa
				FROM antrian
				WHERE kodebooking = :kodebooking";
		//echo $kodebooking;
		$stmt = $conn->prepare( $query );
		$stmt->bindParam(':kodebooking', $kodebooking, PDO::PARAM_STR, 50);
		$stmt->execute();
		$num = $stmt->rowCount();
		//echo $num;
		if($num > 0){
			while($x = $stmt->fetch(PDO::FETCH_ASSOC)){
				$nomorantri = $x["nomorantrean"];
				$tglperiksa= $x["tglperiksa"];
			//	echo $nomorantri;
			//	echo $tglperiksa;
			}
			//echo $x["nomorantrean"];
			//echo $x["tglperiksa"];
			//echo $nomorantri;
		}
		
		//bikin file
$isi = $nomorantri . "^" . $tglperiksa; //"ini adalah data yang akan ditambahkan kedalam file"; //string data yang akan dimasukan kedalam file.txt
$file = "D:/DELETE.txt";
$fhandle = fopen($file, 'w') or die("can’t open file"); //membuka “file.txt” jika file tidak ada didirektori maka dengan mode “w” file.txt akan dibuat secara otomatis didirektori tsb.
fwrite($fhandle, $isi); //untuk menambahkan data “$isi” kedalam file.txt
fclose($fhandle); //menutup file.txt

//delay 3 detik
sleep(3);

// Access is granted. Add code of the operation here 
        $decoded = JWT::decode($jwt, $secret_key, array('HS256'));
     //echo $kodebooking;
	 //try {
		 
		 
		$query1 = "DELETE FROM antrian where kodebooking = :kodebooking";
		//$query1 = "update antrian set kodebooking = '5' where id = 63 )";
		$stmt = $conn->prepare( $query1 );
		$stmt->bindParam(':kodebooking', $kodebooking, PDO::PARAM_STR, 50);
		//$stmt->bindParam(':poly', $kodepoli, PDO::PARAM_STR, 50);
		$stmt->execute();
		
		http_response_code(200);

		echo json_encode(
           array(
                "metadata" => array("message" => "Ok", "code" => 200),
                //"expireAt" => $expire_claim
            ));
				
				
		
       
    }catch (Exception $e){
echo json_encode(
            array(
                "metadata" => array("code" => 201, "message" => "Access denied. " . $e->getMessage()),
				"response" => null
				));
    //http_response_code(401);

    //echo json_encode(array(
    //    "message" => "Access denied.",
    //    "error" => $e->getMessage()
    //));
}

}
?>