<?php

require __dir__ . '/init.php';

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
	
	if( isset($_POST['action']) && $_POST['action'] == 'delete' ):
	
		$TCodes = $settings->get('transfer_codes');
		$key = $_POST['key'];
		
		if( array_key_exists($key, $TCodes) ) {
			unset($TCodes[$key]);
			$status = $settings->set('transfer_codes', $TCodes);
			$__msg = $status ? "Transfer Code successfully deleted" : "Transfer code not deleted";
		} else $__msg = "No data was deleted";
	
	else:
	
		$TCodes = $settings->get('transfer_codes') ?? [];
		
		$key = strtolower($_POST['code_name']);
		unset($_POST['code_name']);
		
		if( $_POST['code_type'] == 'auto' ) $_POST['code_value'] = null;
		else if( empty($_POST['code_value']) ) $_POST['code_value'] = help::generate_number(4);
		
		$TCodes[$key] = $_POST;
		
		$status = $settings->set('transfer_codes', $TCodes);
		$msg = $status ? "Transfer Code saved" : "Transfer Code not saved";
	
	endif;
	
};

template::get_header();

$TCodes = $settings->get('transfer_codes');

if( !empty($TCodes) && array_key_exists(($_GET['c'] ?? help::keygen()), $TCodes) ) {
	$codex = $TCodes[ $_GET['c'] ];
	$codex['code_name'] = strtoupper($_GET['c']);
} else $codex = [];

?>

	<?php template::title( 'Transfer Codes' ); ?>

	<div class='row'>
		<div class='col-xl-5'>
			<div class='card'>
				<div class='card-body'>
				
					<?php template::alert( $msg, $status ?? null ); ?>
					
					<form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
					
						<div class='mb-3'>
							<label class='form-label'>Transfer Code</label>
							<input type='text' class='form-control' name='code_name' placeholder='e.g IMF' value='<?php echo $codex['code_name'] ?? null; ?>' required>
						</div>
					
						<div class='mb-3'>
							<label class='form-label'>Code Type</label>
							<select class='form-control mb-2 text-capitalize' name='code_type' required onchange='($(this).next().toggleClass("d-none"))'>
								<?php 
									foreach(['auto', 'static'] as $value):
										$selected = (($codex['code_type'] ?? null) == $value ) ? 'selected' : null;
								?>
									<option value='<?php echo $value; ?>' <?php echo $selected; ?>><?php echo $value; ?></option>
								<?php endforeach; ?>
							</select>
							<div class='<?php if( ($codex['code_type'] ?? null) != 'static' ) echo 'd-none'; ?>'>
								<input type='number' class='form-control' name='code_value' value='<?php echo $codex['code_value'] ?? null; ?>' placeholder='Enter Code'>
							</div>
						</div>
						
						<div class='mb-2'>
							<button class='btn btn-primary btn-block'>
								<?php echo empty($codex) ? 'Add' : 'Update'; ?>
							</button>
						</div>
						
					</form>
				</div>
			</div>
		</div>
		<div class='col-xl-7'>
			<div class='card'>
				<div class='card-body'>
					
					<?php template::alert( $__msg ?? null, $status ?? null ); ?>
					
					<div class='table-responsive'>
						<table class='table table-hover' data-table='simple'>
							<thead>
								<tr>
									<th>Codes Name</th>
									<th>Types</th>
									<th>Codes</th>
									<th></th>
								</tr>
							</thead>
							<tbody>
								<?php 
									if( !empty($TCodes) ):
										foreach( $TCodes as $key => $data ):
											$value = ( $data['code_value'] ) ? $data['code_value'] : "<i class='fas fa-question-circle'></i>";
								?>
									<tr>
										<th><?php echo strtoupper($key); ?></th>
										<td><?php echo strtoupper($data['code_type']); ?></td>
										<td><?php echo $value; ?></td>
										<td class='text-right'>
											<form method='post' onsubmit="return confirm('Click OK to confirm');">
												<input type='hidden' name='key' value='<?php echo $key; ?>'>
												<div class='btn-group'>
													<a class='btn btn-info' href='?c=<?php echo $key; ?>'>
														<i class='fas fa-pencil-alt'></i>
													</a>
													<button class='btn btn-danger' name='action' value='delete'>
														<i class='fas fa-trash'></i>
													</button>
												</div>
											</form>
										</td>
									</tr>
								<?php 
										endforeach; 
									endif;
								?>
							</tbody>
						</table>
					</div>
					
				</div>
			</div>
		</div>
	</div>

<?php template::get_footer(); 