<?php
// Manage Kategori Produk ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='kategori'){
	echo "<div class='post_title' style='margin-bottom:10px; position:relative;'><b>List atau Daftar Semua kategori Produk Pada Optik<hr></b></div>";
	echo " <input class='linkkk' type=button value='Tambah Kategori' onclick=\"window.location.href='media.php?module=tambahkategori';\"><hr>
	<div id='container'>
			<div id='demo'>
<table class='display' id='example'>
	<thead>
          <tr bgcolor='#2e2e2e'>
			<th width='50px' >No</th>
			<th>Nama Kategori Produk</th>
			<th>Lensa</th>
			<th>Pesanan</th>
			<th style='width:100px; text-align:center;'>Action</th>
		</tr>
	</thead>
	<tfoot>
		<tr style='border: 2px solid #e3e3e3;'>
			<th style='border: none;'>No</th>
			<th style='border: none;'>Cari Nama Kategori Produk</th>
			<th style='border: none;'></th>
			<th style='border: none;'></th>
			<th style='border: none;'></th>
		</tr>
	</tfoot>";
	echo "<tbody>";
	$no=1;
	$tampil = mysql_query("SELECT * FROM rb_kategori_produk ORDER BY id_kategori DESC");
    while($r=mysql_fetch_array($tampil)){
	  if(($no % 2)==0){ $warna="#ffffff"; }else{ $warna="#E1E1E1"; }
       echo "<tr bgcolor=$warna><td class='read_only'>$no</td>
             <td>$r[nama_kategori]</td>
             <td>$r[status]</td>
             <td>$r[pesanan]</td>
             <td><center>
			 <a class='linkkk' href=edit-kategori-$r[id_kategori].html>Edit</a>
			 <a class='linkkk' style='color:red' href='media.php?module=kategori&delete=$r[id_kategori]'' onclick=\"return confirm('Anda yakin ingin hapus data ini?')\">X</a></center></td></tr>";
			 $no++;
		}
		if (isset($_GET[delete])){
			mysql_query("DELETE FROM rb_kategori_produk WHERE id_kategori='$_GET[delete]'");
			echo "<script>document.location='kategori.html';</script>";
		}
echo "	</tbody>
 </table>
</div></div>";
}

