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

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

$data=array();
$data[1]="App Test";
$data[6]=3;
$nama=""; $terdaftar=date("Y-m-d H:i:s");
$hasil=[];

if(@$_POST['nk']) {
	$id				= (int)$crud->escapeString(@$_REQUEST['id']);
	$data[0]		= "";
	$data[1]		= $crud->escapeString(@$_POST['nama']);
	$data[4]		= date("Y-m-d H:i:s");
	$data[6]		= (int)@$_POST['nk'];
	$data[7]		= @$_POST['kriteria'];
	$data[8]		= @$_POST['nilai'];
	$data[9]		= @$_POST['masukan'];	
	
	//unset($_SESSION['knn_start']);
	if(!isset($_SESSION['knn_start'])) {
		$id_testing = (int)$crud->tambah($data);
		if($id_testing>0) {
			$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, $data[6]);
			//Simpan Hasil Klasifikasi
			$crud->simpan_hasil($id_testing,$hasil['hasil']);
			
			$_SESSION['knn_start']=$id_testing;
		} else {
			$_SESSION['error']='Saat ini testing tidak dapat dibuat, silahkan coba beberapa saat lagi.';
			?>
			<script language="JavaScript">window.history.back();</script>
			<?php
		}
	} else {
		$kode="";
		$result = $crud->tampilById((int)$_SESSION['knn_start']);
		foreach ($result as $key => $datax) {	
			$kode=$datax['kode'];
		}
		$data[0]=$kode;
	
		$result = $crud->ubah($data, (int)$_SESSION['knn_start']);
		if($result!=false) {
			$result = $_kriteria->tampil_kriteria();
			foreach ($result as $key => $d_kriteria) {
				if($d_kriteria['masukan']==1 || $d_kriteria['masukan']==2){
					$nilai=$_penilaian->tampil_satu((int)$_SESSION['knn_start'], $d_kriteria['id_kriteria']);
					$data_testing[$d_kriteria['id_kriteria']]=@$nilai[0]['bobot'];
				} else {
					$nilai=$_penilaian->tampil_satu_b((int)$_SESSION['knn_start'], $d_kriteria['id_kriteria']);
					$data_testing[$d_kriteria['id_kriteria']]=@$nilai[0]['masukan'];
				}
			}
			$hasil=$_knn->_knn($data_testing, $data[6]);
			//Simpan Hasil Klasifikasi
			$crud->simpan_hasil((int)$_SESSION['knn_start'],$hasil['hasil']);
		}
	}
} else {
	if(isset($_SESSION['knn_start'])) {
		$result = $_kriteria->tampil_kriteria();
		foreach ($result as $key => $d_kriteria) {
			if($d_kriteria['masukan']==1 || $d_kriteria['masukan']==2){
				$nilai=$_penilaian->tampil_satu((int)$_SESSION['knn_start'], $d_kriteria['id_kriteria']);
				$data_testing[$d_kriteria['id_kriteria']]=@$nilai[0]['bobot'];
			} else {
				$nilai=$_penilaian->tampil_satu_b((int)$_SESSION['knn_start'], $d_kriteria['id_kriteria']);
				$data_testing[$d_kriteria['id_kriteria']]=@$nilai[0]['masukan'];
			}
		}
		$hasil=$_knn->_knn($data_testing, $data[6]);
	}
}
?>

<div class="row">

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

		<div class="card">

			<div class="card-header">
				<h4 class="m-0">K-Nearest Neighbor</h4>
				<h6 style="border-bottom: 1px solid #CCC;">Silahkan masukkan nilai yang diketahui</h6>
			</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" method="post" enctype="multipart/form-data" id="frInput" name="frInput" data-toggle="validator">
				<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 @$data[6]; ?>" 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 @$data[1]; ?>" required>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<?php 
						$result = $_kriteria->tampil_kriteria();
						foreach ($result as $key => $d_kriteria) {
							$nilainya=0;
							if(isset($_SESSION['knn_start'])){
								if($d_kriteria['masukan']==1){
									$nilai=$_penilaian->tampil_satu((int)$_SESSION['knn_start'], $d_kriteria['id_kriteria']);
									$nilainya=(int)@$nilai[0]['id_kriteria_nilai'];
								} else {
									$nilai=$_penilaian->tampil_satu_b((int)$_SESSION['knn_start'], $d_kriteria['id_kriteria']);
									$nilainya=@$nilai[0]['masukan'];
								}
							}
							if(!isset($_SESSION['knn_start'])) { $nilainya=""; }
						?>
						<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; ?>" placeholder="<?php echo $d_kriteria['keterangan']; ?>"  class="form-control" data-error="Silahkan diisi." required>
								<?php } ?>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<?php } ?>
				</div>				
				<div class="card-footer">
					<button type="submit" name="btn_simpan" id="btn_simpan" class="btn btn-primary">Hitung</button>
				</div>
				
			</form>
			
		</div>
		
		<?php if(isset($_SESSION['knn_start'])) { ?>
		<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">
				<a href="selesai.php"><button type="button" name="btn_simpan" id="btn_simpan" class="btn btn-warning">Selesai</button></a>
			</div>
				
			
		</div>
		<?php } ?>

	</div>
</div>