<?php 
$PID = "master_room";
$SC = $_SERVER["SCRIPT_NAME"];

require_once("lib/dbconn.php");
require_once("lib/form.php");
require_once("lib/functions.php");
require_once("lib/function_query.php");


title("<img src='images/judul-setting.png' width='65' align='absmiddle' >  Master Room");
echo "<BR>";

if($_POST[actions] == "tambah"){
	$cek1 = getFromTable("select count(id) + 1 from tbl_room ");
	
	mysql_query("insert into tbl_room (id, kd_jenis,tipe_bed, no_room, id_lock) values ('$cek1', '".$_POST[jns]."', '".$_POST[tipe_bed]."', '".$_POST[no_room]."', '".$_POST[id_lock]."')");
	//echo "insert into tbl_room (id, kd_jenis, no_room, id_lock) values ('$cek1', '".$_POST[jns]."', '".$_POST[no_room]."', '".$_POST[id_lock]."')";
	echo "<script language='JavaScript'>document.location='$SC?p=$PID&jns=".$_POST[jns]."'</script>";
}
if($_POST[actions] == "ubah"){
	$id = $_GET['idx'];
	
	mysql_query("UPDATE tbl_room SET  kd_jenis='".$_POST[jns]."',tipe_bed='".$_POST[tipe_bed]."', no_room='".$_POST[no_room]."', id_lock='".$_POST[id_lock]."' where id=$id");
	echo  "<script language='JavaScript'>document.location='$SC?p=$PID&jns=".$_POST[jns]."'</script>";
}
if($_POST[actions] == "delete"){
	$id = $_GET['idx'];
	
	mysql_query("DELETE FROM tbl_room where id=$id");
	echo  "<script language='JavaScript'>document.location='$SC?p=$PID&jns=".$_POST[jns]."'</script>";
}