elseif ($_GET[module]=='laporanaset'){
		echo "<div class='post_title' style='margin-bottom:10px; position:relative;'><b>Laporan Aset Pada Optik<hr></b></div>";
	echo "<div id='container'>
			<div id='demo'>
<table class='display' id='example'>
	<thead>
          <tr bgcolor='#2e2e2e'>
			<th width='50px' >No</th>
			<th>Nama Kategori Produk</th>
			<th>Harga Modal</th>
			<th>Harga Jual</th>
		</tr>
	</thead>
	<tfoot>
		<tr style='border: 2px solid #e3e3e3;'>
			<th style='border: none;'>No</th>
			<th style='border: none;'>Cari Nama Kategori Produk</th>
			<th style='border: none;'></th>
			<th style='border: none;'></th>
		</tr>
	</tfoot>";
	echo "<tbody>";
	$no=1;
	$tampil = mysql_query("SELECT * FROM rb_kategori_produk where pesanan='Tidak' ORDER BY id_kategori DESC");
    while($r=mysql_fetch_array($tampil)){
    	$jumlahp = mysql_fetch_array(mysql_query("SELECT sum(harga_pokok*stok) as total_pokok FROM rb_produk where id_kategori='$r[id_kategori]'"));
    	$jumlahj = mysql_fetch_array(mysql_query("SELECT sum(harga*stok) as total_jual FROM rb_produk where id_kategori='$r[id_kategori]'"));
	  if(($no % 2)==0){ $warna="#ffffff"; }else{ $warna="#E1E1E1"; }
       echo "<tr bgcolor=$warna><td class='read_only'>$no</td>
             <td>$r[nama_kategori]</td>
             <td>Rp ".format_rupiah($jumlahp[total_pokok])."</td>
             <td>Rp ".format_rupiah($jumlahj[total_jual])."</td>
            </tr>";
			 $no++;
		}

		$totjumlahp = mysql_fetch_array(mysql_query("SELECT sum(harga_pokok*stok) as total_pokok FROM rb_produk a JOIN rb_kategori_produk b ON a.id_kategori=b.id_kategori where b.pesanan='Tidak'"));
    	$totjumlahj = mysql_fetch_array(mysql_query("SELECT sum(harga*stok) as total_jual FROM rb_produk a JOIN rb_kategori_produk b ON a.id_kategori=b.id_kategori where b.pesanan='Tidak'"));
		echo "<tr bgcolor='#9c9c9c'><td style='color:#fff' class='read_only'>X</td>
             <td></td>
             <td style='color:red'>Rp ".format_rupiah($totjumlahp[total_pokok])."</td>
             <td style='color:red'>Rp ".format_rupiah($totjumlahj[total_jual])."</td>
            </tr>";

		if (isset($_GET[delete])){
			mysql_query("DELETE FROM rb_kategori_produk WHERE id_kategori='$_GET[delete]'");
			echo "<script>document.location='kategori.html';</script>";
		}
echo "	</tbody>
 </table>
</div></div>";


}elseif ($_GET[module]=='tambahkategori'){
	if (isset($_POST[submit])){
		  mysql_query("INSERT INTO rb_kategori_produk VALUES('','$_POST[nama_kategori]','$_POST[b]','$_POST[c]')");
		  echo "<script>document.location='kategori.html';</script>";
	}
echo "<div class='post_title' style='margin-left:20%;'><b>Tambah Data Kategori Produk.</b></div><hr><br/>
          <form method=POST action=''>
          <center><table width='60%'>
          <tr><td class='td'>Nama Kategori</td>   <td class='td'>:</td><td class='td'> <input type=text name='nama_kategori' style='width:100%'></td></tr>
          <tr><td class='td'>Lensa</td>   <td class='td'>:</td><td class='td'><input type=radio name='b' value='Ya'> Ya
          																  <input type=radio name='b' value='Tidak' checked> Tidak
          													</td></tr>
          <tr><td class='td'>Pesanan</td>   <td class='td'>:</td><td class='td'><input type=radio name='c' value='Ya'> Ya
          																  <input type=radio name='c' value='Tidak' checked> Tidak
          													</td></tr>
          <tr><td class='td' colspan=3><br/><input class='button-reply' type=button value=Batal onclick=self.history.back()>
							<input name='submit' class='button-reply' type=submit value=Simpan></td></tr>
          </table></center></form>"; 
}

elseif ($_GET[module]=='editkategori'){
	if (isset($_POST[submit])){
		 mysql_query("UPDATE rb_kategori_produk SET nama_kategori = '$_POST[nama_kategori]', status = '$_POST[b]', pesanan = '$_POST[c]' WHERE  id_kategori = '$_POST[id]'");
		echo "<script>document.location='kategori.html';</script>";
	}
$r=mysql_fetch_array(mysql_query("SELECT * FROM rb_kategori_produk WHERE id_kategori='$_GET[id]'"));
echo "<div class='post_title' style='margin-left:20%;'><b>Edit Data Kategori Produk.</b></div><hr><br/>
          <form method=POST action=''>
		  
          <center><table width='60%'><input type=hidden name='id' value='$r[id_kategori]' style='width:100%'>
          <tr><td class='td'>Nama Kategori</td>   <td class='td'>:</td><td class='td'> <input type=text name='nama_kategori' value='$r[nama_kategori]' style='width:100%'></td></tr>
          <tr><td class='td'>Lensa</td>   <td class='td'>:</td><td class='td'>"; 
          														if ($r[status]=='Ya'){
          															echo "<input type=radio name='b' value='Ya' checked> Ya
          																  <input type=radio name='b' value='Tidak'> Tidak";
          														}else{
          															echo "<input type=radio name='b' value='Ya'> Ya
          																  <input type=radio name='b' value='Tidak' checked> Tidak";
          														}
          														echo "</td></tr>
          <tr><td class='td'>Pesanan</td>   <td class='td'>:</td><td class='td'>"; 
          														if ($r[pesanan]=='Ya'){
          															echo "<input type=radio name='c' value='Ya' checked> Ya
          																  <input type=radio name='c' value='Tidak'> Tidak";
          														}else{
          															echo "<input type=radio name='c' value='Ya'> Ya
          																  <input type=radio name='c' value='Tidak' checked> Tidak";
          														}
          														echo "</td></tr>
          <tr><td class='td' colspan=3><br/><input class='button-reply' type=button value=Batal onclick=self.history.back()>
							<input name='submit' class='button-reply' type=submit value=Simpan></td></tr>
          </table></center></form>"; 
}