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

$crud=new training();
$_kriteria=new kriteria();
$_kriteria_nilai=new kriteria_nilai();
$_penilaian=new penilaian();

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

$data=array();
$kode_training="";

if($stat=="ubah") {
	$result = $crud->tampilByKode($kode);
	foreach ($result as $key => $data) {	
		$kode_training=$data['kode_training'];
	}
} else {
	$kode_training=$crud->get_kode();
}

if(@$_POST['kode_training']) {
	$id				= (int)$crud->escapeString(@$_REQUEST['id']);
	$data[0]		= $crud->escapeString(@$_POST['kode_training']);
	$data[1]		= @$_POST['kriteria'];
	$data[2]		= @$_POST['nilai'];	
	$data[3]		= @$_POST['masukan'];	

	if($stat=="tambah") {
		$result = $crud->tambah($data);
		if($result==true) {
			$_SESSION['con']=1;
			?><script language="JavaScript">document.location='?p=training'</script><?php
		}
	} elseif($stat=="ubah") {
		$result = $crud->ubah($data, $kode);
		if($result!=false) {
			$_SESSION['con']=2;
			?><script language="JavaScript">document.location='?p=training'</script><?php
		}
	}
} elseif($stat=="hapus" && $kode!="") {
	$result = $crud->hapus($kode);
	if($result) {
		$_SESSION['con']=3;
		?> <script language="JavaScript">document.location='?p=training'</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 Training</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=training-input&stat=<?php echo $stat; ?>" method="post" enctype="multipart/form-data" id="frInput" name="frInput" data-toggle="validator">
				<input type='hidden' name='kode' id='kode' value='<?php echo $kode; ?>'>
				<div class="card-body col-md-12 col-lg-6" style="font-size: .95em;">
						<div class="form-group row">
							<label class="col-sm-4 col-form-label">Kode Training</label>
							<div class="col-sm-5">
								<input type="text" name="kode_training" class="form-control" data-error="Kode harus diisi." readonly value="<?php echo $kode_training; ?>" required>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<?php 
						$result = $_kriteria->tampil_kriteria();
						foreach ($result as $key => $d_kriteria) {
							$nilainya=0;
							if($d_kriteria['masukan']==1){
								$nilai=$crud->tampil_satu($kode, $d_kriteria['id_kriteria']);
								$nilainya=(int)@$nilai[0]['id_kriteria_nilai'];
							} else {
								$nilai=$crud->tampil_satu_b($kode, $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 } ?>
						<?php 
						$result = $_kriteria->tampil_keputusan();
						foreach ($result as $key => $d_kriteria) {
							$nilai=$crud->tampil_satu($kode, $d_kriteria['id_kriteria']);
							$nilainya=(int)@$nilai[0]['id_kriteria_nilai'];
						?>
						<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=training'">Kembali</button>
				</div>
				
			</form>
			
		</div>
	</div>
</div>