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

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

$data=array();
$nama=""; $username=""; $password=""; $terdaftar=date("Y-m-d H:i:s");

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

if(@$_POST['nama']) {
	$id				= (int)$crud->escapeString(@$_REQUEST['id']);
	$data[0]		= $crud->escapeString(@$_POST['nama']);
	$data[1]		= $crud->escapeString(@$_POST['username']);
	$data[2]		= password_hash(trim($crud->escapeString(@$_POST['pass'])), PASSWORD_DEFAULT);
	$data[3]		= date("Y-m-d H:i:s");

	if($stat=="tambah") {
		$cek=$crud->cekAdministrator(array($data[1]), 1);
		if($cek==false) {
			$result = $crud->tambah($data);
			if($result==true) {
				$_SESSION['con']=1;
				?><script language="JavaScript">document.location='?p=administrator'</script><?php
			}
		} else {
			$_SESSION['error']='Username sudah digunakan.';
			?>
			<script language="JavaScript">window.history.back();</script>
			<?php
		}
	} elseif($stat=="ubah") {
		$cek=$crud->cekAdministrator(array($data[3],$id), 2);
		if($cek==false) {
			$pass_change=0;
			if(trim($crud->escapeString(@$_POST['pass']))!="") { $pass_change=1; }
			$result = $crud->ubah($data, $id, $pass_change);
			if($result==true) {
				$_SESSION['con']=2;
				?><script language="JavaScript">document.location='?p=administrator'</script><?php
			}
		} else {
			$_SESSION['error']='Username sudah digunakan.';
			?>
			<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=administrator'</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 Administrator</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=administrator-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">Nama Lengkap</label>
							<div class="col-sm-6">
								<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>
						<div class="form-group row">
							<label class="col-sm-3 col-form-label">Username</label>
							<div class="col-sm-6">
								<input type="text" name="username" class="form-control" data-error="Username harus diisi." value="<?php echo $username; ?>" required>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-3 col-form-label">Password</label>
							<div class="col-sm-6">
								<input id="pass" name="pass" class="form-control"  type="password" <?php if($stat!="ubah") { ?> data-minlength="5" data-error="Minimal 5 karakter" required <?php } else { ?> placeholder="Kosongkan jika tdk diubah" <?php } ?>>
								<div class="help-block with-errors"></div>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-3 col-form-label">Ulangi Password</label>
							<div class="col-sm-6">
								<input name="pass2" class="form-control" type="password" data-match="#pass" <?php if($stat!="ubah") { ?> data-minlength="5" data-error="Password tidak sama" required <?php } else { ?> placeholder="Kosongkan jika tdk diubah" <?php } ?>>
								<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=administrator'">Kembali</button>
				</div>
				
			</form>
			
		</div>
	</div>
</div>