<?php
//UNTUK MEMBACA KARTU
require_once("../lib/dbconn.php");
$host=$pmshost;
$port=$pmsport;

$data = array();
//PERTAMA BACA DULU KALAU KOSONG BARU ISI
$package =chr(2)."0103E".chr(3); //READ CARD
$SC = $_SERVER["SCRIPT_NAME"];
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
    exit;
} else {
	  $result = socket_connect($socket, $host, $port);
	  //PERTAMA BACA DULU KALAU KOSONG BARU ISI
	  $package =chr(2)."0103E".chr(3); //READ CARD
  
		  if ($result === false) {
			$data['hasil'] = "error";
			$data['isidata'] = "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
		    echo json_encode($data);
		    exit;
		   }else {
			   socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>30, 'usec'=>10)); //SET TIME OUT 30detik
			   $test = socket_write($socket, $package, strlen($package));
			   $result = socket_read($socket,2048) or die('Could not read from socket');
				//echo $result;
				if($result){
					$nostx=str_replace(chr(2),"",$result);	
					$nordx=str_replace(chr(3),"",$nostx);	
					
					if(trim($nordx)=="03010|R|D|O|N|"){
						$data['hasil'] = "error";
						$data['isidata'] = "kartu ini kosong, harap ganti dengan kartu yang lain";
						echo json_encode($data);
						socket_close($socket);
						exit;
					}else{
						if(trim($nordx)=="03018"){ //KALAU TIDAK ADA KARTU DI SLOT
							$data['hasil'] = "error";
							$data['isidata'] = "Masukkan kartu dan ulangi proses";	
							echo json_encode($data);
						}else{
							//RESULT HERE
							$room_number="";
							$guest_name="";
							$date_checkin="";
							$date_checkout="";
							$booking_id="";
							
							$hasil = explode("|",trim($nordx));
							foreach($hasil as $key){
								
								if(substr($key,0,1)=="R"){
									$room_number = substr($key,1);
								}else if(substr($key,0,1)=="D"){
									$date_checkin = substr($key,1);
								}else if(substr($key,0,1)=="O"){
									$date_checkout = substr($key,1);
								}else if(substr($key,0,1)=="1"){
									$booking_id = substr($key,1);
								}else if(substr($key,0,1)=="N"){
									$guest_name = substr($key,1);
								}
							}
							if($date_checkin!=""){
								$year_checkin = substr($date_checkin,0,4);
								$month_checkin = substr($date_checkin,4,2);
								$day_checkin = substr($date_checkin,6,2);
								$hour_checkin = substr($date_checkin,8,2);
								$minute_checkin = substr($date_checkin,10,2);
							}
							if($date_checkout!=""){
								$year_checkout = substr($date_checkout,0,4);
								$month_checkout = substr($date_checkout,4,2);
								$day_checkout = substr($date_checkout,6,2);
								$hour_checkout = substr($date_checkout,8,2);
								$minute_checkout = substr($date_checkout,10,2);
							}
							if($booking_id!="0"){
								$url = "includes/guestfolio.php?idx=".$booking_id;
								$booking_id = '<a href="'.$url.'">'.$booking_id.'</a>';
							}else{
								$booking_id = 'staff';
							}
							//LOG HERE
								$action ="read";
								@mysql_query("insert into tbl_card_log (action, nama, room_number,checkin_date, checkout_date, result,booking_id) values ('$action','$guest_name','$room_number','$date_checkin','$date_checkout','$nordx','$booking_id')"); 
							$table = '<div class="ui bottom attached segment">';
							$table .= '<table class="gridtable_keterangan" width="100%" border="1px">';
							$table .= "<tr><td>Name</td><td>Room Number</td><td>Check In</td><td>Check Out</td><td>Booking Id</td></tr>";
							$table .= "<tr><td>$guest_name</td><td>$room_number</td><td>".$day_checkin."-".$month_checkin."-".$year_checkin." ".$hour_checkin.":".$minute_checkin."</td><td>".$day_checkout."-".$month_checkout."-".$year_checkout." ".$hour_checkout.":".$minute_checkout."</td><td>$booking_id</td></tr>";
							$table .= "</table>";
							$table .= "</div>";
			
							$data=array();
							$data['hasil'] = "sukses";
							$data['isidata'] = $table;
							echo json_encode($data);
						}
						socket_close($socket);
						exit;
					}
				
			}else{
				$data['hasil'] = "error";
				$data['isidata'] = "no reply";	
				echo json_encode($data);
				socket_close($socket);
				exit;
			}

	}
}
?>