<script type="text/javascript">
    var s5_taf_parent = window.location;
    function popup_print() {
        window.open('laporan/hasil.php','page','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=700,left=50,top=50,titlebar=yes')
    }
</script>
<?php
/**
 * Created by PhpStorm.
 * User: apracha[adm]
 * Date: 11/11/2016
 * Time: 21:12
 */
switch(@$_GET['act']){
    // Tampil Data
    default:
        ?>
            <h2>Data PAD</h2>
            <form method='post' enctype='multipart/form-data' action='media.php?module=proses_excel1'>
                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=datalatih&act=hapusdata';\">";
                $jlhattrib = mysql_result(mysql_query("SELECT COUNT(*) as jum FROM tb_atribut"),0); // Jumlah atrribut
                ?>

            </form><br>
            <center><input type="button" name="button"  value="Tambah Data PAD" class="button" onclick="javascript:window.location='media.php?module=datalatih&act=forminput'"></center>
            <br>
            <center><input type="button" name="button"  value="Proses" class="button" onclick="javascript:window.location='media.php?module=proses'"></center>
            <p><a onclick="popup_print()" target="_blank" title="Print dan Preview"><img src="images/pdf-icon.jpeg" border="0"/></a></p>
            <table class="data">
                <tr class="data">
                    <th class="data" width="40">ID</th>
                    <th class="data">Nama PAD</th>
                    <th class="data" width="150">Hasil</th>
                    <th width="80" 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);
                $thsekarang = date('Y');
                $tampil=mysql_query("SELECT * FROM tb_datalatih
							ORDER BY id_datalatih 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><a href=media.php?module=datalatih&act=proses&id=$r[id_datalatih] title='Tampilkan Proses' class='with-tip'>
   	$r[id_datalatih]</a></center></td>
	<td class='data'>$r[nama]</td>
	<td class='data'>$r[hasil]</td>
    <td class='data' align='center'>

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

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

   </td></tr>";
                    $no++;
                }
                ?>
            </table>
            <br />
