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

<?= $this->section('content') ?>
<?php
$productid = 0;
$productnumber = '';
$catalogid = '';
$siteid = '';
$locationstartdate = '';
$installationdate = '';
$warrantystartdate = '';
$warrantyenddate = '';
$productowner = '';
$productactive = '';
$statusservice = '';
$statuspart = '';
$remotetool = '';
$remoteid = '';
$remotepwd = '';
$productserviceid = '';
$accountid_productowner = '';
if(isset($products)) { $data = $products[0]; }
if(isset($new_value)) { $data = $new_value; }
if(isset($data)) {
	if(isset($data['productid'])) { $productid = $data['productid']; }
	$productnumber = $data['productnumber'];
	$catalogid = $data['catalogid'];
	$siteid = $data['siteid'];
	$locationstartdate = $data['locationstartdate'];
	$installationdate = $data['installationdate'];
	$warrantystartdate = $data['warrantystartdate'];
	$warrantyenddate = $data['warrantyenddate'];
	$productowner = $data['productowner'];
	$productactive = ($data['active'] == NULL) ? '1' : $data['active'];
	$remotetool = $data['remotetool'];
	$remoteid = $data['remoteid'];
	$remotepwd = $data['remotepwd'];
	$productserviceid = $data['productserviceid'];
	$accountid_productowner = $data['accountid_productowner'];
	// var_dump($productactive);die();
}
?>

<div class="form-body">
	<h3 class="card-title">Product 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='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='productnumber' value="<?=$productnumber;?>"></div>
		</div>

		<div class="form-group row">
			<label class="col-3 col-form-label">Catalog</label>
			<div class="col-9">
				<select class="form-control select2" name='catalogid' >
					<option value=''>-</option>
					<?php
					foreach ($productcatalog as $data) {
						$qcatalogid = $data['catalogid'];
						$qcatalognumber = $data['catalognumber'];
						$qproductname = $data['productname'];
						if ($qcatalogid == $catalogid) { echo "<option value='$qcatalogid' selected>$qproductname - $qcatalognumber</option>"; }
						else { echo "<option value='$qcatalogid'>$qproductname - $qcatalognumber</option>"; }
					}
					?>
				</select>
			</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 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>

		<div class="form-group row">
			<label class="col-3 col-form-label">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">Installation Date</label>
			<div class="col-9"><input class="form-control" type="date" name='installationdate' value="<?=$installationdate;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-3 col-form-label">Location Start Date</label>
			<div class="col-9"><input class="form-control" type="date" name='locationstartdate' value="<?=$locationstartdate;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-3 col-form-label">Warranty Start Date</label>
			<div class="col-9"><input class="form-control" type="date" name='warrantystartdate' value="<?=$warrantystartdate;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-3 col-form-label">Warranty End Date</label>
			<div class="col-9"><input class="form-control" type="date" name='warrantyenddate' value="<?=$warrantyenddate;?>"></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">Product Service</label>
			<div class="col-9">
				<select class="form-control select2" name='productserviceid' >
					<option value='0'>-</option>
					<?php
					foreach ($productservice as $data) {
						$qproductserviceid = $data['productserviceid'];
						$qproductservicetext = $data['productservicetext'];
						if ($qproductserviceid == $productserviceid ) { echo "<option value='$qproductserviceid' selected>$qproductservicetext</option>"; }
						else { echo "<option value='$qproductserviceid'>$qproductservicetext</option>"; }
					}
					?>
				</select>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-3 col-form-label">Active Status</label>
			<div class="col-9">
				<select class='form-control' name='productactive'> 
					<option value=''></option>
					<?php 
						foreach($productactives as $qactive => $qname) {
							if($qactive == $productactive) { echo "<option value='$qactive' selected>$qname</option>"; }
							else { echo "<option value='$qactive'>$qname</option>";  }
						}
					?>
				</select>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-3 col-form-label">Status Part</label>
			<div class="col-9"><input class="form-control" type="text" name='statuspart' value="<?=$statuspart;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-3 col-form-label">Remote Tool</label>
			<div class="col-9"><input class="form-control" type="text" name='remotetool' value="<?=$remotetool;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-3 col-form-label">Remote ID</label>
			<div class="col-9"><input class="form-control" type="text" name='remoteid' value="<?=$remoteid;?>"></div>
		</div>
		<div class="form-group row">
			<label class="col-3 col-form-label">Remote PWD</label>
			<div class="col-9"><input class="form-control" type="text" name='remotepwd' value="<?=$remotepwd;?>"></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() ?>