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

<?= $this->section('content') ?>
<?php
$zoneid = '';
$parentzoneid = '';
$zonecode = '';
$zoneclass = '';
$zonename = '';
if(isset($zones[0])) { $data = $zones[0]; }
if(isset($new_value)) { $data = $new_value; }
if(isset($data)) {
	if(isset($data['zoneid'])) { $zoneid= $data['zoneid']; }
	$zonecode = $data['zonecode'];
	$zonename = $data['zonename'];
	$zoneclass = $data['zoneclass'];
	$parentzoneid = $data['parentzoneid'];
}
?>
<div class="form-body">
	<h3 class="card-title">Zone 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='zoneid' value='<?=$zoneid;?>' />
		<div class="form-group row">
			<label class="col-2 col-form-label">Code</label>
			<div class="col-10"><input class="form-control" type="text" name='zonecode' value="<?=$zonecode;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Class</label>
			<div class="col-10">
				<select name='zoneclass' id='zoneclass' class='form-select'>
					<option value=''></option>
				<?php
					foreach ($zoneclasses as $qzoneclass => $qzoneclasstext) {
						if($qzoneclass==$zoneclass) { echo "<option value='$qzoneclass' selected>$qzoneclasstext</option>"; }
						else { echo "<option value='$qzoneclass'>$qzoneclasstext</option>"; }
					}
				?>
				</select>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Parent Zone</label>
			<div class="col-10">
				<select id='parentzoneid' name='parentzoneid' class='form-control select' <?php if($zoneclass=='PROP' || $zoneclass=='') {echo "disabled";} ?> >
				<option value=''></option>
				<?php
				foreach ($parentzones as $datas) {
					$qzoneid = $datas['zoneid'];
					$qzonename = $datas['zonename'];
					if($parentzoneid == $qzoneid) { echo "<option value='$qzoneid' selected>$qzonename</option>"; }
					else { echo "<option value='$qzoneid'>$qzonename</option>"; }
				}
				?>
				</select>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Zone Name</label>
			<div class="col-10"><input class="form-control" type="text" name='zonename' value="<?=$zonename;?>"></div>
		</div>
		<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() ?>

<?= $this->section('script') ?>
<script>
$('#zoneclass').change(function() {
	//console.log(this.value);
	if(this.value == 'PROP'){ $("#parentzoneid").prop('disabled', true); } 
	else { $("#parentzoneid").prop('disabled', false); }
})
</script>
<?= $this->endSection() ?>