<?php 

require __DIR__ . '/init.php';

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
	
	$_POST = help::sanitize($_POST);
	
	switch( $_POST['request'] ) {
		
		case "delete":
				$SQL = "DELETE FROM banks WHERE id = '{$_POST['id']}'";
				$status = $link->query( $SQL );
				$msg = $status ? "Bank info deleted" : "Bank info failed to delete";
			break;
			
		default:
		
			$data = array( 
				"bank_name" => $_POST['bank_name'],
				"bank_region" => $_POST['bank_region']
			);
			
			if( empty($_POST['id']) ) {
				$SQL = sQuery::insert( "banks", $data );
				$type = "added";
			} else  {
				$SQL = sQuery::update( "banks", $data, "id='{$_POST['id']}'" );
				$type = "updated";
			};
			
			$status = $link->query( $SQL );
			
			$msg = $status ? "Bank info successfully {$type}" : "Bank info could not be {$type}";
			
	}
	
};

template::get_header();

template::title("Bank Management");

if( !empty($_GET['n']) ) {
	$query = $link->query( sQuery::select( "banks", "id='{$_GET['n']}'" ) );
	$bank = $query->fetch_assoc();
	if( empty($bank) ) {
		$msg = "The bank to edit was not found";
		$status = 'warning';
	} else $msg = "Change Bank Info";
} else $bank = null;

?>

	<div class='row'>
	
		<div class='col-lg-4 mb-3'>
			
			<div class='card'>
				<div class='card-body'>
				
					<?php template::alert( $msg, $status ?? null ); ?>
					
					<form method='post' id='add-bank' action="<?php echo $_SERVER['PHP_SELF']; ?>">
					
						<input type='hidden' name='id' value='<?php if( $bank ) echo $bank['id']; ?>'>
						<input type='hidden' name='request' value='manage'>
						
						<div class='form-group'>
							<label class='form-label'>Bank Name</label>
							<input type='text' name='bank_name' class='form-control' placeholder='Enter Bank Name' value='<?php if( $bank ) echo $bank['bank_name']; ?>'>
						</div>
						
						<div class='form-group'>
							<label class='form-label'>Bank Region</label>
							<select name='bank_region' class='form-control text-capitalize'>
								<?php 
									foreach( ['local', 'international'] as $region ):
										$select = ($bank && $bank['bank_region'] == $region) ? "selected" : null;
								?>
								<option value='<?php echo $region; ?>' <?php echo $select; ?>>
									<?php echo $region; ?>
								</option>
								<?php endforeach; ?>
							</select>
						</div>
						
						<div class='text-right'>
							<?php if( $bank ): ?>
								<a href='<?php echo $_SERVER['PHP_SELF']; ?>' class='btn btn-warning'>
									<i class='fas fa-ban'></i> Cancel
								</a>
							<?php endif; ?>
							<button class='btn btn-primary'>
								<?php echo $bank ? '<i class="fas fa-check-circle"></i> Update' : '<i class="fas fa-plus-circle"></i> Add'; ?> Bank
							</button>
						</div>
						
					</form>
			
				</div>
			</div>
			
		</div>
		
		<div class='col-lg-8'>
			<div class='card'>
				<div class='card-body'>
					<div class='table-responsive'>
					
						<table class='table table-striped' data-table='simple'>
							<thead>
								<tr>
									<th>Bank Name</th>
									<th>Bank Region</th>
									<th class='text-right'>Action</th>
								</tr>
							</thead>
							<tbody>
								<?php 
									$banks = $link->query( sQuery::select( "banks", "1 ORDER BY id DESC" ));
									while( $bank = $banks->fetch_assoc() ):
								?>
								<tr>
									<td><?php echo $bank['bank_name']; ?></td>
									<td><?php echo ucwords($bank['bank_region']); ?></td>
									<td class='text-right'>
										<form method='post' onsubmit="return confirm('Please click OK to proceed');">
											<input type='hidden' name='id' value='<?php echo $bank['id']; ?>'>
											<div class='btn-group'>
												<a class='btn btn-primary' type='button' href='?n=<?php echo $bank['id']; ?>'>
													<i class='fas fa-pencil-alt'></i>
												</a>
												<button class='btn btn-danger' name='request' value='delete'>
													<i class='fas fa-trash'></i>
												</button>
											</div>
										</form>
									</td>
								</tr>
								<?php endwhile; ?>
							</tbody>
						</table>
						
					</div>
				</div>
			</div>
		</div>
	
	</div>

<?php template::get_footer();
