<?php
include_once "./class/training.php";
include_once "./class/kriteria.php";
include_once "./class/metode_lvq.php";
include_once "./class/penilaian.php";
include_once "./class/testing.php";

$crud=new training();
$_kriteria=new kriteria();
$_lvq=new metode_lvq();
$_penilaian= new penilaian();
$_testing= new testing();

//$_lvq->LVQ();
$data=[0=>58, 1=>178, 2=>2, 3=>36, 4=>32];
$data=[0=>56.2, 1=>174.5, 2=>3, 3=>36.51, 4=>29.5]; //Seharusnya Sehat
$data=[0=>51.6, 1=>163.5, 2=>1.1, 3=>37.89, 4=>34.3]; //Seharusnya sehat
//echo $_lvq->klasifikasi($data);
//print_r($data);
?>
<div class="content-header">
  <div class="container-fluid">
  </div>
</div>

<div class="content">
  <div class="container-fluid">
    <div class="row">

        <div class="col-lg-12">
        
        <div class="card">
        
          <div class="card-header">
            <h4 class="m-0">Perhitungan</h4>
          </div>
          <div class="card-body table-responsive" style="font-size: .8em;">

			<h5>Data Training</h5>
            <table class="csstable responsive" id="tabel_data_simple" width="100%" border="0" cellpadding="4" cellspacing="0">
                <thead>
                    <tr>
                        <th width="3%" class="no_sort">No</th>
                        <th width="6%">Kode</th>                        
						<?php
						$r_kriteria = $_kriteria->tampil_kriteria();
						if($r_kriteria)
                    	foreach ($r_kriteria as $key => $d_kriteria) { ?>
                        <th width="48" style="text-align: center;"><?php echo $d_kriteria['kriteria']; ?></th>
						<?php } ?>
						<?php
						$r_kriteria = $_kriteria->tampil_keputusan();
						if($r_kriteria)
                    	foreach ($r_kriteria as $key => $d_kriteria) { ?>
                        <th width="48" style="text-align: center;"><?php echo $d_kriteria['kriteria']; ?></th>
						<?php } ?>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $no=0;
                    $result = $crud->tampil();
					if($result)
                    foreach ($result as $key => $data) {
                        $no++;
                    ?>
                    <tr>
                        <td><?php echo $no; ?></td>
                        <td><?php echo $data['kode_training']; ?></td>
						<?php
						$r_kriteria = $_kriteria->tampil_kriteria();
						if($r_kriteria)
                    	foreach ($r_kriteria as $key => $d_kriteria) { 
                            if($d_kriteria['masukan']==1){ 
                                $r_nilai = $crud->tampil_satu($data['kode_training'], $d_kriteria['id_kriteria']);
                                ?>
                                <td style="text-align: center;"><?php echo @$r_nilai[0]['nilai']; ?></td>
                            <?php } else { 
                                $r_nilai = $crud->tampil_satu_b($data['kode_training'], $d_kriteria['id_kriteria']);
                                ?>
                                <td style="text-align: center;"><?php echo @$r_nilai[0]['masukan']; ?></td>
                            <?php } ?>
						<?php } ?>
						<?php
						$r_kriteria = $_kriteria->tampil_keputusan();
						if($r_kriteria)
                    	foreach ($r_kriteria as $key => $d_kriteria) { 
							$r_nilai = $crud->tampil_satu($data['kode_training'], $d_kriteria['id_kriteria']);
						?>
                        <td style="text-align: center;"><?php echo @$r_nilai[0]['nilai']; ?></td>
						<?php } ?>

                    </tr>
                    <?php } ?>
                </tbody>
            </table>

            <h5>Data Training [Bobot]</h5>
            <table class="csstable responsive" id="tabel_data_simple2" width="100%" border="0" cellpadding="4" cellspacing="0">
                <thead>
                    <tr>
                        <th width="3%" class="no_sort">No</th>
                        <th width="6%">Kelas</th>                        
                        <?php
                        $r_kriteria = $_kriteria->tampil_kriteria();
                        if($r_kriteria)
                        foreach ($r_kriteria as $key => $d_kriteria) { ?>
                        <th width="48" style="text-align: center;"><?php echo $d_kriteria['kriteria']; ?></th>
                        <?php } ?>
                        <?php
                        $r_kriteria = $_kriteria->tampil_keputusan();
                        if($r_kriteria)
                        foreach ($r_kriteria as $key => $d_kriteria) { ?>
                        <th width="48" style="text-align: center;"><?php echo $d_kriteria['kriteria']; ?></th>
                        <?php } ?>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $no=0;
                    $result = $crud->tampil();
                    if($result)
                    foreach ($result as $key => $data) {
                        $no++;
                    ?>
                    <tr>
                        <td><?php echo $no; ?></td>
                        <td><?php echo $data['kode_training']; ?></td>
                        <?php
                        $r_kriteria = $_kriteria->tampil_kriteria();
                        if($r_kriteria)
                        foreach ($r_kriteria as $key => $d_kriteria) { 
                            if($d_kriteria['masukan']==1){ 
                                $r_nilai = $crud->tampil_satu($data['kode_training'], $d_kriteria['id_kriteria']);
                                ?>
                                <td style="text-align: center;"><?php echo @$r_nilai[0]['bobot']; ?></td>
                            <?php } else { 
                                $r_nilai = $crud->tampil_satu_b($data['kode_training'], $d_kriteria['id_kriteria']);
                                ?>
                                <td style="text-align: center;"><?php echo @$r_nilai[0]['masukan']; ?></td>
                            <?php } ?>
                        <?php } ?>
                        <?php
                        $r_kriteria = $_kriteria->tampil_keputusan();
                        if($r_kriteria)
                        foreach ($r_kriteria as $key => $d_kriteria) { 
                            $r_nilai = $crud->tampil_satu($data['kode_training'], $d_kriteria['id_kriteria']);
                        ?>
                        <td style="text-align: center;"><?php echo @$r_nilai[0]['nilai']; ?></td>
                        <?php } ?>

                    </tr>
                    <?php } ?>
                </tbody>
            </table>

            <?php
            $_print=$_lvq->_print();
            //echo "<pre>"; print_r($_print); echo "</pre>";
            foreach($_print['iterasi'] as $key => $it){

                echo "<div style='background-color:#CCC; padding:5px; font-size:2em; font-weight:bold;'>Iterasi $it</div>";

                $nd=1;
                foreach($_print['bobot'][$it] as $keyX => $bobot){
                    $jenis_bobot=($keyX==0) ? "Awal" : "Baru";
                ?>
                    <br>
                    <h6>Bobot <?= $jenis_bobot ?></h6>
                    <table class="csstable responsive" id="" width="100%" border="0" cellpadding="4" cellspacing="0">
                        <thead>
                            <tr>
                                <th width="3%" class="no_sort">No</th>
                                <th width="15%">Kelas</th>                        
                                <?php
                                $r_kriteria = $_kriteria->tampil_kriteria();
                                if($r_kriteria)
                                foreach ($r_kriteria as $key => $d_kriteria) { ?>
                                <th width="48" style="text-align: center;"><?php echo $d_kriteria['kriteria']; ?></th>
                                <?php } ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $no=0;
                            foreach($bobot as $kelas => $bobotnya){
                                $no++;
                            ?>
                            <tr>
                                <td><?php echo $no; ?></td>
                                <td><?php echo $kelas; ?></td>
                                <?php
                                foreach($bobotnya as $k => $bbt){ ?>
                                    <td style="text-align: center;"><?php echo $bbt; ?></td>
                                <?php
                                }
                                ?>

                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>

                    <?php if($jenis_bobot=="Baru") { ?>
                        <hr style="height: 1px; background-color:red;">
                    <?php } ?>

                    <?php if(@$_print['training'][$it][$keyX]){ ?>
                    <?php if($jenis_bobot=="Awal") { ?>
                        <br>
                    <?php } ?>
                    <table class="csstable responsive" id="" width="100%" border="0" cellpadding="4" cellspacing="0">
                        <thead>
                            <tr>
                                <th>Data Ke <?= $nd; ?></th>
                                <?php
                                $jml=count($_print['training'][$it][$keyX])-1;
                                foreach($_print['training'][$it][$keyX] as $k => $data){ 
                                    $t_target=($jml==$k) ? "<b><u>Target</u></b>: " : "";
                                    ?>
                                    <th style="text-align: center;"><?php echo $t_target.$data; ?></th>
                                <?php
                                }
                                ?>
                            </tr>
                        </thead>
                    </table>
                    <?php } 

                    if(@$_print['output'][$it][$keyX]!=""){
                    ?>
                    <div style="border: 1px solid blue; padding: 10px; margin-top: 10px;">
                        <u>1. Hasil bobot masing-masing output</u><br>
                        <?php 
                        if(@$_print['bobot_output'][$it][$keyX]){
                            foreach($_print['bobot_output'][$it][$keyX] as $k => $data){
                                echo "<b>".$k."</b> : ".$data."<br>";
                            }
                        }
                        ?>
                        <u>2. Kelas Pemenang (Nilai terendah dari hasil bobot)</u><br>
                        <?php 
                        if(@$_print['output'][$it][$keyX]){
                            echo "<b><u>Output</u> : ".@$_print['output'][$it][$keyX]."</b><br>";
                        }
                        ?>
                        <u>3. Update Bobot</u><br>
                        <?php 
                        if(@$_print['output'][$it][$keyX]==@$_print['target'][$it][$keyX]){
                            echo "Karena target <u>".@$_print['target'][$it][$keyX]."</u> sama dengan output <u>".@$_print['output'][$it][$keyX]."</u>, maka <b>update bobot</b>";
                        } else {
                            echo "Karena target <u>".@$_print['target'][$it][$keyX]."</u> tidak sama dengan output <u>".@$_print['output'][$it][$keyX]."</u>, maka <b>bobot baru sama dengan bobot lama</b>";
                        }
                        ?>

                    </div>


                <?php } $nd++; } ?>
            <?php } //End Iterasi ?>

            <h5>Klasifikasi</h5>
            <div style="border: 1px solid green; padding: 10px; margin-top: 10px;">

                <?php
                $id=(int)@$_GET['id'];
                $kode=""; $nama=""; $alamat=""; $terdaftar=date("Y-m-d H:i:s");
                $result = $_testing->tampilById($id);
                foreach ($result as $key => $data) {    
                    $kode=$data['kode'];
                    $nama=$data['nama'];
                    $terdaftar=$data['terdaftar'];
                }

                $data_testing=[];   
                $result = $_kriteria->tampil_kriteria();
                foreach ($result as $key => $d_kriteria) {
                    if($d_kriteria['masukan']==1 || $d_kriteria['masukan']==2){
                        $nilai=$_penilaian->tampil_satu($id, $d_kriteria['id_kriteria']);
                        $data_testing[]=@$nilai[0]['bobot'];
                    } else {
                        $nilai=$_penilaian->tampil_satu_b($id, $d_kriteria['id_kriteria']);
                        $data_testing[]=@$nilai[0]['masukan'];
                    }
                }

                $_lvq2=new metode_lvq();
                $hasil=$_lvq2->klasifikasi($data_testing);
                $bobot_output=$_lvq2->_bobot_output_hasil;
                ?>

                Kode : <?= $kode ?><br>
                Nama : <?= $nama ?>

                <table class="csstable responsive" id="" width="100%" border="0" cellpadding="4" cellspacing="0">
                    <thead>
                        <tr>
                            <th>Data Testing</th>
                            <?php
                            foreach($data_testing as $k => $data){ 
                                ?>
                                <th style="text-align: center;"><?php echo $data; ?></th>
                            <?php
                            }
                            ?>
                        </tr>
                    </thead>
                </table>

                <u>1. Hasil bobot masing-masing output</u><br>
                <?php 
                if($bobot_output){
                    foreach($bobot_output as $k => $data){
                        echo "<b>".$k."</b> : ".$data."<br>";
                    }
                }
                ?>
                <u>2. Kelas Pemenang (Nilai terendah dari hasil bobot)</u><br>
                <?php 
                $output=current(array_keys($bobot_output, min($bobot_output)));
                echo "<b><u>Hasil Klasifikasi</u> : <span style='color:green'>".$output."</span></b><br>";
                ?>

            </div>

            <!--
            <hr>
			<a href="./export.php?tipe=pdf&p=laporan-training" target="_blank"><button type="button" name="btn_simpan" id="btn_simpan" class="btn btn-danger"><i class="fa fa-file-pdf"></i> Export Data (PDF)</button></a>  
			-->
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="modal fade" id="modalHapus" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="text-align: left;">
                <h4 class="modal-title pull-left" id="myModalLabel">Hapus Data?</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>