<?php
	require_once("config/koneksi.php");
?>

<link type="text/css" rel="stylesheet" href="development-bundle/themes/ui-lightness/ui.all.css" />
    
    <script src="development-bundle/jquery-1.8.0.min.js"></script>
    <script src="development-bundle/ui/ui.core.js"></script>
    <script src="development-bundle/ui/ui.datepicker.js"></script>
    <script src="development-bundle/ui/i18n/ui.datepicker-id.js"></script>
    <script type="text/javascript">
            $(document).ready(function(){
                $("#tgllahir").datepicker({
                    dateFormat : "mm/dd/yy",
                    changeMonth : true,
                    changeYear : true
                });
            });
        </script>
            
<!-- end jq -->
<script>
function confirmdelete(delUrl) {
   if (confirm("Anda yakin ingin menghapus?")) {
      document.location = delUrl;
   }
}
</script>

<?php 
switch(@$_GET['act']){
  // Tampil Data administrasi
  default:
  $jlhattrib = mysql_result(mysql_query("SELECT COUNT(*) as jum FROM tb_atribut"),0); // Jumlah atrribut
  	?>
    <h2>Dataset</h2>
<center><input type="button" name="button"  value="Tambah DataSet" class="button" onclick="javascript:window.location='media.php?module=dataset&act=forminput'"></center>
<table width="100%">
<tr class="data">
	<th width="50" class="data" rowspan="2">No. </th>
	<th class="data" rowspan="2">Nama</th>
    <th class="data" colspan="<?php echo $jlhattrib;?>">Nilai</th>
    <th class="data" rowspan="2">Aksi</th>
</tr>
<tr>
  	<?php
    	for($i=1; $i<=$jlhattrib; $i++){
			echo "<th class='data'>$i</th>";
		}
	?>
</tr>
<?php
				$query = mysql_query("SELECT * FROM tb_dataset ORDER BY id_dataset asc");
				$no = 1;
				while ($r = mysql_fetch_array($query)){
					//$data = explode(',',$r['dataset']);
					
					echo "<TR class='data'>
							<TD class='data'><input style='background:#e3e3e3; border:1px solid #cecece' size='1' 
									type='text' value='$no' name='id'></TD>
							<TD class='data'><input size='30' type='text' value='$r[nm_dataset]' name='nm_data'></TD>";
							
							$q_nilaiatribut = mysql_query("SELECT * FROM tb_tmpdataset join tb_nilaiatribut 
															on tb_tmpdataset.id_nilaiatribut=tb_nilaiatribut.id_nilaiatribut where 
															tb_tmpdataset.id_dataset='$r[id_dataset]'");
							while ($r_nilaiatribut = mysql_fetch_array($q_nilaiatribut)){
								echo "<TD class='data'><INPUT style='background:#e3e3e3; border:1px solid #cecece' 
										type='text' size='2' name='data$i' value='$r_nilaiatribut[nm_nilaiatribut]' readonly='on'/></TD>";
							}
							
							echo "
							<TD class='data'><a href=media.php?module=dataset&act=tampil&id=$r[id_dataset] title='Tampil' class='with-tip'>
   <center><img src='images/view.png'></a>
   
   <a href=media.php?module=dataset&act=edit&id=$r[id_dataset] title='Edit' class='with-tip'>
   <img src='images/edit.gif'></a>
   
   <a href=javascript:confirmdelete('media.php?module=dataset&act=hapus&id=$r[id_dataset]') 
   title='Hapus' class='with-tip'>
   <img src='images/cancel.gif'></center></a></TD>
						  </TR>";
					$no++;
				}
				?>
</table>
<hr />
<h2>Keterangan : </h2>
<table border="0">
<?php
	$tampil=mysql_query("SELECT * FROM tb_atribut ORDER BY id_atribut asc");
    $no = 1;
    while ($r=mysql_fetch_array($tampil)){
		echo "
			<tr>
				<td width='4'>$no</td>
				<td width='5'>=</td>
				<td>$r[nm_atribut]</td>
			</tr>
		";
		$no++;
	}
?>
</table>
    <?php
  break;
   case "hapusdatauji":
  	mysql_query("TRUNCATE tb_dataset");
	echo "<script>window.alert('Sukses Menghapus Semua Data Uji');
			window.location=('media.php?module=dataset')</script>";
  break;
  case "forminput":
  	?>
    <h2>Input Dataset</h2>
                <hr>
<form action="media.php?module=dataset&act=simpandata" method="post" id="datasetinistrasi" name="datasetinistrasi" 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 Data</td>
    <td class="data" width="5">:</td>
	<td class="data"><input type="text" name="id_dataset" class="pendek"
    value="<?php echo kode_auto("tb_dataset","S","1","3");?>" readonly /></td>
 </tr>
 <tr class="data">
	<td class="data" width="200">Nama Anak</td>
    <td class="data" width="5">:</td>
	<td class="data"><input type="text" name="nm_dataset" class="panjang"/></td>
 </tr>
 <tr class="data">
	<td class="data">Jenis Kelamin</td>
    <td class="data">:</td>
	<td class="data">
    <select name="jkel" id="jkel" required>
    	<option value="">--Jenis Kelamin--</option>
        <option value="L">Laki-Laki</option>
        <option value="P">Perempuan</option>
      </select>
    </td>
 </tr>
 <tr class="data">
	<td class="data" width="200">Tempat Lahir</td>
    <td class="data" width="5">:</td>
	<td class="data"><input type="text" name="tempatlahir" class="panjang"/></td>
 </tr>
 <tr class="data">
	<td class="data" width="200">Tanggal Lahir</td>
    <td class="data" width="5">:</td>
	<td class="data"><input type="text" name="tgllahir" id="tgllahir" class="pendek"/></td>
 </tr>
 </table>
 <table class="data">
 <tr class="data">
	<td colspan="4" align="center" class="data"><h2>PENILAIAN JENIS PERILAKU</h2></td>
 </tr>
 <?php
 	$q_atribut=mysql_query("SELECT * FROM tb_atribut ORDER BY id_atribut asc");
    $no=1;
    while ($r_atribut=mysql_fetch_array($q_atribut)){
		echo "
		<tr class='data'>
			<td class='data' width='30' align='center'>$no</td>
			<td class='data'>$r_atribut[nm_atribut]</td>
    		<td class='data'>:</td>
			<td class='data'>";
			?>
            <input type="hidden" class="sedang" value="<?php echo $r_atribut['id_atribut'];?>" name="id_atribut<?php echo $no;?>" />
            <select name="data<?php echo $no;?>">
            	<option value="">-- Nilai Atribut --</option>
                <?php
				$q_nilai=mysql_query("SELECT * FROM tb_nilaiatribut where id_atribut='$r_atribut[id_atribut]' ORDER BY id_nilaiatribut asc");
    			while ($r_nilai=mysql_fetch_array($q_nilai)){
					echo "<option value='$r_nilai[id_nilaiatribut]'>$r_nilai[nm_nilaiatribut]</option>";
				}
				?>
            </select>
            <?php
		echo "</td>
 		</tr>
		";	
		$no++;
	}
 ?>
 <tr class="data">
	<td colspan="4" align="center" class="data"><hr /></td>
 </tr>
 <tr class="data">
	<td colspan="4" 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 "simpandata":
		$jlhattrib = mysql_result(mysql_query("SELECT COUNT(*) as jum FROM tb_atribut"),0); // Jumlah atribut
		$data[] = "";
		$id_atribut[] = "";
		$tmp = "";
		//$tot = 0;
		for($i=1; $i<=$jlhattrib; $i++){
			$data[$i] = $_POST['data'.$i.'']; 
			$id_atribut[$i] = $_POST['id_atribut'.$i.'']; 
			$query=mysql_query("insert into tb_tmpdataset(
							id_dataset,
							id_atribut,
							id_nilaiatribut
						)values(
							'$_POST[id_dataset]',
							'".$id_atribut[$i]."',
							'".$data[$i]."'
							)");
			//$tmp = implode(',',$data);
		}	
		
		$query=mysql_query("insert into tb_dataset(
							id_dataset,
							nm_dataset,
							jkel,
							tempatlahir,
							tgllahir
						)values(
							'$_POST[id_dataset]',
							'$_POST[nm_dataset]',
							'$_POST[jkel]',
							'$_POST[tempatlahir]',
							'$_POST[tgllahir]'
							)");
							
		if($query){
			?><script language="javascript">alert("Data sukses di proses...")</script><?php
			?><script language="javascript">document.location.href="media.php?module=dataset"</script><?php
		}else{
			echo mysql_error();
		}
  break;
  
  case "hapus":
  	$query=mysql_query("DELETE FROM tb_dataset WHERE id_dataset='$_GET[id]'");
		if($query){
			?><script language="javascript">alert("Data sudah dihapus !")</script><?php
			?><script language="javascript">document.location.href="media.php?module=dataset"</script><?php
		}else{
			echo mysql_error();
		}
  break;
  
}
  	?>