<?php 
$PID = "master_referensi";
$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");

if (strlen($_GET[jns]) > 0 ) {
	$ket = getFromTable("select keterangan from tbl_referensi where id = '000' and kd = '".$_GET[jns]."'");
}
title("<img src='images/judul-setting.png' width='65' align='absmiddle' >  Master Referensi $ket");
echo "<BR>";

if($_POST[actions] == "tambah"){
	$cek1 = getFromTable("select count(kd) from tbl_referensi where kd = '".$_POST[jns]."'");
	$cek2 = str_pad(((int) $cek1), 3, "0", STR_PAD_LEFT);
	if ($_POST[jns] == "ROM" ) { 
        
    } else {
		mysql_query("insert into tbl_referensi (id, kd, keterangan) values ('$cek2', '".$_POST[jns]."', '".$_POST[ket]."')");
	}
	
	//echo "insert into tbl_referensi (id, kd, keterangan) values ('$cek2', '".$_POST[jns]."', '".$_POST[ket]."')";
	echo "<script language='JavaScript'>document.location='$SC?p=$PID&jns=".$_POST[jns]."'</script>";
}
if($_POST[actions] == "ubah"){
	$idx = $_GET['idx'];
	$cek1 = getFromTable("select count(kd) from tbl_referensi where kd = '".$_POST[jns]."'");
	$cek2 = str_pad(((int) $cek1), 3, "0", STR_PAD_LEFT);
	
	if ($_POST[jns] == "ROM" ) { 
        mysql_query("update tbl_referensi set keterangan = '".$_POST[ket]."', keterangan2 = '".$_POST[ket2]."',
        harga = ".$_POST[harga].", breakfast = ".$_POST[breakfast].", suplies = ".$_POST[suplies].",
        utilities = ".$_POST[utilities].", revenue = ".$_POST[revenue]." where id =$idx and kd='".$_POST[jns]."'");
        
    } else {
		mysql_query("update tbl_referensi set keterangan = '".$_POST[ket]."' where id =$idx and kd='".$_POST[jns]."'");
	}
	
	//echo "insert into tbl_referensi (id, kd, keterangan) values ('$cek2', '".$_POST[jns]."', '".$_POST[ket]."')";
	echo "<script language='JavaScript'>document.location='$SC?p=$PID&jns=".$_POST[jns]."'</script>";
}



if ($_GET[act] == "tambah") {
    
    ?>
    <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>Item</td>
                <td>: <input type="text" name="ket" value="" required="required"></td>   
            </tr>
            <?php if ($_GET[jns] == "ROM" ) { ?>
            <tr>
                <td>Description</td>
                <td>: <textarea name="ket2" required="required"></textarea></td>   
            </tr>
            <tr>
                <td>Price</td>
                <td>: <input type="text" name="harga" value="0" required="required"></td>   
            </tr>
            <tr>
                <td>Breakfast</td>
                <td>: <input type="text" name="breakfast" value="0" required="required"></td>   
            </tr>
            <tr>
                <td>Suplies</td>
                <td>: <input type="text" name="suplies" value="0" required="required"></td>   
            </tr>
            <tr>
                <td>Utilities</td>
                <td>: <input type="text" name="utilities" value="0" required="required"></td>   
            </tr>
            <tr>
                <td>Revenue</td>
                <td>: <input type="text" name="revenue" value="0" required="required"></td>   
            </tr>
            <?php }?>
            <tr>
                <td>&nbsp;</td>
                <td>: <input type="submit" value="Simpan"></td>   
            </tr>
        </table>
    </form>
    <?php
}else if ($_GET[act] == "edit") {
     $sql = "select * from tbl_referensi where kd = '".$_GET[jns]."' and id = '".$_GET['idx']."' ";
	  @$r1 = mysql_query($sql,$con);
	  $ro=@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_form">
            <tr>
                <td>Item</td>
                <td>: <input type="text" name="ket" value="<?=$ro->keterangan;?>" required="required"></td>   
            </tr>
            <?php if ($_GET[jns] == "ROM" ) { ?>
            <tr>
                <td>Description</td>
                <td>: <textarea name="ket2" required="required"><?=$ro->keterangan2;?></textarea></td>   
            </tr>
            <tr>
                <td>Price</td>
                <td>: <input type="text" name="harga" value="<?=$ro->harga;?>" required="required"></td>   
            </tr>
            <tr>
                <td>Breakfast</td>
                <td>: <input type="text" name="breakfast" value="<?=$ro->breakfast;?>" required="required"></td>   
            </tr>
            <tr>
                <td>Suplies</td>
                <td>: <input type="text" name="suplies" value="<?=$ro->suplies;?>" required="required"></td>   
            </tr>
            <tr>
                <td>Utilities</td>
                <td>: <input type="text" name="utilities" value="<?=$ro->utilities;?>" required="required"></td>   
            </tr>
            <tr>
                <td>Revenue</td>
                <td>: <input type="text" name="revenue" value="<?=$ro->revenue;?>" required="required"></td>   
            </tr>
            <?php }?>
            <tr>
                <td>&nbsp;</td>
                <td>: <input type="submit" value="Ubah"></td>   
            </trb
        </table>
    </form>
    <?php
} else {
    ?>
    <div class="ui three column stackable grid">
  		<div class="column">
		<h4 class="ui top attached block header">Filter Type</h4>
		<form>
			<input type="hidden" name="p" value="<?php echo $PID;?>"/>
			<?php echo query_referensi("jns",$_GET[jns],"  "," onChange='submit()'");?>
		</form> 
		</div>
	</div>
		
    <?php
    $sql = "select * from tbl_referensi where kd = '".$_GET[jns]."' and id != '000' ";
    @$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 Kecamatan"/></a>
            <?php } else { echo "&nbsp;"; } ?>
            </th>
            <th class="gridtable_th">ID</th>
            <th class="gridtable_th">TYPE</th>
            <th class="gridtable_th">ITEMS</th>
            <?php if ($_GET[jns] == "ROM" ) { ?>
            <th class="gridtable_th">DESCRIPTIONS</th>
            <th class="gridtable_th">PRICE</th>
            <th class="gridtable_th">BREAKFAST</th>
            <th class="gridtable_th">SUPLIES</th>
            <th class="gridtable_th">UTILITIES</th>
            <th class="gridtable_th">REVENUE</th>
            <?php }?>
        </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>
                <a href="<?php echo "$SC?p=$PID&act=edit&idx=$row[id]&jns=$_GET[jns]";?>"><img src="images/icon-edit.png" /></a>
            </td>
            <td class="gridtable_td" align="center"><?php echo $row["id"];?></td>
            <td class="gridtable_td" align="center"><?php echo $row["kd"];?></td>
            <td class="gridtable_td"><?php echo ucwords(strtolower($row["keterangan"]));?></td>
            <?php if ($_GET[jns] == "ROM" ) { ?>
            <td class="gridtable_td"><?php echo ucwords(strtolower($row["keterangan2"]));?></td>
            <td class="gridtable_td" align="right"><?php echo number_format($row["harga"],2,',','.');?></td>
            <td class="gridtable_td" align="right"><?php echo number_format($row["breakfast"],2,',','.');?></td>
            <td class="gridtable_td" align="right"><?php echo number_format($row["suplies"],2,',','.');?></td>
            <td class="gridtable_td" align="right"><?php echo number_format($row["utilities"],2,',','.');?></td>
            <td class="gridtable_td" align="right"><?php echo number_format($row["revenue"],2,',','.');?></td>
            <?php } ?>
        </tr>
        <?php
            }
        ?>
    </table>
    <?php
}
?>