<?php
// membuat nomor halaman
        $total_record = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tb_datalatih"),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=datalatih&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=datalatih&hal=$i>$i</a> ";
                }
            }
        }
        if(@$hal < $total_halaman){
            $next = ($page + 1);
            echo "<a href=media.php?module=datalatih&hal=$next>>></a>";
        }
        echo "</center><br/>";
        ?>
        <?php
        break;
    case "user":
        ?>
        <h2>Data PAD</h2>
        <p><a onclick="popup_print()" target="_blank" title="Print dan Preview"><img src="images/pdf-icon.jpeg" border="0"/></a></p>
        <table class="data">
            <tr class="data">
                <th class="data" width="40">ID</th>
                <th class="data">Nama PAD</th>
                <th class="data" width="150">Hasil</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);
            $thsekarang = date('Y');
            $tampil=mysql_query("SELECT * FROM tb_datalatih
							ORDER BY id_datalatih asc");

            $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><a href=media.php?module=datalatih&act=proses&id=$r[id_datalatih] title='Tampilkan Proses' class='with-tip'>
   	$r[id_datalatih]</a></center></td>
	<td class='data'>$r[nama]</td>
	<td class='data'>$r[hasil]</td></tr>";
                $no++;
            }
            ?>
        </table>

    <?php
        break;

    case "hapusdata":
        mysql_query("TRUNCATE tb_datalatih");
        echo "<script>window.alert('Sukses Menghapus Semua Data');
			window.location=('media.php?module=datalatih')</script>";
        break;
    case "forminput":
        ?>
        <h2>Input Data PAD</h2>
        <hr>
        <form action="media.php?module=datalatih&act=simpandata" method="post" id="dataanakinistrasi" name="dataanakinistrasi" 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_datalatih" class="pendek"
                                            value="<?php echo kode_auto("tb_datalatih","D","1","4");?>" readonly /></td>
                </tr>
                <tr class="data">
                    <td class="data" width="200">PAD Mingguan</td>
                    <td class="data" width="5">:</td>
                    <td class="data"><input type="text" name="nama" 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 where status_atribut <> 'Dicari' 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'>";
                    ?>
                    <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] | $r_nilai[ket]</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 "edit":
        $q_tampildata = mysql_query("SELECT * FROM tb_datalatih WHERE id_datalatih='$_GET[id]'");
        $r_data    = mysql_fetch_array($q_tampildata);
        $data = explode(',',$r_data['data']);
        ?>
        <h2>Edit Data</h2>
        <hr>
        <form action="media.php?module=datalatih&act=update" method="post" id="dataanakinistrasi" name="dataanakinistrasi" 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_datalatih" class="pendek"
                                            value="<?php echo $r_data['id_datalatih'];?>" readonly /></td>
                </tr>
                <tr class="data">
                    <td class="data" width="200">PAD Mingguan</td>
                    <td class="data" width="5">:</td>
                    <td class="data"><input type="text" name="nama" class="panjang" value="<?php echo $r_data['nama'];?>"/></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 where status_atribut <> 'Dicari' 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'>";
                    ?>
                    <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");
                        //$no=1;
                        while ($r_nilai=mysql_fetch_array($q_nilai)){
                            if($r_nilai['id_nilaiatribut']==$data[$no])
                                $cek = "selected";
                            else
                                $cek = "";
                            echo "<option value='$r_nilai[id_nilaiatribut]' $cek>$r_nilai[nm_nilaiatribut]</option>";
                            //$no++;
                        }
                        ?>
                    </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="Ubah" class="button" /> </td>
                </tr>
            </table>
        </form>
        <?php
        break;

    case "proses" :
        $q_tampildata = mysql_query("SELECT * FROM tb_datalatih WHERE id_datalatih='$_GET[id]'");
        $r_data    = mysql_fetch_array($q_tampildata);
        $data = explode(',',$r_data['data']);
        $jlhattrib = mysql_result(mysql_query("SELECT COUNT(*) as jum FROM tb_atribut"),0); // Jumlah atrribut
        ?>
        <h2>Detail Data</h2>
        <form action="" method="post" id="datalatih" name="datalatih" enctype="multipart/form-data">
            <table width="100%">
                <tr class="data">
                    <td colspan="3" align="center" class="data"><hr /></td>
                </tr>
                <tr class="data">
                    <td width="200" class="data" width="150">ID Data</td>
                    <td class="data" width="5">:</td>
                    <td class="data"><?php echo $r_data['id_datalatih'];?></td>
                </tr>
                <tr class="data">
                    <td class="data">PAD</td>
                    <td class="data">:</td>
                    <td class="data"><?php echo $r_data['nama'];?></td>
                </tr>
                <tr class="data">
                    <td colspan="3" align="center" class="data"><hr /></td>
                </tr>
            </table>
            <table width="100%">
                <tr class="data">
                    <td colspan="4" align="center" class="data"><h2>PENILAIAN</h2></td>
                </tr>
                <?php
                $q_atribut=mysql_query("SELECT * FROM tb_atribut where status_atribut <> 'Dicari' ORDER BY id_atribut asc");
                $no=1;
                while ($r_atribut=mysql_fetch_array($q_atribut)){
                    $q_nilaiatribut=mysql_query("SELECT * FROM tb_nilaiatribut where id_nilaiatribut='$data[$no]'");
                    $r_nilaiatribut=mysql_fetch_array($q_nilaiatribut);
                    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' width='150'>$r_nilaiatribut[nm_nilaiatribut]</td>
 		</tr>
		";
                    $no++;
                }
                ?>
                <tr class="data">
                    <td colspan="4" align="center" class="data"><hr /></td>
                </tr>
            </table>
        </form>
        <div id="perhitungan" style="display:none;">
        <h2>Dataset</h2>
        <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">PAD</th>
                <th class="data" colspan="<?php echo $jlhattrib;?>">Nilai</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 = 20;  // 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'>$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 "
						  </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=datalatih&act=proses&id=$_GET[id]&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=datalatih&act=proses&id=$_GET[id]&hal=$i>$i</a> ";
                }
            }
        }
        if(@$hal < $total_halaman){
            $next = ($page + 1);
            echo "<a href=media.php?module=datalatih&act=proses&id=$_GET[id]&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>
        <br />
        <h2>Perhitungan</h2>
        <?php
        $queryatributdicari = mysql_query("SELECT * FROM tb_atribut WHERE status_atribut = 'Dicari'");
        $dataatributdicari = mysql_fetch_array($queryatributdicari)
        ?>
        <table width="100%">
            <tr class="data">
                <th class="data" width="30">No.</th>
                <th class="data">Atribut Diketahui</th>
                <th class="data" width="100">Nilai Atribut</th>
                <th class="data" width="100">Atribut Dicari ?<br /><?php echo $dataatributdicari['nm_atribut'];?></th>
                <th class="data" width="100">Jumlah Dataset</th>
                <th class="data" width="100">Jumlah Dataset Dicari</th>
                <th class="data" width="80">Total Nilai</th>
            </tr>
            <?php
            $queryatributdicari = mysql_query("SELECT * FROM tb_atribut WHERE status_atribut = 'Dicari'");
            $dataatributdicari = mysql_fetch_array($queryatributdicari);

            $nilai_akhir = array();
            $jumlah_dicari = array();
            $katakata = "";

            $querynilaiatributdicari = mysql_query("SELECT * FROM tb_nilaiatribut WHERE
								id_atribut = '$dataatributdicari[id_atribut]' order by id_nilaiatribut");
            $jumlahnilaiatributdicari = mysql_num_rows($querynilaiatributdicari);
            $i=0;
            while ($datanilaiatributdicari = mysql_fetch_array($querynilaiatributdicari)){
                $nilai_akhir[$i] = 1;
                $jumlah_dicari[$i] = 0;
                $i++;
            }

            $edit = mysql_query("SELECT * FROM tb_datalatih WHERE id_datalatih='$_GET[id]'");
            $r    = mysql_fetch_array($edit);
            $data = explode(',',$r['data']);

            $x=1;
            $queryatributdiketahui = mysql_query("SELECT * FROM tb_atribut WHERE status_atribut = 'Diketahui' ORDER BY id_atribut");
            while ($dataatributdiketahui = mysql_fetch_array($queryatributdiketahui)){

                $id_nilai_atribut_diketahui = $data[$x];
                $querynilaiatributdiketahui = mysql_query("SELECT * FROM tb_nilaiatribut WHERE id_nilaiatribut = '$id_nilai_atribut_diketahui'");
                $datanilaiatributdiketahui = mysql_fetch_array($querynilaiatributdiketahui);

                $queryatributdicari = mysql_query("SELECT * FROM tb_atribut WHERE status_atribut = 'Dicari'");
                $dataatributdicari = mysql_fetch_array($queryatributdicari);

                $querynilaiatributdicari = mysql_query("SELECT * FROM tb_nilaiatribut
			WHERE id_atribut = '$dataatributdicari[id_atribut]' order by id_nilaiatribut");
                $jumlahnilaiatributdicari = mysql_num_rows($querynilaiatributdicari);
                $i=0;
                while ($datanilaiatributdicari = mysql_fetch_array($querynilaiatributdicari)){
                    $jumlah_dataset_diketahui = 0;
                    $jumlah_dataset_dicari = 0;

                    $querydatasetnomor = mysql_query("SELECT id_dataset FROM tb_tmpdataset GROUP BY id_dataset ORDER BY id_dataset");
                    while ($datadatasetnomor = mysql_fetch_array($querydatasetnomor)){
                        $querycek1 = mysql_query("SELECT * FROM tb_tmpdataset WHERE
					id_dataset = '$datadatasetnomor[id_dataset]' AND id_nilaiatribut = '$datanilaiatributdiketahui[id_nilaiatribut]'");
                        $jumlahcek1 = mysql_num_rows($querycek1);
                        $querycek2 = mysql_query("SELECT * FROM tb_tmpdataset WHERE
					id_dataset = '$datadatasetnomor[id_dataset]' AND id_nilaiatribut = '$datanilaiatributdicari[id_nilaiatribut]'");
                        $jumlahcek2 = mysql_num_rows($querycek2);
                        if (($jumlahcek1 >= 1) && ($jumlahcek2 >= 1)) {
                            $jumlah_dataset_diketahui = $jumlah_dataset_diketahui + 1;
                        }if ($jumlahcek2 >= 1){
                            $jumlah_dataset_dicari = $jumlah_dataset_dicari + 1;
                        }
                    }

                    $hasil = $jumlah_dataset_diketahui / $jumlah_dataset_dicari;
                    $nilai_akhir[$i] = $nilai_akhir[$i] * $hasil;
                    $jumlah_dicari[$i] = $jumlah_dataset_dicari;

                    echo "<tr>";
                    if ($i==0){
                        echo "<td class='data' rowspan=\"".$jumlahnilaiatributdicari."\" align='center'>".$x."</td>";
                        echo "<td class='data' rowspan=\"".$jumlahnilaiatributdicari."\">".$dataatributdiketahui['nm_atribut']."</td>";
                        echo "<td class='data' rowspan=\"".$jumlahnilaiatributdicari."\">".$datanilaiatributdiketahui['nm_nilaiatribut']."</td>";

                        if ($katakata != "") { $katakata = $katakata.", "; }
                        $katakata = $katakata.$dataatributdiketahui['nm_atribut']." : ".$datanilaiatributdiketahui['nm_nilaiatribut'];
                    }
                    echo "<td class='data'>".$datanilaiatributdicari['nm_nilaiatribut']."</td>";
                    echo "<td class='data' align='right'>".$jumlah_dataset_diketahui."</td>";
                    echo "<td class='data' align='right'>".$jumlah_dataset_dicari."</td>";
                    echo "<td class='data' align='right'>".number_format($hasil,8)."</td>";
                    echo "</tr>";

                    $i++;
                }
                $x++;
            }


            ?>
        </table>

        <?php
        echo "
	<br><br>
	<table width='100%'>";
        echo "<tr class='data'>";
        echo "<th class='data'>Atribut Dicari</th>";
        echo "<th class='data'>Nilai<br/>Atribut</th>";
        echo "<th class='data'>Total Nilai</th>";
        echo "<th class='data'>Jumlah<br/>Dataset Dicari</th>";
        echo "<th class='data'>Hasil Akhir</th>";
        echo "</tr>";

        $nilai_atribut_dicari_terbesar = "";
        $hasil_akhir_dicari_terbesar = 0;

        $queryatributdicari = mysql_query("SELECT * FROM tb_atribut WHERE status_atribut = 'Dicari'");
        $dataatributdicari = mysql_fetch_array($queryatributdicari);

        $querynilaiatributdicari = mysql_query("SELECT * FROM tb_nilaiatribut
									WHERE id_atribut = '$dataatributdicari[id_atribut]' order by id_nilaiatribut");
        $jumlahnilaiatributdicari = mysql_num_rows($querynilaiatributdicari);
        $i=0;
        while ($datanilaiatributdicari = mysql_fetch_array($querynilaiatributdicari))
        {
            echo "<tr>";
            if ($i==0) { echo "<td class='data' rowspan=\"".$jumlahnilaiatributdicari."\">".$dataatributdicari['nm_atribut']."</td>"; }
            echo "<td class='data'>".$datanilaiatributdicari['nm_nilaiatribut']."</td>";
            echo "<td class='data' align='right'>".number_format($nilai_akhir[$i],10)."</td>";
            echo "<td class='data' align='right'>".$jumlah_dicari[$i]."</td>";
            echo "<td class='data' align='right'>".number_format(($nilai_akhir[$i] * $jumlah_dicari[$i]),8)."</td>";
            echo "</tr>";

            if (($i==0) || ($hasil_akhir_dicari_terbesar < ($nilai_akhir[$i] * $jumlah_dicari[$i])))
            {
                $nilai_atribut_dicari_terbesar = $dataatributdicari['nm_atribut']." : ".$datanilaiatributdicari['nm_nilaiatribut'];
                $hasil_autis = $datanilaiatributdicari['nm_nilaiatribut'];
                $hasil_akhir_dicari_terbesar = ($nilai_akhir[$i] * $jumlah_dicari[$i]);
            }

            $i++;
        }
        $query=mysql_query("UPDATE tb_datalatih SET
							hasil = '$hasil_autis'
						WHERE id_datalatih = '$_GET[id]'");
        echo "</table>";

        ?>
        </div>
        <br />
        <center>
            <input type="button" value="View Proses Perhitungan" onclick="document.getElementById('perhitungan').style.display='block';" class="button"/>
        </center>
        <H2>Hasil Analisa</H2>

        <table width="100%">
            <tr>
                <th class='data'>Atribut Dicari</th>
                <th class='data'>Nilai Atribut</th>
                <th class='data'>Hasil Akhir</th>
            </tr>
            <?php
            $queryatributdicari = mysql_query("SELECT * FROM tb_atribut WHERE status_atribut = 'Dicari'");
            $dataatributdicari = mysql_fetch_array($queryatributdicari);

            $querynilaiatributdicari = mysql_query("SELECT * FROM tb_nilaiatribut WHERE
									id_atribut = '$dataatributdicari[id_atribut]' order by id_nilaiatribut");
            $jumlahnilaiatributdicari = mysql_num_rows($querynilaiatributdicari);
            $i=0;
            while ($datanilaiatributdicari = mysql_fetch_array($querynilaiatributdicari))
            {
                ?>
                <tr>
                    <?php if ($i==0) { ?>
                        <td class='data' rowspan="<?php echo $jumlahnilaiatributdicari; ?>"><?php echo $dataatributdicari['nm_atribut']; ?></td>
                    <?php } ?>
                    <td class='data'><?php echo $datanilaiatributdicari['nm_nilaiatribut']; ?></td>
                    <td class='data' align="right"><?php echo number_format(($nilai_akhir[$i] * $jumlah_dicari[$i]),8); ?></td>
                </tr>
                <?php
                $i++;
            }
            ?>
        </table>
        <br />
        Hasilnya, Kemungkinan Besar <b><?php echo $nilai_atribut_dicari_terbesar; ?></b> dengan Nilai Terbesar = <b><?php echo number_format($hasil_akhir_dicari_terbesar,8); ?></b>
		<center><input type="button" name="button"  value="Tampilkan Grafik" class="button" onclick="javascript:window.location='media.php?module=datalatih&act=grafik'"></center>
        <?php
        break;
		
		case "grafik":
			$jlhNaik = mysql_result(mysql_query("
					SELECT COUNT(*) as jum from tb_datalatih
                  where hasil = 'NAIK'
				  "),0);
			$jlhTurun = mysql_result(mysql_query("
					SELECT COUNT(*) as jum from tb_datalatih
                  where hasil = 'TURUN'
				  "),0);
			?>
			<h2>Tampilan Grafik</h2>
			<div class="containerxx">
            <canvas id="myChart" width="100" height="100"></canvas>
        </div>
        <script>
            var ctx = document.getElementById("myChart");
            var myChart = new Chart(ctx, {
                type: 'pie',
                data: {
                    labels: ["NAIK", "TURUN"],
                    datasets: [{
                            label: 'Grafik',
                            data: [<?php echo $jlhNaik;?>, <?php echo $jlhTurun;?>],
                            backgroundColor: [
								'rgba(54, 162, 235, 0.2)',
                                'rgba(255, 99, 132, 0.2)'
                            ],
                            borderColor: [
								'rgba(54, 162, 235, 1)',
                                'rgba(255,99,132,1)'
                            ],
                            borderWidth: 1
                        }]
                },
                options: {
                    scales: {
                        yAxes: [{
                                ticks: {
                                    beginAtZero: true
                                }
                            }]
                    }
                }
            });
        </script>
			<?php
		break;
		
    case "simpandata":
            $t=mysql_fetch_array($hasil);
            $thsekarang = date('Y');
            //$thlahir = date_format(date_create($_POST['tgllahir']),'Y');
            //$umur = $thsekarang - $thlahir;
            $jlhattrib = mysql_result(mysql_query("SELECT COUNT(*) as jum FROM tb_atribut"),0); // Jumlah atribut
            $data[] = "";
            $tmp = "";
            //$tot = 0;
            for($i=1; $i<$jlhattrib; $i++){
                $data[$i] = $_POST['data'.$i.''];
                //$tot = $tot + $_POST['data'.$i.''];
                $tmp = implode(',',$data);
            }

            $query=mysql_query("insert into tb_datalatih(
							id_datalatih,
							nama,
							data)
						values(
							'$_POST[id_datalatih]',
							'$_POST[nama]',
							'$tmp')");

            if($query){
                ?><script language="javascript">alert("Data sukses di proses...")</script><?php
                ?><script language="javascript">document.location.href="media.php?module=datalatih&act=proses&id=<?php echo $_POST['id_datalatih'];?>"</script><?php
            }else{
                echo mysql_error();
            }

        break;

    case "update":
        $thsekarang = date('Y');
        //$thlahir = date_format(date_create($_POST['tgllahir']),'Y');
        //$umur = $thsekarang - $thlahir;
        $jlhattrib = mysql_result(mysql_query("SELECT COUNT(*) as jum FROM tb_atribut"),0); // Jumlah atribut
        $data[] = "";
        $tmp = "";
        for($i=1; $i<$jlhattrib; $i++){
            $data[$i] = $_POST['data'.$i.''];
            $tmp = implode(',',$data);
        }

        $query=mysql_query("
							UPDATE tb_datalatih SET
							nama = '$_POST[nama]',
							data = '$tmp'
							WHERE id_datalatih = '$_POST[id_datalatih]'");

        if($query){
            ?><script language="javascript">alert("Data sudah diubah !")</script><?php
            ?><script language="javascript">document.location.href="media.php?module=datalatih&act=proses&id=<?php echo $_POST['id_datalatih'];?>"</script><?php
        }else{
            echo mysql_error();
        }
        break;

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