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

<?= $this->section('content') ?>
<?php

if(isset($accounts)){
	// var_dump($accounts);die();	
}


if(!isset($accountname)) {$accountname = '';}
if(!isset($areaid)) {$areaid = '';}
?>
<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">Accounts Management Page</h4>
			</div>					
			<div class="col-md-7 align-self-center text-end">
				<a class='btn btn-info text-white btn-sm' href='accounts/create/' 
					onclick="window.open(this.href, 'Create Account','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">

						<form method='post' >
							<div class='form-group row'>
								<label class='form-label col-2'>Accountname</label>
								<div class='col-10'>
									<input class='form-control form-control-sm' type='text' placeholder='* accountname *' name='accountname' value='<?=$accountname;?>' />
								</div>
							</div>

							<div class='form-group row'>
								<label class='form-label col-2'>Area</label>
								<div class='col-10'>
									<select name='areaid' class='form-select form-select-sm'>
										<option value=''>-</option>
										<?php
											foreach($areas as $data) {
												$qareaid = $data['areaid'];
												$qareaname = $data['areaname'];
												if($areaid == $qareaid) { echo "<option value='$qareaid' selected>$qareaname</option>"; }
												else { echo "<option value='$qareaid'>$qareaname</option>"; }
											}
										?>
									</select>
								</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($accounts)) { ?>
		<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>Name</th> <th>Parent</th> <th>Area</th> <th>Create date</th> <th></th>
								</thead>
								<tbody>
									<?php
									foreach($accounts as $data) {
										$classend = '';
										$accountid = $data['accountid'];
										$zoneid = $data['zoneid'];
										$initial = $data['initial'];
										$accountname = $data['accountname'];
										$parentname = $data['parentname'];
										$areaid = (int)$data['areaid'];
										$areaname = $data['areaname'];
										$createdate = date('d-m-Y', strtotime($data['createdate']));
										$toggletext = "<a class='btn btn-danger btn-sm crm-confirmation' href='accounts/toggle/$accountid'> <i class='fas fa-trash-alt'></i> Disable </a>";
										if ( $initial != '' ) { $accountname = "$accountname ($initial)"; }
										if( $data['enddate'] != '' ) { 
											$classend = 'class="bg-danger"'; 
											$toggletext = "<a class='btn btn-success btn-sm' href='accounts/toggle/$accountid' 
												onclick=\"window.open(this.href, 'form','width=400,height=300,toolbar=1,resizable=0'); return false;\">
												<i class='fa-solid fa-check'></i> Enable
											</a>";
										} else {
											$toggletext = "<a class='btn btn-danger btn-sm' href='accounts/toggle/$accountid' 
												onclick=\"window.open(this.href, 'form','width=400,height=300,toolbar=1,resizable=0'); return false;\">
												<i class='fas fa-trash-alt'></i> Disable
											</a>";
										}
									?>
									<tr <?=$classend;?>>
										<td><?=$accountname;?></td> <td><?=$parentname;?></td> <td><?=$areaname;?></td> <td><?=$createdate;?></td> 
										<td>
											<button type='button' class='btn btn-success btn-sm openViewAccount' data-accountid='<?=$accountid;?>'><i class="fas fa-eye"></i> View</button>
											<a class='btn btn-warning btn-sm' href='accounts/edit/<?=$accountid;?>' 
												onclick="window.open(this.href, 'Account Editor','width=900,height=800,toolbar=1,resizable=0'); return false;">
												<i class="fas fa-pencil-alt"></i> Edit
											</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" : []
	});
});

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