if ($_GET[act] == "tambah") {
    $last = getFromTable("select max(no_room) from tbl_room where kd_jenis = '".$_GET[jns]."'");
    ?>
    <form method="POST">
    <input type="hidden" name="p" value="<?php echo $_GET[p];?>" />
    <input type="hidden" name="jns" value="<?php echo $_GET[jns];?>" />
    <input type="hidden" name="actions" value="tambah" />
        <table width="40%" border="0" class="gridtable_form">
        	<tr>
                <td>Last No. Room</td>
                <td>: <input type="text" name="last" value="<?php echo $last; ?>" readonly="readonly"></td>   
            </tr>
            <tr>
                <td>No. Room</td>
                <td>: <input type="text" name="no_room" value="" required="required"></td>   
            </tr>
            <tr>
               <td>Tipe Bed</td>
                <td>: 
				<select name="tipe_bed">
					<option value="single">Single</option>
					<option value="double">Double</option>
				</select>
				</td>   
            </tr>
			<tr>
                <td>ID KEY</td>
                <td>: <input type="text" name="id_lock" value="" required="required"></td>   
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>: <input type="submit" value="Simpan"></td>   
            </tr>
        </table>
    </form>
    <?php
}else if ($_GET[act] == "edit") {
    $last = getFromTable("select max(no_room) from tbl_room where kd_jenis = '".$_GET[jns]."'");
	$idx=$_GET['idx'];
	 $sql = "select a.*, b.keterangan from tbl_room a, tbl_referensi b 
    where a.kd_jenis = b.id and b.kd = 'ROM' and a.kd_jenis = '".$_GET[jns]."' and a.id=$idx order by a.no_room";
    
	@$r1 = mysql_query($sql,$con);
	$row=mysql_fetch_object($r1);
	
    ?>
    <form method="POST">
    <input type="hidden" name="p" value="<?php echo $_GET[p];?>" />
    <input type="hidden" name="jns" value="<?php echo $_GET[jns];?>" />
    <input type="hidden" name="actions" value="ubah" />
        <table width="40%" border="0" class="gridtable_uform">
        	<tr>
                <td>Last No. Room</td>
                <td>: <input type="text" name="last" value="<?php echo $last; ?>" readonly="readonly"></td>   
            </tr>
            <tr>
                <td>No. Room</td>
                <td>: <input type="text" name="no_room" value="<?=$row->no_room?>" required="required"></td>   
            </tr>
			 <tr>
               <td>Tipe Bed</td>
                <td>: 
				<select name="tipe_bed">
					<option value="single" <?=($row->tipe_bed)=='single'?'selected="selected"':''?> >Single</option>
					<option value="double" <?=($row->tipe_bed)=='double'?'selected="selected"':''?>>Double</option>
				</select>
				</td>   
            </tr>
            <tr>
                <td>ID KEY</td>
                <td>: <input type="text" name="id_lock" value="<?=$row->id_lock;?>" required="required"></td>   
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>: <input type="submit" value="Simpan"></td>   
            </tr>
        </table>
    </form>
    <?php
} else if ($_GET[act] == "hapus") {
    $last = getFromTable("select max(no_room) from tbl_room where kd_jenis = '".$_GET[jns]."'");
	$idx=$_GET['idx'];
	 $sql = "select a.*, b.keterangan from tbl_room a, tbl_referensi b 
    where a.kd_jenis = b.id and b.kd = 'ROM' and a.kd_jenis = '".$_GET[jns]."' and a.id=$idx order by a.no_room";
    
	@$r1 = mysql_query($sql,$con);
	$row=mysql_fetch_object($r1);
	
    ?>
    <form method="POST">
    <input type="hidden" name="p" value="<?php echo $_GET[p];?>" />
    <input type="hidden" name="jns" value="<?php echo $_GET[jns];?>" />
    <input type="hidden" name="actions" value="delete" />
        <table width="40%" border="0" class="gridtable_uform">
        	<tr>
                <td>Last No. Room</td>
                <td>: <input type="text" name="last" value="<?php echo $last; ?>" readonly="readonly"></td>   
            </tr>
            <tr>
                <td>No. Room</td>
                <td>: <input type="text" name="no_room" value="<?=$row->no_room?>"  readonly="readonly" required="required"></td>   
            </tr>
			 <tr>
               <td>Tipe Bed</td>
                <td>: 
				<select name="tipe_bed"  readonly="readonly">
					<option value="single" <?=($row->tipe_bed)=='single'?'selected="selected"':''?> >Single</option>
					<option value="double" <?=($row->tipe_bed)=='double'?'selected="selected"':''?>>Double</option>
				</select>
				</td>   
            </tr>
            <tr>
                <td>ID KEY</td>
                <td>: <input  readonly="readonly" type="text" name="id_lock" value="<?=$row->id_lock;?>" required="required"></td>   
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>: <input type="submit" value="Hapus"></td>   
            </tr>
        </table>
    </form>
    <?php
}else {
    ?>
    <div class="ui three column stackable grid">
  		<div class="column">
		<form>
			<input type="hidden" name="p" value="<?php echo $PID;?>"/>
			<?php echo query_jnsroom("jns",$_GET[jns],"  "," onChange='submit()'");?>
		</form> 
		</div>
	</div>
		
    <?php
	if($_GET[jns]!=""){
		 $sql = "select a.*, b.keterangan from tbl_room a, tbl_referensi b 
			where a.kd_jenis = b.id and b.kd = 'ROM' and a.kd_jenis = '".$_GET[jns]."' order by a.no_room ";
	}else{
		 $sql = "select a.*, b.keterangan from tbl_room a, tbl_referensi b 
			where a.kd_jenis = b.id and b.kd = 'ROM'  order by a.no_room ";
	}
   
    @$r1 = mysql_query($sql,$con);
    @$n1 = mysql_num_rows($r1);
    ?>
    <table border="1" width="100%" id='mytable' class="gridtable tableWithFloatingHeader">
        <tr>
            <th class="gridtable_th">
            <?php if (strlen($_GET[jns]) > 0 ) { ?>
            <a href="<?php echo "$SC?p=$PID&act=tambah&jns=$_GET[jns]"; ?>"><img src="images/icon-checklist.png" title="Tambah Jenis Room"/></a>
            <?php } else { echo "&nbsp;"; } ?>
            </th>
            <th class="gridtable_th">ID</th>
            <th class="gridtable_th">TYPE</th>
            <th class="gridtable_th">NO.ROOM</th>
            <th class="gridtable_th">TYPE BED</th>
            <th class="gridtable_th">ID. KEY</th>
        </tr>
        <?php
            while (@$row = mysql_fetch_array($r1)) {
        ?>
        <tr>
            <td class="gridtable_td" align="center">
                <a href="<?php echo "$SC?p=$PID&act=hapus&idx=$row[id]&jns=$_GET[jns]";?>"><img src="images/icon-delete.png" /></a>
            </td>
            <td class="gridtable_td" align="center"><?php echo $row["id"];?></td>
            <td class="gridtable_td"><a href="<?php echo "$SC?p=$PID&act=edit&idx=$row[id]&jns=$_GET[jns]";?>"><?php echo ucwords(strtolower($row["keterangan"]));?></a></td>
            <td class="gridtable_td" align="center"><?php echo $row["no_room"];?></td>
            <td class="gridtable_td" align="center"><?php echo $row["tipe_bed"];?></td>
            <td class="gridtable_td" align="center"><?php echo $row["id_lock"]; ?></td>
        </tr>
        <?php
            }
        ?>
    </table>
    <?php
}
?>
