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

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


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

if ($_GET[jns] == "tambahx") {
    $md5paz = md5($_GET[pass_user]);
    mysql_query("insert into tbl_user (nama_user, pass_user, nama_lengkap, kd_bagian) values ('".$_GET[nama_user]."', '$md5paz', '".$_GET[nama_lengkap]."', '".$_GET[gr]."')");
    //echo "insert into tbl_user (nama_user, pass_user, nama_lengkap, kd_bagian) values ('".$_GET[nama_user]."', $md5paz, '".$_GET[nama_lengkap]."', '".$_GET[gr]."')";
    //die();
    echo "<script language='JavaScript'>document.location='index2.php?p=$PID'</script>";
}elseif ($_GET[jns] == "editx") {
    mysql_query("update tbl_user set nama_user = '".$_GET[nama_user]."', nama_lengkap= '".$_GET[nama_lengkap]."', kd_bagian= '".$_GET[gr]."'  where kd_user = ".$_GET[idx]);
    //echo "update tbl_user set nama_user = '".$_GET[nama_user]."', nama_lengkap= '".$_GET[nama_lengkap]."', kd_bagian= '".$_GET[gr]."'  where kd_user = ".$_GET[idx];
    echo "<script language='JavaScript'>document.location='index2.php?p=$PID'</script>";
    
}elseif ($_GET[jns] == "aktif") {
    $md5paz = md5("123456");
    mysql_query("update tbl_user set pass_user = '$md5paz' where kd_user = ".$_GET[idx]);
    echo "<script language='JavaScript'>document.location='index2.php?p=$PID'</script>";
    
}elseif ($_GET[jns] == "hapus") {
    mysql_query("delete from tbl_user where kd_user = ".$_GET[idx]);
    echo "<script language='JavaScript'>document.location='index2.php?p=$PID'</script>";
    
}elseif ($_GET[jns] == "tambah" or $_GET[jns] == "edit") {
    
    if($_GET[jns] == "edit"){
        $r = mysql_query("select * from tbl_user where kd_user = '".$_GET[idx]."'");
        $n = mysql_num_rows($r);
        if($n > 0) $d = mysql_fetch_object($r);
        mysql_free_result($r);
        
    } 
    ?>
    <form method="GET">
    <INPUT type="hidden" name="p" value="<?php echo $_GET[p];?>" />
    
        <table width="40%" border="0">
            <tr>
                <td class="JUDUL_INPUT">Input User</td>
                <td>&nbsp;</td>
            </tr>
            <?php if($_GET[jns] == "edit"){ ?>
            <INPUT type="hidden" name="jns" value="editx" />
            <tr>
                <td class="FORM_INPUT" width="40%">ID User</td>
                <td class="FORM_INPUT" >: <input type="text" name="idx" value="<?php echo $d->kd_user;?>" readonly="readonly"></td>
            </tr>
            <?php } else { ?>
            <INPUT type="hidden" name="jns" value="tambahx" />
            <tr>
                <td class="FORM_INPUT" width="40%">ID User</td>
                <td class="FORM_INPUT" >: <input type="text" name="id" value="OTOMATIS" disabled="disabled"></td>
            </tr>
            <?php }  ?>
            
            <tr>
                <td class="FORM_INPUT">Email</td>
                <td class="FORM_INPUT" >: <input type="email" name="nama_user" value="<?php echo $d->nama_user;?>" required="required"></td>
            </tr>
            <?php if($_GET[jns] == "tambah"){ ?>
            <tr>
                <td class="FORM_INPUT">Password</td>
                <td class="FORM_INPUT" >: <input type="password" name="pass_user" value="<?php echo $d->pass_user;?>" required="required"></td>
            </tr>
            <?php } ?>
            <tr>
                <td class="FORM_INPUT">Name</td>
                <td class="FORM_INPUT" >: <input type="text" name="nama_lengkap" value="<?php echo $d->nama_lengkap;?>" required="required"></td>
            </tr>
            <tr>
                <td class="FORM_INPUT">Position</td>
                <td class="FORM_INPUT" >: <?php echo query_user("gr",$d->kd_bagian);?></td>
            </tr>
            <tr>
                <td class="FORM_INPUT">&nbsp;</td>
                <td class="FORM_INPUT" >: <input type="Submit" value="Simpan"></td>
            </tr>
        </table>
    </form>
    <?php
} else {
    $sql = "select * from tbl_user where kd_bagian != 0";
    @$r1 = mysql_query($sql,$con);
    @$n1 = mysql_num_rows($r1);
    ?>
    <form method="GET" name="Form1">
    <INPUT type="hidden" name="p" value="<?php echo $_GET[p];?>" />
    <INPUT type="submit" style="display: none;" />
    <table border="1" width="100%" id='mytable' class="gridtable tableWithFloatingHeader">
        <tr>
            <th class="gridtable_th"><a href="<?php echo "$SC?p=$PID&jns=tambah"; ?>"><img src="images/icon-checklist.png" title="Tambah Kecamatan"/></a></th>
            <th class="gridtable_th">USERNAME</th>
            <th class="gridtable_th">NAME</th>
            <th class="gridtable_th">POSITION</th>
        </tr>
        <?php
            while (@$row = mysql_fetch_array($r1)) {
        ?>
        <tr>
            <td class="gridtable_td" align="center">
                <a href="<?php echo "$SC?p=$PID&jns=edit&idx=$row[kd_user]";?>"><img src="images/icon-edit.png" /></a>
                <a href="<?php echo "$SC?p=$PID&jns=aktif&idx=$row[kd_user]";?>"><img src="images/icon-ok.png" /></a>
                <a href="<?php echo "$SC?p=$PID&jns=hapus&idx=$row[kd_user]";?>"><img src="images/icon-delete.png" /></a>
            </td>
            <td class="gridtable_td"><?php echo $row["nama_user"];?></td>
            <td class="gridtable_td"><?php echo ucwords(strtolower($row["nama_lengkap"]));?></td>
            <td class="gridtable_td"><?php echo ucwords(strtolower($row["nama_lengkap"]));?></td>
        </tr>
        <?php
            }
        ?>
    </table>
    </form>
    <?php
}

?>
