<?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 = '';
$waktu = '';


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

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

	
$kodebooking = $data->kodebooking;
$waktu = $data->waktu;

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




 $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){


    try {
// Access is granted. Add code of the operation here 
        $decoded = JWT::decode($jwt, $secret_key, array('HS256'));
     //echo $kodebooking;
//	 try {
		 
		 
		$query1 = "update antrian set cekin = :waktu where kodebooking = :kodebooking";
		//$query1 = "update antrian set kodebooking = '5' where id = 63 )";
		$stmt = $conn->prepare( $query1 );
		$stmt->bindParam(':waktu', $waktu, PDO::PARAM_STR, 50);
		$stmt->bindParam(':kodebooking', $kodebooking, PDO::PARAM_STR, 50);
		//$stmt->bindParam(':poly', $kodepoli, PDO::PARAM_STR, 50);
		$stmt->execute();
		/*
		if ($stmt->execute()) {
	echo 'kodebooking ' . $kodebooking . ' was deleted successfully.';
}
else
{
$str='';
$msg='';
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); 
$a=$stmt->errorInfo();
//echo $a;
print_r($stmt->errorInfo());
while (list ($key, $val) = each ($a)  ) {
$str .= "<br>$key -> $val";
}
$msg .= " Not able to delete record  please contact Admin: Error Message :  $str";

}
	 }
	 catch (PDOException  $e){
		 echo "Gagal menghapus data siswa:".$e->getMessage();
	 }
	 */
		http_response_code(200);

		echo json_encode(
           array(
                "metadata" => array("message" => "Ok", "code" => 200),
                //"expireAt" => $expire_claim
            ));
				
				
		
        /* echo json_encode(array(
            "message" => "Access granted:",
         //   "error" => $e->getMessage()
        )); */

    }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()
    //));
}

}
?>