<?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");

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

if(@$_POST['nama']) {
	$id				= (int)$crud->escapeString(@$_REQUEST['id']);

	$data[0]		= $crud->escapeString(@$_POST['kode']);
	$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'];	

	if($stat=="tambah") {
		$cek=$crud->cektesting(array($data[0]), 1);
		if($cek==false) {
			$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['con']=1;
				?><script language="JavaScript">document.location='?p=testing'</script><?php
			}
		} else {
			$_SESSION['error']='Kode sudah terdaftar.';
			?>
			<script language="JavaScript">window.history.back();</script>
			<?php
		}
	} elseif($stat=="ubah") {
		$cek=$crud->cektesting(array($data[0],$id), 2);
		if($cek==false) {
			$result = $crud->ubah($data, $id);
			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($id, $d_kriteria['id_kriteria']);
						$data_testing[$d_kriteria['id_kriteria']]=@$nilai[0]['bobot'];
					} else {
						$nilai=$_penilaian->tampil_satu_b($id, $d_kriteria['id_kriteria']);
						$data_testing[$d_kriteria['id_kriteria']]=@$nilai[0]['masukan'];
					}
				}
				$hasil=$_knn->_knn($data_testing, $data[6]);
				//print_r($hasil);
				//Simpan Hasil Klasifikasi
				$crud->simpan_hasil($id,$hasil['hasil']);
					
				$_SESSION['con']=2;
				if($f=="h"){
					?><script language="JavaScript">//document.location='?p=hasil'</script><?php
				} else {
					?><script language="JavaScript">//document.location='?p=testing'</script><?php
				}
			}
		} else {
			$_SESSION['error']='Kode sudah terdaftar.';
			?>
			<script language="JavaScript">window.history.back();</script>
			<?php
		}
	}
} elseif($stat=="hapus" && $id!="") {
	$result = $crud->hapus($id);
	if($result) {
		$_SESSION['con']=3;
		?> <script language="JavaScript">document.location='?p=testing'</script> <?php
		exit;
	}
}
?>

<div class="row">

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

		<div class="card">

			<div class="card-header">
				<h4 class="m-0"><?php if($stat=="ubah") { echo "Ubah"; } else { echo "Tambah"; } ?> Data Testing</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>
				
				<div class="card-footer">
					<button type="submit" name="btn_simpan" id="btn_simpan" class="btn btn-primary">Simpan</button>
					<button type="button" id="btn_batal" class="btn btn-danger" onclick="document.location='./?p=testing'">Kembali</button>
				</div>
				
			</form>
			
		</div>
	</div>
</div>
