<?php
	require_once("config/koneksi.php");
?>
<!-- end jq -->
<script>
function confirmdelete(delUrl) {
   if (confirm("Anda yakin ingin menghapus?")) {
      document.location = delUrl;
   }
}
</script>

<?php
switch(@$_GET['act']){
  // Tampil Data atribut
  default:
  	?>
    <h2>Data Atribut</h2>
    <center><input type="button" name="button"  value="Tambah Data Atribut" class="button" onclick="javascript:window.location='media.php?module=atribut&act=forminput'"></center>
<table class="data">
<tr class="data">
	<th width="50" class="data">No. </th>
	<th class="data">Nama Atribut</th>
    <th width="100" class="data">Status Atribut</th>
	<th width="100" class="data">Aksi</th>
</tr>
<?php
	if(!isset($_GET['hal'])){
		@$page = 1;
		@$hal = 1;
	} else {
		@$page = $_GET['hal'];
	}
	$jmlperhalaman = 10;  // jumlah record per halaman
	$offset = (($page * $jmlperhalaman) - $jmlperhalaman);

	$tampil=mysql_query("SELECT * FROM tb_atribut ORDER BY id_atribut asc LIMIT $offset, $jmlperhalaman");

	$no = 1;
    while ($r=mysql_fetch_array($tampil)){

    $lebar=strlen($no);
    switch($lebar){
      case 1:
      {
        $g="0".$no;
        break;
      }
      case 2:
      {
        $g=$no;
        break;
      }
    }

   echo "<tr class='data'>
   <td class='data'><center>$g</center></td>
    <td class='data'>$r[nm_atribut]</td>
	<td class='data'>$r[status_atribut]</td>
    <td class='data'>

   <a href=media.php?module=atribut&act=tampil&id=$r[id_atribut] title='Tampil' class='with-tip'>
   <center><img src='images/view.png'></a>

   <a href=media.php?module=atribut&act=edit&id=$r[id_atribut] title='Edit' class='with-tip'>
   <img src='images/edit.gif'></a>

   <a href=javascript:confirmdelete('media.php?module=atribut&act=hapus&id=$r[id_atribut]')
   title='Hapus' class='with-tip'>
   <img src='images/cancel.gif'></center></a>

   </td></tr>";
      $no++;
    }
?>
</table>
<?php
// membuat nomor halaman
	$total_record = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tb_atribut"),0);
	$total_halaman = ceil($total_record / $jmlperhalaman);
	echo "<center>Halaman :<br/>";
	$perhal=4;
	if(@$hal > 1){
		$prev = ($page - 1);
		echo "<a href=media.php?module=atribut&hal=$prev> << </a> ";
	}
	if($total_halaman<=10){
	$hal1=1;
	$hal2=$total_halaman;
	}else{
	$hal1=$hal-$perhal;
	$hal2=$hal+$perhal;
	}
	if(@$hal<=5){
	$hal1=1;
	}
	if(@$hal<$total_halaman){
	$hal2=@$hal+$perhal;
	}else{
	$hal2=@$hal;
	}
	for($i = $hal1; $i <= $hal2; $i++){
		if((@$hal) == $i){
			echo "[<b>$i</b>] ";
			} else {
		if($i<=$total_halaman){
				echo "<a href=media.php?module=atribut&hal=$i>$i</a> ";
		}
		}
	}
	if(@$hal < $total_halaman){
		$next = ($page + 1);
		echo "<a href=media.php?module=atribut&hal=$next>>></a>";
	}
	echo "</center><br/>";
?>
    <?php
  break;
   case "forminput":
  	?>
    <h2>Input Data Atribut</h2>
                <hr>
<form action="media.php?module=atribut&act=simpandata" method="post" id="atribut" name="atribut" enctype="multipart/form-data">
<table class="data">
<tr class="data">
	<td colspan="3" align="center" class="data"><hr /></td>
 </tr>
 <tr class="data">
	<td class="data" width="150">Nama Atribut</td>
    <td class="data" width="5">:</td>
	<td class="data"><input type="text" name="nm_atribut" class="panjang"/></td>
 </tr>
 <tr class="data">
	<td class="data">Status Atribut</td>
    <td class="data">:</td>
	<td class="data">
    <select name="status_atribut">
    	<option value="">-- Status Atribut --</option>
        <option value="Diketahui">Diketahui</option>
        <option value="Dicari">Dicari</option>
    </select>
          </td>
 </tr>
 <tr class="data">
	<td colspan="3" align="center" class="data">
    <input type="button" name="button"  value="<< Kembali" class="button" onclick="history.go(-1)"/>
    <input type="submit" name="button"  value="Simpan" class="button" /> </td>
 </tr>
 </table>
 </form>
	<?php
  break;

  case "tampil":
  	$edit = mysql_query("SELECT * FROM tb_atribut WHERE id_atribut='$_GET[id]'");
    $r    = mysql_fetch_array($edit);
  ?>
    <h2>Detail Data Atribut</h2>
<form action="" method="post" id="atribut" name="atribut" enctype="multipart/form-data">
<table class="data">
<tr class="data">
	<td colspan="3" align="center" class="data"><hr /></td>
 </tr>
<tr class="data">
	<td width="200" class="data" width="150">ID Atribut</td>
    <td class="data" width="5">:</td>
	<td class="data"><?php echo $r['id_atribut'];?></td>
 </tr>
 <tr class="data">
	<td class="data">Nama Atribut</td>
    <td class="data">:</td>
	<td class="data"><?php echo $r['nm_atribut'];?></td>
 </tr>
 <tr class="data">
	<td class="data">Status Atribut</td>
    <td class="data">:</td>
	<td class="data"><?php echo $r['status_atribut'];?></td>
 </tr>
 <tr class="data">
	<td colspan="3" align="center" class="data"><hr /></td>
 </tr>
 <tr class="data">
	<td colspan="3" align="center" class="data">
    <input type="button" name="button"  value="<< Kembali" class="button" onclick="history.go(-1)"/></td>
 </tr>
 </table>
 </form>
 <?php
  break;

  case "edit":
  	$edit = mysql_query("SELECT * FROM tb_atribut WHERE id_atribut='$_GET[id]'");
    $r    = mysql_fetch_array($edit);
  ?>
    <h2>Edit Data Atribut</h2>
<form action="media.php?module=atribut&act=update" method="post" id="atribut" name="atribut" enctype="multipart/form-data">
<table class="data">
<tr class="data">
	<td colspan="3" align="center" class="data"><hr /></td>
 </tr>
<tr class="data">
	<td class="data" width="200">ID Atribut</td>
    <td class="data" width="5">:</td>
	<td class="data"><input type="text" name="id_atribut" class="pendek"
    value="<?php echo $r['id_atribut'];?>" readonly /></td>
 </tr>
 <tr class="data">
	<td class="data">Nama Atribut</td>
    <td class="data">:</td>
	<td class="data"><input type="text" name="nm_atribut" class="panjang" value="<?php echo $r['nm_atribut'];?>"/></td>
 </tr>
 <tr class="data">
	<td class="data">Status Atribut</td>
    <td class="data">:</td>
	<td class="data">
    <select name="status_atribut">
    	<option value="">-- Status Atribut --</option>
    <?php
		if($r['status_atribut']=="Diketahui"){
			?>
            <option value="Diketahui" selected="selected">Diketahui</option>
        	<option value="Dicari">Dicari</option>
            <?php
		}else{
			?>
            <option value="Diketahui">Diketahui</option>
        	<option value="Dicari" selected="selected">Dicari</option>
            <?php
		}
	?>
    </select>
    </td>
 </tr>
 <tr class="data">
	<td colspan="3" align="center" class="data"><hr /></td>
 </tr>
 <tr class="data">
	<td colspan="3" align="center" class="data">
    <input type="button" name="button"  value="<< Kembali" class="button" onclick="history.go(-1)"/>
    <input type="submit" name="button"  value="Ubah" class="button" /> </td>
 </tr>
 </table>
 </form>
 <?php
  break;

  case "simpandata":
  			$query=mysql_query("insert into tb_atribut(
								nm_atribut,
								status_atribut
							)values(
								'".strtoupper($_POST['nm_atribut'])."',
								'$_POST[status_atribut]'
							)");


  	 if($query){
			?><script language="javascript">alert("Data sudah tersimpan")</script><?php
			?><script language="javascript">document.location.href="media.php?module=atribut"</script><?php
		}else{
			echo mysql_error();
		}
  break;

  case "update":
  			$query=mysql_query("
							UPDATE tb_atribut SET
							nm_atribut = '".strtoupper($_POST['nm_atribut'])."',
							status_atribut = '$_POST[status_atribut]'
							WHERE id_atribut = '$_POST[id_atribut]'");

		if($query){
			?><script language="javascript">alert("Data sudah diubah !")</script><?php
			?><script language="javascript">document.location.href="media.php?module=atribut"</script><?php
		}else{
			echo mysql_error();
		}
  break;

  case "hapus":
  	$query=mysql_query("DELETE FROM tb_atribut WHERE id_atribut='$_GET[id]'");
		if($query){
			?><script language="javascript">alert("Data sudah dihapus !")</script><?php
			?><script language="javascript">document.location.href="media.php?module=atribut"</script><?php
		}else{
			echo mysql_error();
		}
  break;
}
  	?>