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

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

$data=array();
$kriteria=""; $jenis=""; $masukan=""; $keterangan="";

if($stat=="ubah") {
	$result = $crud->tampilById($id);
	foreach ($result as $key => $data) {	
		$kriteria=$data['kriteria'];
		$jenis=$data['jenis'];
		$masukan=$data['masukan'];
		$keterangan=$data['keterangan'];
	}
}

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

	if($stat=="tambah") {
		$cek=$crud->cekkriteria(array($data[0]), 1);
		if($cek==false) {
			$result = $crud->tambah($data);
			if($result==true) {
				$_SESSION['con']=1;
				?><script language="JavaScript">document.location='?p=kriteria'</script><?php
			}
		} else {
			$_SESSION['error']='Kriteria sudah terdaftar.';
			?>
			<script language="JavaScript">window.history.back();</script>
			<?php
		}
	} elseif($stat=="ubah") {
		$cek=$crud->cekkriteria(array($data[0],$id), 2);
		if($cek==false) {
			$result = $crud->ubah($data, $id);
			if($result==true) {
				$_SESSION['con']=2;
				?><script language="JavaScript">document.location='?p=kriteria'</script><?php
			}
		} else {
			$_SESSION['error']='Kriteria 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'</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</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-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">
								<input type="text" name="kriteria" class="form-control" data-error="Kriteria harus diisi." value="<?php echo $kriteria; ?>" required>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-3 col-form-label">Jenis</label>
							<div class="col-sm-5">
								<select name="jenis"  class="form-control" data-error="Jenis harus diisi.">
								<option value="1" <?php if($jenis==1) { echo "selected"; }?>><?php echo $lib->kriteria_jenis[1]; ?></option>
								<option value="2" <?php if($jenis==2) { echo "selected"; }?>><?php echo $lib->kriteria_jenis[2]; ?></option>
								</select>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-3 col-form-label">Masukan</label>
							<div class="col-sm-5">
								<select name="masukan"  class="form-control" data-error="Masukan harus diisi.">
								<option value="1" <?php if($masukan==1) { echo "selected"; }?>><?php echo $lib->kriteria_masukan[1]; ?></option>
								<option value="2" <?php if($masukan==2) { echo "selected"; }?>><?php echo $lib->kriteria_masukan[2]; ?></option>
								<option value="3" <?php if($masukan==3) { echo "selected"; }?>><?php echo $lib->kriteria_masukan[3]; ?></option>

								</select>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-3 col-form-label">Keterangan</label>
							<div class="col-sm-5">
								<input type="text" name="keterangan" class="form-control" data-error="Kriteria harus diisi." value="<?php echo $keterangan; ?>">
								<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'">Kembali</button>
				</div>
				
			</form>
			
		</div>
	</div>
</div>