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

<?= $this->section('content') ?>
<?php
$cardtext = '';
$owner_userid = '';
$priority = '';
if(isset($cards)) {
	$priority = $cards[0]['priority'];
	$listid = $cards[0]['listid'];
	$cardtext = $cards[0]['cardtext'];
	$owner_userid = $cards[0]['owner_userid'];
}
?>
<div class="form-body">
	<h3 class="card-title">Card Editor <button class="btn btn-danger fa fa-x float-end" onclick="window.close()"></button> </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' id='myForm'>
		<input type='hidden' name='cardid' value='<?=$cardid;?>'/>
		<div class='row mb-3'>
			<div class='col-2'>List</div>
			<div class='col-4'>
				<select class='form-control form-control-sm select2' type='text' name='listid' />
					<?php
					foreach($lists as $data) {
						$qlistid = $data['listid'];
						$qlistname = $data['listname'];
						if($listid == $qlistid) { echo "<option value='$qlistid' selected>$qlistname</option>"; }
						else { echo "<option value='$qlistid'>$qlistname</option>"; }
					}
					?>
				</select>
			</div>
			<div class='col-3 text-end'>Priority</div>
			<div class='col-3'>
				<select class='form-control form-control-sm select2' type='text' name='priority' />
					<?php
					foreach($card_priorities as $qindex => $qvalue) {
						if($qindex == $priority) { echo "<option value='$qindex' selected>$qvalue</option>"; }
						else { echo "<option value='$qindex'>$qvalue</option>"; }
					}
					?>
				</select>
			</div>
		</div>
		<div class='row mb-3'>
			<div class='col-2'>Card Owner</div>
			<div class='col-10'>
				<select class='form-control form-control-sm select2' type='text' name='owner_userid' />
					<?php
					foreach($users as $data) {
						$userid = $data['userid'];
						$username = $data['firstname'].' '.$data['lastname'];
						if($owner_userid != '') {
							if($userid == $owner_userid) { echo "<option value='$userid' selected>$username</option>"; }
							else { echo "<option value='$userid'>$username</option>"; }
						} else {
							if($userid == $_SESSION['userid']) { echo "<option value='$userid' selected>$username</option>"; }
							else { echo "<option value='$userid'>$username</option>"; }
						}
					}
					?>
				</select>
			</div>
		</div>		
		<div class='row mb-3'>
			<div class='col-2'>Card text</div>
			<div class='col-10'><textarea class='form-control' name='cardtext'><?=$cardtext;?></textarea></div>
		</div>
		<button type="submit" class="btn btn-success text-white float-start"> <i class="fa fa-check"></i> Submit</button>
	</form>
</div>
<?= $this->endSection() ?>