<?php

require __dir__ . '/init.php';


if($_SERVER['REQUEST_METHOD'] == 'POST') {
	
	$_POST = help::sanitize($_POST);
	
	$SQL = sQuery::update( 'tbl_users', $_POST, "email='{$email}'" );
	
	$update = $link->query( $SQL );
	
	if( $update ) {
		$msg = "Card updated and ";
		$msg .= empty($_POST['sstatus']) ? "deactivated" : "activated";
	} else $msg = "Cannot Activate Card! ";
	
};


template::get_header();

$user = help::get_user( 'admin@yourdomain.com' );

?>

	<?php template::title( "CARD INFORMATION" ); ?>

	<div class="section-body">
		<div class="invoice">
			<div class="invoice-print">
				<div class="row">
					<div class="col-lg-12">
					
						<?php template::alert( $msg, $update ?? null ); ?>
						
						<form action="card-edit.php?email=<?php echo $email ;?>" method="POST">
						
							<?php
								$content = array(
								
									"First Name" => array(null, $user['fname'], 'readonly'),
									"Last Name" => array(null, $user['lname'], 'readonly'),
									"Account Type" => array(null, $user['utype'], 'readonly'),
									"Account Number" => array(null, $user['acctno'], 'readonly'),
									
									"Card" => array('card', $user['card']),
									
									"Card Type" => array('//', function() use($user) { 
										$types = ['Credit Master Card', 'Debit Master Card', 'Credit Visa Card', 'Debit Visa Card'];
									?>
										<select name='cardt' class='form-control'>
											<?php foreach( $types as $value ): ?>
												<option value='<?php echo $value; ?>' <?php if( $value == $user['cardt'] ) echo 'selected'; ?>><?php echo $value; ?></option>
											<?php endforeach; ?>
										</select>
									<?php }),
									
									"Month" => array('month', $user['month'], 'type="number"'),
									"Year" => array('yr', $user['yr'], "type='number'"),
									
									"CSV" => array('csv', $user['csv'], "type='number'"),
									
									"Card Status" => array('//', function() use($user) { ?>
										<select name='sstatus' class='form-control'>
											<?php foreach(['Deactived', 'Actived'] as $key => $value): ?>
												<option value='<?php echo $key; ?>' <?php if((int)$user['sstatus'] === $key) echo 'selected'; ?>>
													<?php echo $value; ?>
												</option>
											<?php endforeach; ?>
										</select>
									<?php })
									
								);
								
								foreach( $content as $label => $data ):
							?>
								<div class='form-group'>
									<label><?php echo $label; ?></label>
									<?php if( $data[0] != '//' ) { ?>
									<input class='form-control' <?php if( !empty($data[0]) ) echo "name='{$data[0]}'"; ?> value="<?php echo $data[1]; ?>" <?php echo $data[2] ?? null; ?>>
									<?php } else $data[1](); ?>
								</div>
							<?php endforeach; ?>
							
							<div class='form-group'>
								<button  type="submit" class="btn btn-success btn-icon icon-left">
									<i class="fas fa-check"></i> Update Card
								</button>
							</div>
							
						</form>
						
					</div>
				</div>
			</div>
		</div>
	</div>

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