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

<?= $this->section('content') ?>
<?php
$accountid = 0;
$initial = '';
$accountnpwp = '';
$accountname = '';
$parentaccount = '';
$street_1 = '';
$street_2 = '';
$street_3 = '';
$zoneid = '';
if(!isset($parentzoneid)) { $parentzoneid=''; }
$zip = '';
$country = '';
$email_1 = '';
$email_2 = '';
$phone = '';
$fax = '';
if(isset($accounts)) { $data = $accounts[0]; }
if(isset($new_value)) { $data = $new_value; }
if(isset($data)) {
	if(isset($data['accountid'])) { $accountid = $data['accountid']; }
	$initial = $data['initial'];
	$accountnpwp = $data['accountnpwp'];
	$accountname = $data['accountname'];
	$parentaccount = $data['parentaccount'];
	$street_1 = $data['street_1'];
	$street_2 = $data['street_2'];
	$street_3 = $data['street_3'];
	$zoneid = $data['zoneid'];
	//if(isset($data['parentzoneid'])) { $parentzoneid = $data['parentzoneid']; }
	$zip = $data['zip'];
	$country = $data['country'];
	$email_1 = $data['email_1'];
	$email_2 = $data['email_2'];
	$phone = $data['phone'];
	$fax = $data['fax'];
}
?>
<div class="form-body">
	<h3 class="card-title">Account 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='accountid' value='<?=$accountid;?>'/>
		<div class="form-group row">
			<label class="col-2 col-form-label">Account Name</label>
			<div class="col-10"><input class="form-control" type="text" name='accountname' value="<?=$accountname;?>"></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">Parent Account</label>
			<div class="col-10">
				<select class="form-control select2" name='parentaccount' >
					<option value='0'>-</option>
					<?php
					foreach ($parentaccounts as $parent) {
						$qparentid = $parent['accountid'];
						$qparentname = $parent['accountname'];
						if ($qparentid == $parentaccount) { echo "<option value='$qparentid' selected>$qparentname</option>"; }
						else { echo "<option value='$qparentid'>$qparentname</option>"; }
					}
					?>
				</select>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">NPWP</label>
			<div class="col-10"><input class="form-control" type="text" name='accountnpwp' value="<?=$accountnpwp;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Address</label>
			<div class="col-10"><input class="form-control" type="text" name='street_1' value="<?=$street_1;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label"></label>
			<div class="col-10"><input class="form-control" type="text" name='street_2' value="<?=$street_2;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label"></label>
			<div class="col-10"><input class="form-control" type="text" name='street_3' value="<?=$street_3;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Province</label>
			<div class="col-10">
				<select id='parentzoneid' name='parentzoneid' class='form-select select2 parentzones'>
					<option value=''>-</option>
					<?php
					foreach ($parentzones as $qdata) {
						$qzoneid = $qdata['zoneid'];
						$qzonename = $qdata['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">City</label>
			<div class="col-10">
				<select id='zoneid' name='zoneid' class='form-select select2 zones' >
					<option value=''>-</option>
					<?php
					if(isset($zones)) {
						foreach ($zones as $qdata) {
							$qzoneid = $qdata['zoneid'];
							$qzonename = $qdata['zonename'];
							if($zoneid == $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">Zip Code</label>
			<div class="col-10"><input class="form-control" type="text" name='zip' value="<?=$zip;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-2 col-form-label">Country</label>
			<div class="col-10"><input class="form-control" type="text" name='country' value="<?=$country;?>"></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">Fax</label>
			<div class="col-10"><input class="form-control" type="text" name='fax' value="<?=$fax;?>"></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>
$('#parentzoneid').change(function() {
	var parentzoneid=this.value;
	$.get("<?=base_url();?>/accounts/getcity/"+parentzoneid, function(data) {
		$('#zoneid').html(data);
	})
})
</script>
<?= $this->endSection() ?>
