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

<?= $this->section('content') ?>
<?php
if(!isset($sitename)) {$sitename = '';}
?>
<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">Sites Management Page</h4>
			</div>
			<div class="col-md-7 align-self-center text-end">
				<a class='btn btn-info text-white btn-sm' href='sites/create/' 
					onclick="window.open(this.href, 'Create Site','width=900,height=600,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">
						
						<form method='post' >
							<div class='form-group row'>
								<label class='form-label col-2'>Sitename</label>
								<div class='col-10'>
									<input class='form-control form-control-sm' type='text' placeholder='* sitename *' name='sitename' value='<?=$sitename;?>' />
								</div>
							</div>
							<button type="submit" class="btn btn-success text-white"> <i class="fa fa-search"></i> Search</button>
						</form>

					</div>
				</div>
			</div>
		</div>
		
		<?php if(isset($sites)) { ?>
		<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>ID</th> 
									<th>Site</th> 
									<th>Account</th>
									<th>Create Date</th> 
									<th style="width:30%;"></th>
								</thead>

								<tbody>
									<?php
									foreach($sites as $data) {
								
										$siteid = $data['siteid'];
										$sitename = $data['sitename'];
										$accountname = $data['accountname'];
										$initial = $data['initial'];
										if ($initial != null) {
											$initial = "(" . $initial . ")";
										} else {
											$initial = "";
										}
										$toggletext = "<a class='btn btn-danger btn-sm crm-confirmation' href='sites/toggle/$siteid'> <i class='fas fa-trash-alt'></i> Disable </a>";
										
										if( $data['createdate'] != '' ) {
											$createdate = date('d-m-Y', strtotime($data['createdate']));
										} else { $createdate = ''; }
										if ($data['enddate'] != '' ) { 
											$sitename = "<del>$sitename</del>"; 
											 $toggletext = "<a class='btn btn-success btn-sm crm-confirmation' href='sites/toggle/$siteid'> <i class='fas fa-check'></i> Enable </a>";
										}
									?>
									<tr> 
										<td><?=$siteid;?></td> <td><?=$sitename;?></td> <td><?=$accountname;?> <?=$initial;?></td> <td><?=$createdate;?></td> 
										<td>
											<button type='button' class='btn btn-success btn-sm openViewSite' data-siteid='<?=$siteid;?>'><i class="fas fa-eye"></i> View</button>
											<a class='btn btn-warning btn-sm' href='sites/edit/<?=$siteid;?>' 
												onclick="window.open(this.href, 'Site Editor','width=900,height=600,toolbar=1,resizable=0'); return false;">
												<i class="fas fa-pencil-alt"></i> Edit
											</a>
											<a class='btn btn-dark btn-sm' href='sitecontact/edit/<?=$siteid;?>' 
												onclick="window.open(this.href, 'Site Contact Editor','width=1400,height=600,toolbar=1,resizable=0'); return false;">
												<i class="far fa-address-book"></i> Contact
											</a>
											<?=$toggletext;?>
										</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>
		<?php } ?>
	</div>
</div>
<?= $this->endSection() ?>

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

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