<?php
include_once "./class/kriteria_nilai.php";
include_once "./class/kriteria.php";
$crud=new kriteria_nilai();
$_kriteria=new kriteria();

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

$data=array();
$id_kriteria=0; $nilai=""; $bobot="";

if($stat=="ubah") {
	$result = $crud->tampilById($id);
	foreach ($result as $key => $data) {
		$id_kriteria=$data['id_kriteria'];
		$nilai=$data['nilai'];
		$bobot=$data['bobot'];
	}
}

if(@$_POST['nilai']) {
	$id				= (int)$crud->escapeString(@$_REQUEST['id']);
	$data[0]		= (int)$crud->escapeString(@$_POST['id_kriteria']);
	$data[1]		= $crud->escapeString(@$_POST['nilai']);
	$data[2]		= (int)$crud->escapeString(@$_POST['bobot']);
	
	if($stat=="tambah") {
		$cek=$crud->cekkriteria_nilai(array($data[0], $data[1]), 1);
		if($cek==false) {
			$result = $crud->tambah($data);
			if($result==true) {
				$_SESSION['con']=1;
				?><script language="JavaScript">document.location='?p=kriteria-nilai'</script><?php
			}
		} else {
			$_SESSION['error']='Nilai sudah terdaftar.';
			?>
			<script language="JavaScript">window.history.back();</script>
			<?php
		}
	} elseif($stat=="ubah") {
		$cek=$crud->cekkriteria_nilai(array($data[0], $data[1],$id), 2);
		if($cek==false) {
			$result = $crud->ubah($data, $id);
			if($result==true) {
				$_SESSION['con']=2;
				?><script language="JavaScript">document.location='?p=kriteria-nilai'</script><?php
			}
		} else {
			$_SESSION['error']='Nilai 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=kriteria-nilai'</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 Kriteria Nilai</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=kriteria-nilai-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; ?>'>
				<div class="card-body col-md-12 col-lg-6" style="font-size: .95em;">
						
						<div class="form-group row">
							<label class="col-sm-3 col-form-label">Kriteria</label>
							<div class="col-sm-5">
								<select name="id_kriteria" data-error="Silahkan pilih" class="form-control" required>
								<option value="">- Pilih -</option>
								<?php
								$result = $_kriteria->tampil_inp();
								foreach ($result as $key => $d_kriteria) {
								?>
								<option value="<?php echo $d_kriteria['id_kriteria']; ?>" <?php if($d_kriteria['id_kriteria']==$id_kriteria) { echo "selected"; } ?>><?php echo $d_kriteria['kriteria']; ?></option>
								<?php } ?>
								</select>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-3 col-form-label">Nilai</label>
							<div class="col-sm-5">
								<input type="text" name="nilai" class="form-control" data-error="Nilai harus diisi." value="<?php echo $nilai; ?>" required>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-3 col-form-label">Bobot</label>
							<div class="col-sm-5">
								<input type="number" step="1" name="bobot" class="form-control" data-error="Bobot harus diisi." value="<?php echo $bobot; ?>" required>
								<div class="help-block with-errors"></div>
							</div>
						</div>

				</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=kriteria-nilai'">Kembali</button>
				</div>
				
			</form>
			
		</div>
	</div>
</div>