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

<?= $this->section('content') ?>
<?php
$data = $sites[0];
$siteid = $data['siteid'];
$sitename = $data['sitename'];
?>
<div class="form-body">
	<h3 class="card-jobtitle">Contact Editor</h3>
	<hr>
	<h4>Contact for <b><?=$sitename;?></b></h4>
	<?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='siteid' value='<?=$siteid;?>'/>
		<input id='sitecontactid_delete' type='hidden' name='sitecontactid_delete' value='' />
		<table class='table'>
			<thead>
				<tr> <th>Name</th> <th>Alt-email</th> <th>Jobtitle</th> <th>Department</th> 
				<th><button class='btn btn-sm btn-info' type='button' onclick='newrow()'>+ Add</button></th> </tr>
			</thead>
			<tbody class='sitecontact_tbl'>
			<?php
			for($i=-1; $i<count($sitecontact); $i++) {
				$contactid = '';
				$contactemail = '';
				$jobtitle = '';
				$department = '';
				$sitecontactid = 0;
				if(isset($sitecontact[$i])) {
					$data = $sitecontact[$i];
					$sitecontactid = $data['sitecontactid'];
					$contactid = $data['contactid'];
					if(isset($data['contactemail'])) { $contactemail = $data['contactemail']; }
					if(isset($data['jobtitle'])) { $jobtitle = $data['jobtitle']; }
					if(isset($data['department'])) { $department = $data['department']; }
				}
			?>
			<tr> 
				<td style='min-width:300px;'> 
					<select class='form-control form-control-sm select2' name='contactid[]' style='width:100%;'>
						<option value=''>--</option>
						<?php
							foreach($contacts as $data) {
								$qcontactid = $data['contactid'];
								$qfirstname = $data['firstname'];
								$qlastname = $data['lastname'];
								$qemail = $data['email_1'];
								if($qcontactid==$contactid) { echo "<option value='$qcontactid' selected>$qfirstname $qlastname - $qemail</option>"; }
								else { echo "<option value='$qcontactid'>$qfirstname $qlastname - $qemail</option>"; }
							}
						?>
					</select> 
				</td> 
				<td><input class='form-control form-control-sm' type='text' id='email' name='contactemail[]' value='<?=$contactemail;?>' /></td> 
				<td><input class='form-control form-control-sm' type='text' name='jobtitle[]' value='<?=$jobtitle;?>' /></td> 
				<td><input class='form-control form-control-sm' type='text' name='department[]' value='<?=$department;?>' /></td>
				<td><button type='button' class='btn btn-sm btn-warning' onclick='f_sitecontactid_delete(this, <?=$sitecontactid;?>)'>Delete</button>
			</tr>
			<?php } ?>
			</tbody>
		</table>
		<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>
<script>
// acttext
// hapus sitecontactid
function f_sitecontactid_delete(e, sitecontactid) {
	if(confirm('Are you sure?')) {
		e.parentNode.parentNode.remove();
		var sitecontactid = sitecontactid.toString();
		var d = $('#sitecontactid_delete');
		d.val(d.val()+' '+sitecontactid);
		console.log(d.val());
	}
}

function newrow() {
	$.get("<?=base_url();?>/sitecontact/newrow", function(data) {
		$('.sitecontact_tbl').prepend(data);
		$('.select2').select2({
			theme: 'bootstrap-5',
			width: '100%'
		});
	})
}
</script>
<?= $this->endSection() ?>