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

<?= $this->section('content') ?>
<div class="page-wrapper">
	<div class="container-fluid">
		<div class="row page-titles">
			<div class="col-md-5 align-self-center">
				<h4 class="text-themecolor">Contacts Management Page</h4>
			</div>					
			<div class="col-md-7 align-self-center text-end">
				<a class='btn btn-info text-white btn-sm' href='contacts/create/' 
					onclick="window.open(this.href, 'Create Contacts','width=900,height=800,toolbar=1,resizable=0'); return false;">
					<i class="fas fa-plus-circle"></i> Create
				</a>
			</div>
		</div>
		
		<div class="row">
			<div class="col-12">
				<div class="card">
					<div class="card-body">
						<div class="table-responsive">
							<table id="myTable" class="table display table-striped border">
								<thead>
									<th>Email</th> <th>Name</th> <th>Initial</th> <th>Create date</th> <th></th>
								</thead>
								<tbody>
									<?php
									foreach($contacts as $data) {
										$contactid = $data['contactid'];
										$firstname = $data['firstname'];
										$lastname = $data['lastname'];
										$email_1 = $data['email_1'];
										$name = "$firstname $lastname";
										$initial = $data['initial'];
										$createdate = date('d-m-Y', strtotime($data['createdate']));
									?>
									<tr> 
										<td><?=$email_1;?></td> <td><?=$name;?></td> <td><?=$initial;?></td> <td><?=$createdate;?></td> 
										<td>
											<button type='button' class='btn btn-success btn-sm openViewContact' data-contactid='<?=$contactid;?>'><i class="fas fa-eye"></i> View</button>
											<a class='btn btn-warning btn-sm' href='contacts/edit/<?=$contactid;?>' 
												onclick="window.open(this.href, 'Contact Editor','width=900,height=800,toolbar=1,resizable=0'); return false;">
												<i class="fas fa-pencil-alt"></i> Edit
											</a>
										</td>
									</tr>
									<?php } ?>
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
		
		<div id="modal" class="modal" tabindex="-1" role="dialog" aria-labelledby="tooltipmodel" aria-hidden="true">
			<div class="modal-dialog modal-dialog-centered modal-xl">
				<div class="modal-content">
				</div>
			</div>
		</div>
	</div>
</div>
<?= $this->endSection() ?>

<?= $this->section('script') ?>
<script>
$(function () {
	$('#myTable').DataTable({
		"order" : []
	});
});

$('.openViewContact').on('click',function(){
	const contactid = $(this).data('contactid');
	$('.modal-content').load('contacts/view/'+contactid,function(){
		$('#modal').modal('show');
	});
});
</script>
<?= $this->endSection() ?>