<html>

<script type="text/javascript">
    
	function show(id){
	
		window.location.href = "http://127.0.0.1/booking/patrons.php?id=" + id;
	}
	function newuser(){
	
		window.location.href = "http://127.0.0.1/booking/patrons.php";
	}
	
	function del(id){
	
		window.location.href = "http://127.0.0.1/booking/del_patrons.php?id=" + id;
	}
	
</script>
<form action='manage_patrons.php' method="post">
<?
	$conn = new mysqli('localhost', 'root', 'tester123','book_store');
	if ($conn->connect_error) {
		die("Connection failed: " . $conn->connect_error);
	}else{
		//echo "ok";
	}
	$sql = "select * from patrons";	
	$result = $conn->query($sql);		
	
	$cmd = $_GET['cmd'];
	$id = $_GET['id'];
	
	$txt_barcode='';
	$s_name='';
	$f_name='';
	$l_name='';
	
	
	if($id != ''){
		$sql_edit = "select * from patrons where p_barcode='".$id."'";	
		$resultedit = $conn->query($sql_edit);
		if ($result->num_rows > 0) {
			while($rowedit = $resultedit->fetch_assoc()) {
				$p_barcode= $rowedit['p_barcode'];
				$s_name=$rowedit['s_name'];
				$f_name=$rowedit['f_name'];
				$l_name=$rowedit['l_name'];
			}
		}
	}
		
?>

<h3>Manage Patrons Page</h3>
<table width ='60%' align='center' >
	<tr>
		<td width='20%' >Barcode</td>
		<td>
		<input type='text' name='txt_barcode' id='txt_barcode' value='<?=$p_barcode?>'disabled>
		<input type='hidden' name='h_txt_barcode' id='h_txt_barcode' value='<?=$p_barcode?>'>
		
		</td>
	</tr>
	<tr>
		<td>คำนำหน้า</td>
		<td><input type='text' name='txt_s_name' id='txt_s_name' value='<?=$s_name?>'required></td>
	</tr>
	<tr>
		<td>ชื่อ</td>
		<td><input type='text' name='txt_f_name' id='txt_f_name' value='<?=$f_name?>'required></td>
	</tr>
	<tr>
		<td>นามสกุล</td>
		<td><input type='text' name='txt_l_name' id='txt_l_name' value='<?=$l_name?>'required></td>
	</tr>
	<tr>
	<td></td>
		<td>
			<input type='submit'>
			<input type='button' value='New' onclick="newuser();">
		</td>
		
	</tr>
</table>
<br/>
<br/>
<br/>


<table width ='80%' border='1'  align='center'>
<tr>
	<td>Barcode</td>
	<td>คำนำหน้า</td>
	<td>ชื่อ</td>
	<td>นามสกุล</td>
	<td>จัดการ</td>
</tr>


<?
if ($result->num_rows > 0) {
		while($row = $result->fetch_assoc()) {
			$txt_p_barcode = $row['p_barcode'];
			echo "<tr><td>".$row['p_barcode']."</td>";
			echo "<td>".$row['s_name']."</td>";
			echo "<td>".$row['f_name']."</td>";
			echo "<td>".$row['l_name']."</td>
			
			<td>";
			
			?>
				<input type='button' value='ลบ' onclick="del('<?=$txt_p_barcode;?>');">
				
				<input type="button" value="แก้ไข" onclick="show('<?=$txt_p_barcode;?>');">	
				<?
				
			echo "</td>
		<tr>";
		}	
	}


?>
	
	
</tr>
</table>


</form>



</html>