<?php

require __dir__ . '/init.php';

require _SDIR . '/for-loan.php';

template::get_header();

?>

	<?php template::title( "Loan Management" ); ?>
        
	<div class='card'>
		<div class="card-body">
		
			<?php template::alert( $msg, $status ?? null ); ?>
	
			<div class="table-responsive">
				<table class="table table-striped" data-table="simple">
					<thead>
						<tr>
							<th>ACCOUNT</th>
							<th>EMAIL</th>
							<th>DATE</th>
							<th>NAME</th>
							<th>AMOUNT </th>
							<th>DURATION </th>
							<th>REASON </th>
							<th>LOAN-ID </th>
							<th>STATUS</th>
							<th>ACTION</th>
						</tr>
					</thead>
					<tbody>
						<?php 
							
							$sql= "SELECT * FROM loan ORDER BY id DESC";
							$result = mysqli_query($link,$sql);
							
							if(mysqli_num_rows($result) > 0){
								while($row = mysqli_fetch_assoc($result)) {  

									if(!empty($row['status']))
										$st = '<i class="fas fa-check text-success"></i> Approved';
									else $st = '<i class="fas fa-times text-danger"></i> Unpproved';
									
							?>
						<tr class="odd gradeX">
							<td><?php echo $row['account'];?></td>
							<td><?php echo $row['email'];?></td>
							<td><?php echo $row['date'];?></td>
							<td><?php echo $row['name'];?></td>
							<td><?php echo $row['amount'];?></td>
							<td><?php echo $row['duration'];?></td>
							<td><?php echo $row['reason'];?></td>
							<td><?php echo $row['loanid'];?></td>
							<td class='text-nowrap'><?php echo $st;?></td>
							<td>
								<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return confirm('Please click OK to proceed');">
									<input type='hidden' name='id' value="<?php echo $row['id']; ?>">
									<div class='btn-group'>
										<button type="submit" name="delete" class="btn btn-danger">
											<span class="fas fa-trash"></span>
										</button>
										<button  type="submit" name="approve" value="1" <?php if( !empty($row['status'])) echo 'disabled'; ?> class="btn btn-success">
											<span class="fas fa-<?php echo empty($row['status']) ? 'check' : 'ban'; ?>"></span>
										</button>
									</div>
								</form>
							</td>
						</tr>
						<?php    
								};
							};
							?>
					</tbody>
				</table>
			</div>
		</div>
	</div>


<?php template::get_footer(); ?>  