<?= $this->extend('layouts/form.php') ?>

<?= $this->section('content') ?>
<?php
$userid = 0;
$usernumber = '';
$firstname = '';
$lastname = '';
$initial = '';
$birthdate= '';
$email_1 = '';
$email_2 = '';
$phone = '';
$userdeptid = '';
$userposid = '';
$reportto = '';
$offid = '';
$startdate = '';
$enddate = '';
$level = '';
if(isset($users)) { $data = $users[0]; }
if(isset($new_value)) { $data = $new_value; }
if(isset($data)) {
	if(isset($data['userid'])) { $userid= $data['userid']; }
	$usernumber = $data['usernumber'];
	$firstname = $data['firstname'];
	$lastname = $data['lastname'];
	$initial = $data['initial'];
	$birthdate = $data['birthdate'];
	if($birthdate != '') { $birthdate = substr( $birthdate,0,10 ); }
	$email_1 = $data['email_1'];
	$email_2 = $data['email_2'];
	$phone = $data['phone'];
	$userposid = $data['userposid'];
	$userdeptid = $data['userdeptid'];
	$reportto = $data['reportto'];
	$offid = $data['offid'];
	$level = $data['level'];
	$startdate = $data['startdate'];
	if($startdate != '') { $startdate = substr( $startdate,0,10 ); }
	$enddate = $data['enddate'];
	if($enddate != '') { $enddate = substr( $enddate,0,10 ); }
}
?>
<div class="form-body">
	<h3 class="card-title">User Editor</h3>
	<hr>
	<?php 
	if(isset($validation)) {
	?>
		<div class='alert alert-danger alert-dismissible'>
			<?= $validation->listErrors(); ?>
			<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"> <span aria-hidden="true"></span> </button>
		</div>
	<?php
	}
	?>
	<form method='post'>
		<input type='hidden' name='userid' value='<?=$userid;?>'/>
		<div class="form-group row">
			<label class="col-2 col-form-label">User#</label>
			<div class="col-10"><input class="form-control" type="text" name='usernumber' value="<?=$usernumber;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">First Name</label>
			<div class="col-10"><input class="form-control" type="text" name='firstname' value="<?=$firstname;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Last Name</label>
			<div class="col-10"><input class="form-control" type="text" name='lastname' value="<?=$lastname;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Initial</label>
			<div class="col-10"><input class="form-control" type="text" name='initial' value="<?=$initial;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">DoB</label>
			<div class="col-10"><input class="form-control" type="date" name='birthdate' value="<?=$birthdate;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Email_1</label>
			<div class="col-10"><input class="form-control" type="text" name='email_1' value="<?=$email_1;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Email_2</label>
			<div class="col-10"><input class="form-control" type="text" name='email_2' value="<?=$email_2;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Phone</label>
			<div class="col-10"><input class="form-control" type="text" name='phone' value="<?=$phone;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Access Level</label>
			<div class="col-10">
				<select class="form-control select2" name='level' >
					<?php
					foreach ($levels as $qlevel => $qrole) {
						if ($qlevel == $level) { echo "<option value='$qlevel' selected>$qrole</option>"; }
						else { echo "<option value='$qlevel'>$qrole</option>"; }
					}
					?>
				</select>
			</div>
		</div>

		<!-- Hanya Ditampilkan Ketika Create Data -->
		<?php if( $userid == 0) { ?>
		<div class="form-group row">
			<label class="col-2 col-form-label">Reportto</label>
			<div class="col-10">
				<select class="form-control select2" name='reportto' >
					<option value='0'>-</option>
					<?php
					foreach ($userreportto as $data) {
						$quserid= $data['userid'];
						$qfirstname = $data['firstname'];
						$qlastname = $data['lastname'];
						if ($quserid == $reportto) { echo "<option value='$quserid' selected>$qfirstname $qlastname</option>"; }
						else { echo "<option value='$quserid'>$qfirstname $qlastname</option>"; }
					}
					?>
				</select>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Office</label>
			<div class="col-10">
				<select class="form-control select2" name='offid' >
					<option value='0'>-</option>
					<?php
					foreach ($offices as $data) {
						$qoffid = $data['offid'];
						$qoffname = $data['offname'];
						if ($qoffid == $offid) { echo "<option value='$qoffid' selected>$qoffname</option>"; }
						else { echo "<option value='$qoffid'>$qoffname</option>"; }
					}
					?>
				</select>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">User Department</label>
			<div class="col-10">
				<select class="form-control select2" name='userdeptid' >
					<option value='0'>-</option>
					<?php
					foreach ($userdepartment as $data) {
						$quserdeptid= $data['userdeptid'];
						$qshorttext = $data['shorttext'];
						$qtexts = $data['texts'];
						if ($quserdeptid== $userdeptid) { echo "<option value='$quserdeptid' selected>$qshorttext - $qtexts</option>"; }
						else { echo "<option value='$quserdeptid'>$qshorttext - $qtexts</option>"; }
					}
					?>
				</select>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">User Position</label>
			<div class="col-10">
				<select class="form-control select2" name='userposid' >
					<option value='0'>-</option>
					<?php
					foreach ($userposition as $data) {
						$quserposid= $data['userposid'];
						$qshorttext = $data['shorttext'];
						$qtexts = $data['texts'];
						if ($quserposid== $userposid) { echo "<option value='$quserposid' selected>$qshorttext - $qtexts</option>"; }
						else { echo "<option value='$quserposid'>$qshorttext - $qtexts</option>"; }
					}
					?>
				</select>
			</div>
		</div>
		<?php } ?>
		
		<?php if( $userid == 0) { ?>
			<div class="form-group row">
				<label class="col-2 col-form-label">Start Work
					<br> 
					<small class="text-muted fs-6">Mulai Bekerja</small>
				</label>
				<div class="col-10 fs-6"><input class="form-control" type="date" name='startdate'></div>
			</div>

			<div class="form-group row">
				<label class="col-2 col-form-label">End Work
					<br> 
					<small class="text-muted fs-6">Terakhir Bekerja</small>
				</label>
				<div class="col-10"><input class="form-control" type="date" name='enddate'></div>
			</div>
		<?php } ?>

		<?php if( $userid != 0) { ?>
			<div class="form-group row">
				<label class="col-2 col-form-label">Start Work
					<br> 
					<small class="text-muted fs-6">Awal Bekerja</small>
				</label>
				<div class="col-10 fs-6"><input class="form-control" type="date" name='startdate' value="<?=$startdate;?>"></div>
			</div>

			<div class="form-group row">
				<label class="col-2 col-form-label">End Work
					<br> 
					<small class="text-muted fs-6">Terakhir Bekerja</small>
				</label>
				<div class="col-10"><input class="form-control" type="date" name='enddate'></div>
			</div>
		<?php } ?>

		<button type="button" class="btn btn-dark float-start" onclick="window.close()">Cancel</button>
		<button type="submit" class="btn btn-success text-white float-end"> <i class="fa fa-check"></i> Submit</button>
	</form>
</div>
<?= $this->endSection() ?>