<?php
/**
 * Created by PhpStorm.
 * User: apracha@adm
 * Date: 03/04/2017
 * Time: 20:12
 */

switch(@$_GET['act']) {
    // Tampil Data atribut
    default:
        $jum_data = mysql_result(mysql_query("SELECT COUNT(*) as jum FROM tb_datalatih"), 0);

        if($jum_data <= 0){
            ?><script language="javascript">alert("Proses tidak bisa dilakukan, data kosong !")</script><?php
            ?><script language="javascript">document.location.href="media.php?module=datalatih"</script><?php
        }else {

            $q_proses = mysql_query("SELECT * FROM tb_datalatih ORDER BY id_datalatih");

            while ($r_proses = mysql_fetch_array($q_proses)) {


                $q_tampildata = mysql_query("SELECT * FROM tb_datalatih WHERE id_datalatih='$r_proses[id_datalatih]'");
                $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
                ?>
                <div id="perhitungan" style="display:none;">
                    <h1>Dataset</h1>
                    <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>
                        </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>

                    <h1>Perhitungan</h1>
                    <?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='$r_proses[id_datalatih]'");
                        $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, 2) . "</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 "<th class='data'>Propabilitas</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], 4) . "</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]), 4) . "</td>";
                        echo "<td class='data' align='right'>" . number_format(($jumlah_dicari[$i] / $jlhData), 4) . "</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 = '$r_proses[id_datalatih]'");
                    echo "</table>";

                    ?>
                </div>

            <?php
            }
            ?>
            <script language="javascript">alert("Data Sukses di proses !")</script><?php
            ?>
            <script language="javascript">document.location.href = "media.php?module=proses&act=hasil"</script><?php
        }
        break;

    case "hasil":
        ?>
<h1>Data Sasaran Kinerja</h1>
    <table class="data">
        <tr class="data">
            <th class="data" width="40">ID</th>
            <th class="data">Nama Data</th>
            <th class="data" width="100">Hasil</th>
            <th width="50" 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;
                }
            }
            $thlahir = date_format(date_create($r['tgllahir']),'Y');

            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>
<?php
        break;
}