<?php
/**
 * Created by PhpStorm.
 * User: apracha[adm]
 * Date: 11/11/2016
 * Time: 22:43
 */
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>
            <form method='post' enctype='multipart/form-data' action='media.php?module=proses_excel'>
                Data Excel: <input style='border:1px solid #000;'name='userfile' type='file'>
                <input name='upload' type='submit' value='Import'>
                <?php echo "<input type=button value='Hapus Semua' onclick=\"window.location.href='media.php?module=dataset&act=hapusdata';\">";
                $jlhattrib = mysql_result(mysql_query("SELECT COUNT(*) as jum FROM tb_atribut"),0); // Jumlah atrribut
                ?>

            </form>
            <center><input type="button" name="button"  value="Tambah DataSet" class="button" onclick="javascript:window.location='media.php?module=dataset&act=forminput'"></center>
            <div style="width:100%;overflow:scroll;">
            <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
                $jlhData = mysql_result(mysql_query("SELECT COUNT(*) as jum FROM tb_dataset"),0);
                if(!isset($_GET['hal'])){
                    @$page = 1;
                    @$hal = 1;
                } else {
                    @$page = $_GET['hal'];
                }
                $jmlperhalaman = 50;  // jumlah record per halaman
                $offset = (($page * $jmlperhalaman) - $jmlperhalaman);

                $query = mysql_query("SELECT * FROM tb_dataset ORDER BY id_dataset asc LIMIT $offset, $jmlperhalaman");
                $no = 1;
                while ($r = mysql_fetch_array($query)){
                    //$data = explode(',',$r['dataset']);

                    echo "<TR class='data'>
							<TD class='data' align='center'>$r[id_dataset]</TD>
							<TD class='data'>$r[nm_dataset]</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'>$r_nilaiatribut[nm_nilaiatribut]</TD>";
                    }

                    echo "
							<TD class='data' align='center'>

   <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>
            </div>
<?php
// membuat nomor halaman
        $total_record = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tb_dataset"),0);
        $total_halaman = ceil($total_record / $jmlperhalaman);
        echo "<center>Halaman :<br/>";
        $perhal=14;
        if(@$hal > 1){
            $prev = ($page - 1);
            echo "<a href=media.php?module=dataset&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=dataset&hal=$i>$i</a> ";
                }
            }
        }
        if(@$hal < $total_halaman){
            $next = ($page + 1);
            echo "<a href=media.php?module=dataset&hal=$next>>></a>";
        }
        echo "</center><br/>";
        echo "<b>Total Data : $jlhData record</b>";
        ?>
            <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 "hapusdata":
        mysql_query("TRUNCATE tb_dataset");
        mysql_query("TRUNCATE tb_tmpdataset");
        echo "<script>window.alert('Sukses Menghapus Semua Dataset');
			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","4");?>" readonly /></td>
                </tr>
                <tr class="data">
                    <td class="data" width="200">Nama Pegawai</td>
                    <td class="data" width="5">:</td>
                    <td class="data"><input type="text" name="nm_dataset" class="panjang"/></td>
                </tr>
            </table>
            <table class="data">
                <tr class="data">
                    <td colspan="4" align="center" class="data"><h2>PENILAIAN</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' width='5'>:</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
						)values(
							'$_POST[id_dataset]',
							'$_POST[nm_dataset]'
							)");

        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;

}