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

<?= $this->section('content') ?>
<?php
$productid = 0;
$productnumber = '';
$siteid = '';
$newlocationstartdate = '';
$oldlocationenddate = '';
// $installationdate = '';
$accountid_productowner = '';
$productowner = '';
if(isset($products)) { $data = $products[0]; }
if(isset($new_value)) { $data = $new_value; }
if(isset($data)) {
	if(isset($data['productid'])) { $productid = $data['productid']; }
	$siteid = $data['siteid'];
	if(isset($data['newlocationstartdate'])) { $newlocationstartdate = $data['newlocationstartdate']; }
	if(isset($data['oldlocationenddate'])) { $oldlocationenddate = $data['oldlocationenddate']; }
	if(isset($data['accountid_productowner'])) { $accountid_productowner = $data['accountid_productowner']; }

	// if (isset($data['installationdate'])) {
	// 	$installationdate = $data['installationdate'];
	// }
	// $installationdate = $data['installationdate'];
	
	$productowner = $data['productowner'];

	// $productnumber = $data['productnumber'];
	if (isset($data['productnumber'])) {
		$productnumber = $data['productnumber'];
	} else {
		// Handle case when 'installationdate' key is not present
		$productnumber = $new_value['productnumb'];
	}

	// $productname = $data['productname'];
	if (isset($data['productname'])) {
		$productname = $data['productname'];
	} else {
		$productname = '';
		// Handle case when 'installationdate' key is not present
		$productname = $new_value['productnam'];
	}
}
?>

<div class="form-body">
	<h3 class="card-title">Product Moving</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='productid' value='<?=$productid;?>'/>
		<div class="form-group row">
			<label class="col-3 col-form-label">Product# (SN.)</label>
			<div class="col-9"><input class="form-control" type="text" name='productnumb' value="<?=$productnumber;?>" readonly></div>
		</div>

		<div class="form-group row">
			<label class="col-3 col-form-label">Catalog</label>
			<div class="col-9"><input class="form-control" type="text" name='productnam' value="<?=$productname;?>" readonly></div>
		</div>

		<div class="form-group row">
			<label class="col-3 col-form-label">New Site</label>
			<div class="col-9">
				<select class="form-control select2" name='siteid' >
					<option value='0'>-</option>
					<?php
					foreach ($sites as $data) {
						$qsiteid= $data['siteid'];
						$qsitename = $data['sitename'];
						if ($qsiteid == $siteid) { echo "<option value='$qsiteid' selected>$qsitename</option>"; }
						else { echo "<option value='$qsiteid'>$qsitename</option>"; }
					}
					?>
				</select>
			</div>
		</div>
		
		<div class="form-group row">
			<label class="col-3 col-form-label">Old Location End Date</label>
			<div class="col-9"><input class="form-control" type="date" name='oldlocationenddate' value="<?=$oldlocationenddate;?>"></div>
		</div>
		
		<div class="form-group row">
			<label class="col-3 col-form-label">New Location Start Date</label>
			<div class="col-9"><input class="form-control" type="date" name='newlocationstartdate' value="<?=$newlocationstartdate;?>"></div>
		</div>
		
		<div class="form-group row">
			<label class="col-3 col-form-label">Product Owner</label>
			<div class="col-9">
				<select class='form-control' name='productowner'> 
					<option value=''>--</option>
					<?php 
						foreach($productowners as $qowner => $qname) {
							if($qowner == $productowner) { echo "<option value='$qowner' selected>$qname</option>"; }
							else { echo "<option value='$qowner'>$qname</option>";  }
						}
					?>
				</select>
			</div>
		</div>
		
		<div class="form-group row">
			<label class="col-3 col-form-label">Account Product Owner</label>
			<div class="col-9">
				<select class="form-control select2" name='accountid_productowner' >
					<option value=''>-</option>
					<?php
					foreach ($accounts as $data) {
						$qaccountid = $data['accountid'];
						$qaccountname = $data['accountname'];
						if ($qaccountid == $accountid_productowner) { echo "<option value='$qaccountid' selected>$qaccountname</option>"; }
						else { echo "<option value='$qaccountid'>$qaccountname</option>"; }
					}
					?>
				</select>
			</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() ?>