<?= $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">Users Management Page</h4>
			</div>
			<div class="col-md-7 align-self-center text-end">
				<a class='btn btn-info text-white btn-sm' href='users/create/' 
					onclick="window.open(this.href, 'Create User','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>User#</th> <th>Initial</th> <th>Name</th> <th>User Type</th> <th style="width:39%"></th>
								</thead>
								<tbody>
									<?php
									foreach($users as $data) {
										$classend = '';
										$userid = $data['userid'];
										$usernumber = $data['usernumber'];
										$name = $data['firstname'].' '.$data['lastname'];
										$initial = $data['initial'];
										$userposition = $data['userposition'];
										$userdepartment = $data['userdepartment'];
										if( $data['createdate'] != '' ) {
											$createdate = date('d-m-Y', strtotime($data['createdate']));
										} else { $createdate = ''; }

										
										if( $data['enddate'] != '' ) { 
											$classend = 'class="bg-danger"'; 
											$toggletext = "<a class='btn btn-success btn-sm' href='users/toggle/$userid' 
												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='users/toggle/$userid' 
												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><?=$usernumber;?></td> <td><?=$initial;?></td> <td><?=$name;?></td> <td><?=$userposition;?></td>
										<td>
											<button type='button' class='btn btn-success btn-sm openViewUser' data-userid='<?=$userid;?>'><i class="fas fa-eye"></i> View</button>
											<a class='btn text-white btn-sm' href='users/edit/<?=$userid;?>' style="background-color:#f29727f5;" 
												onclick="window.open(this.href, 'User Editor','width=900,height=800,toolbar=1,resizable=0'); return false;">
												<i class="fa-solid fa-user-pen"></i> Edit
											</a>
											<a class='btn btn-info btn-sm' href='users/role/edit/<?=$userid;?>' 
												onclick="window.open(this.href, 'User Editor','width=900,height=800,toolbar=1,resizable=0'); return false;">
												<i class="fa-solid fa-pen-to-square"></i> Role
											</a>
											<a class='btn btn-secondary btn-sm' href='users/edit_password/<?=$userid;?>' 
												onclick="window.open(this.href, 'User Editor','width=900,height=500,toolbar=1,resizable=0'); return false;">
												<i class="fas fa-key"></i> Edit Password
											</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>
	</div>
</div>
<?= $this->endSection() ?>


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

$('.openViewUser').on('click',function(){
	const userid = $(this).data('userid');
	$('.modal-content').load('<?=base_url();?>/users/view/'+userid,function(){
		$('#modal').modal('show');
	});
});

</script>
<?= $this->endSection() ?>