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

$crud=new testing();
$_kriteria=new kriteria();
$_kriteria_nilai=new kriteria_nilai();
$_penilaian=new penilaian();
$_training=new training();
$_knn=new mKNN();

$stat	= @$_GET['stat'];
$id		= (int)$crud->escapeString(@$_REQUEST['id']);
$f      = $crud->escapeString(@$_REQUEST['f']);

$data=array();
$nk=3; $kode=""; $nama=""; $alamat=""; $terdaftar=date("Y-m-d H:i:s");

$result = $crud->tampilById($id);
foreach ($result as $key => $data) {	
    $nk=$data['nk'];
    $kode=$data['kode'];
    $nama=$data['nama'];
    $terdaftar=$data['terdaftar'];
}

$id_testing=$id;
$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_testing, $d_kriteria['id_kriteria']);
        $data_testing[$d_kriteria['id_kriteria']]=@$nilai[0]['bobot'];
    } else {
        $nilai=$_penilaian->tampil_satu_b($id_testing, $d_kriteria['id_kriteria']);
        $data_testing[$d_kriteria['id_kriteria']]=@$nilai[0]['masukan'];
    }
}

$hasil=$_knn->_knn($data_testing, $nk);
?>

<div class="row">

	<div class="col-md-12">

		<div class="card">

			<div class="card-header">
				<h4 class="m-0">Perhitungan</h4>
			</div>
			
			<?php 
			if(@$_SESSION['error']!="") { ?>
				<div class="alert alert-danger alert-dismissable" style=" padding: 5px 10px; margin: 20px; opacity:0.8;">
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
					<i class="icon fa fa-check"></i>
					<?php echo @$_SESSION['error']; ?>
				</div>
			<?php unset($_SESSION['error']); } ?>

			<form role="form" class="form-horizontal" action="?p=testing-input&stat=<?php echo $stat; ?>" method="post" enctype="multipart/form-data" id="frInput" name="frInput" data-toggle="validator">
				<input type='hidden' name='id' id='id' value='<?php echo $id; ?>'>
				<input type='hidden' name='f' id='f' value='<?php echo $f; ?>'>						
				<div class="card-body col-md-12 col-lg-6 col-12" style="font-size: .95em;">
						<div class="form-group row">
							<label class="col-sm-4 col-form-label">Nilai K</label>
							<div class="col-sm-5">
								<input type="number" name="nk" class="form-control" data-error="Nilai K harus diisi." value="<?php echo $nk; ?>" required>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-4 col-form-label">Kode</label>
							<div class="col-sm-5">
								<input type="text" name="kode" class="form-control" data-error="Kode harus diisi dengan benar." value="<?php echo $kode; ?>" required>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-4 col-form-label">Nama</label>
							<div class="col-sm-5">
								<input type="text" name="nama" class="form-control" data-error="Nama harus diisi." value="<?php echo $nama; ?>" required>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<h4>Penilaian</h4>
						<hr>
						<?php 
						$result = $_kriteria->tampil_kriteria();
						foreach ($result as $key => $d_kriteria) {
							$nilainya=0;
							if($d_kriteria['masukan']==1){
								$nilai=$_penilaian->tampil_satu((int)$id, $d_kriteria['id_kriteria']);
								$nilainya=(int)@$nilai[0]['id_kriteria_nilai'];
							} else {
								$nilai=$_penilaian->tampil_satu_b((int)$id, $d_kriteria['id_kriteria']);
								$nilainya=@$nilai[0]['masukan'];
							}
						?>
						<div class="form-group row">
							<label class="col-sm-4 col-form-label"><?php echo $d_kriteria['kriteria']; ?></label>
							<div class="col-sm-5">
								<input type="hidden" name="kriteria[]" value="<?php echo $d_kriteria['id_kriteria']; ?>">
								<input type="hidden" name="masukan[<?php echo $d_kriteria['id_kriteria']; ?>]" value="<?php echo $d_kriteria['masukan']; ?>">									  
								<?php if($d_kriteria['masukan']==1) { ?>
									<select name="nilai[<?php echo $d_kriteria['id_kriteria']; ?>]"  class="form-control" data-error="Silahkan pilih." required>
									<option value="0">- Pilih -</option>
									<?php
										$r_kriteria_nilai = $_kriteria_nilai->tampil($d_kriteria['id_kriteria']);
										foreach ($r_kriteria_nilai as $key => $d_kriteria_nilai) {
										?>
										<option value="<?php echo $d_kriteria_nilai['id_kriteria_nilai']; ?>" <?php if($nilainya==$d_kriteria_nilai['id_kriteria_nilai']) { echo "selected"; } ?>><?php echo $d_kriteria_nilai['nilai']; ?></option>
										<?php } ?>
									</select>
								<?php } elseif($d_kriteria['masukan']==2 || $d_kriteria['masukan']==3) { ?>
									<input type="number" name="nilai[<?php echo $d_kriteria['id_kriteria']; ?>]" step="0.1" value="<?php echo $nilainya; ?>"  class="form-control" data-error="Silahkan diisi." required>
								<?php } ?>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<?php } ?>
					
						<table width="100%">
						
						<tr>
							<td></td>
						</tr>	
						</table>
				</div>
				
			</form>
			
		</div>

		<div style="clear:both; height:20px;">&nbsp;</div>
		<div class="card">

			<div class="card-header">
				<h4 class="m-0">Hasil Perhitungan</h4>
				<h6 style="border-bottom: 1px solid #CCC;">Berikut adalah hasil dari klasifikasi berdasar data training dan data input</h6>
			</div>
			<div class="card-body col-md-12 col-lg-12 col-12" style="font-size: .95em;">
				<div class="table-responsive p-0">
					<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="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 } ?>
								<th width="6%">Jarak</th>                        
							</tr>
						</thead>
						<tbody>
							<?php
							$no=0;
							foreach($hasil['data_terdekat'] as $kode => $dt){
								$no++;
								?>
								<tr>
									<td><?php echo $no; ?></td>
									<td><?php echo $kode; ?></td>
									<?php
									$r_kriteria = $_kriteria->tampil_kriteria();
									if($r_kriteria)
									foreach ($r_kriteria as $key => $d_kriteria) { 
										if($d_kriteria['masukan']==1){ 
											$r_nilai = $_training->tampil_satu($kode, $d_kriteria['id_kriteria']);
											?>
											<td style="text-align: center;"><?php echo @$r_nilai[0]['nilai']; ?></td>
										<?php } else { 
											$r_nilai = $_training->tampil_satu_b($kode, $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) { 
										if($d_kriteria['masukan']==1){ 
											$r_nilai = $_training->tampil_satu($kode, $d_kriteria['id_kriteria']);
											?>
											<td style="text-align: center;"><?php echo @$r_nilai[0]['nilai']; ?></td>
										<?php } else { 
											$r_nilai = $_training->tampil_satu_b($kode, $d_kriteria['id_kriteria']);
											?>
											<td style="text-align: center;"><?php echo @$r_nilai[0]['masukan']; ?></td>
										<?php } ?>
									<?php } ?>
									<td><?php echo $dt['jarak']; ?></td>
								</tr>
							<?php } ?>
						</tbody>
					</table>
				</div>

				<h5 style="margin-top: 20px;">Total Setiap Class</h5>
				<div class="table-responsive p-0">
					<table class="csstable responsive" id="" width="100%" border="0" cellpadding="4" cellspacing="0">
						<thead>
							<tr>
								<th width="10%">Class</th>
								<th>Total</th>                        
							</tr>
						</thead>
						<tbody>
							<?php
							$no=0;
							foreach($hasil['jumlah'] as $kelas => $jml){
								$no++;
								?>
								<tr>
									<td><?php echo $kelas; ?></td>
									<td><?php echo $jml; ?></td>
								</tr>
							<?php } ?>
						</tbody>
					</table>
				</div>

				<div class="alert alert-success" style=" padding: 5px 10px; margin-top: 20px; opacity:1;">
					<i class="icon fa fa-check"></i>
					Dari total setiap Class, maka hasil klasifikasi dari nilai input adalah Class <b><?= $hasil['hasil'] ?></b>
				</div>
				

			</div>
	
			<div class="card-footer">
                <button type="button" id="btn_batal" class="btn btn-warning" onclick="document.location='./?p=testing'">Kembali</button>
			</div>
				
			
		</div>


	</div>
</div>
