<?php

require __dir__ . '/init.php';

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

template::get_header();

?>

	<?php template::title( "MOBILE DEPOSIT MANAGEMENT" ); ?>
	
    <?php template::alert( $msg , $status ?? null ); ?>
          
	<div class='card'>
		<div class="card-body">
			
			<div class="table-responsive">
				<table class="table"  id="example">
					<thead>
						<tr>
							<th>ACCOUNT</th>
							<th style="">AMOUNT</th>
							<th>DATE</th>
							<th>CHEQUE</th>
							<th>CHEQUE-ID </th>
							<th>STATUS</th>
							<th>ACTION</th>
						</tr>
					</thead>
					<tbody>
						<?php 
						
							$sql= "SELECT * FROM md";
							$result = mysqli_query($link,$sql);
							
							if(mysqli_num_rows($result) > 0){
								while($row = mysqli_fetch_assoc($result)) {  
									
									$image = help::url( UPLOADS_DIR . '/cheques/' . $row['cheque'] );
						?>
						<tr class="odd gradeX">
							<td><?php echo $row['account'];?></td>
							<td style="">
								<input type="number" step="0.01" data-key="<?php echo $row['id']; ?>" value="<?php echo $row['amount']; ?>" class='form-control' <?php if( !empty($row['status']) ) echo 'disabled'; ?>>
							</td>
							<td><?php echo $row['date'];?></td>
							<td>
								<div class='p-1'>
									<?php if( !empty($row['cheque']) ): ?>
									<a href="<?php echo $image; ?>" target="_blank">
										<img src="<?php echo $image; ?>" width="120px">
									</a>
									<?php else: ?>
									<i class='fas fa-question-circle'></i>
									<?php endif; ?>
								</div>
							</td>
							<td><?php echo $row['cid'];?></td>
							<td>
								<span class='text-nowrap'>
									<?php echo help::stdCheck($row['status']); ?>
								</span>
							</td>
							<td>
								<form method="post" action="md.php" onsubmit="return confirm('Click OK to proceed');">
									<input type="hidden" name="cid" value="<?php echo $row['cid']; ?>">
									<input type='hidden' name='amount' data-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" class="btn btn-success" <?php if( !empty($row['status']) ) echo 'disabled'; ?>>
											<span class="fas fa-<?php echo !empty($row['status']) ? 'ban' : 'check'; ?>"></span>
										</button>
									</div>
								</form>
							</td>
						</tr>
						<?php    
								}
							}
							?>
					</tbody>
				</table>
			</div>
		
		</div>
	</div>
	
	<script>
		document.querySelectorAll("input[data-key]").forEach(function(el) {
			el.addEventListener('input', function() {
				document.querySelector("input[data-value='" + el.dataset.key + "']").value = el.value;
			});
		});
	</script>

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