<?php
session_start();

if (isset($_SESSION['username']) == true) {
	include 'master.php';
	$username = $_SESSION['username'];
}else{
	header("location:login");
}
?>
		<!--page-wrapper-->
		<div class="page-wrapper">
			<!--page-content-wrapper-->
			<div class="page-content-wrapper">
				<div class="page-content">
					<!--breadcrumb-->
					<div class="page-breadcrumb d-none d-md-flex align-items-center mb-3">
						<div class="breadcrumb-title pr-3">Edit Post</div>
						<div class="pl-3">
							<nav aria-label="breadcrumb">
								<ol class="breadcrumb mb-0 p-0">
									<li class="breadcrumb-item"><a href="javascript:;"><i class='bx bx-home-alt'></i></a>
									</li>
									<li class="breadcrumb-item active" aria-current="page">Layouts</li>
								</ol>
							</nav>
						</div>
						<div class="ml-auto">
							<div class="btn-group">
								<button type="button" class="btn btn-primary">Active</button>
							</div>
						</div>
					</div><br><br>
					<!--end breadcrumb-->
					<?php
						$acc_no = '';
						if (isset($_GET['account_number'])) {
							$acc_no = $_GET['account_number'];
						}
						$_af_stmt = $conn->prepare("SELECT * FROM users WHERE acc_no = ?");
						$_af_stmt->bind_param("s", $acc_no);
						$_af_stmt->execute();
						$sql = $_af_stmt->get_result();
						$_af_stmt->close();
						foreach ($sql as $key) {
					?>
						<div class="row">
						<div class="col-12 col-lg-9 mx-auto">
							<div class="card radius-15">
								<div class="card-body">
									<form action="" method="post" enctype="multipart/form-data">
										<div class="form-group">
											<label>Account N0</label>
											<input type="text" class="form-control border-left" disabled name="acc_no" value="<?php echo $key['acc_no'];?>">
										</div>
										<div class="form-group">
											<label>Email</label>
											<input type="text" class="form-control border-left" disabled name="email" value="<?php echo $key['email'];?>">
										</div>
										<div class="form-group">
											<label>Previous Balance</label>
											<input type="text" class="form-control border-left" disabled name="balance" value="$<?php echo $key['balance'];?>">
										</div>
										<div class="form-group">
											<label>Amount</label>
											<input type="text" class="form-control border-left" name="amount" value="">
										</div>
										
											<div class="form-group">
											<label>Date</label>
										 <input type="text" class="form-control border-left" id="fdate" name="fdate" placeholder="2026-04-30 13:36:35" value="<?php echo date('Y-m-d H:i:s'); ?>">
										</div>
										<div class="form-group">
											<button type="submit" class="btn btn-primary px-4" name="update" onclick="return confirm('Are you sure you want to add funds?')">Add Funds</button>
										</div>
									</form>
								</div>
							</div>
						</div>
					</div>
				<?php } ?>
					<!--end row-->
				</div>
			</div>
			<!--end page-content-wrapper-->
		</div>
		<!--end page-wrapper-->
		<!--start overlay-->
		<div class="overlay toggle-btn-mobile"></div>
		<!--end overlay-->
		<!--Start Back To Top Button--> <a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
		<!--End Back To Top Button-->
<?php
include 'footer.php';

if (isset($_POST['update']) == true) {
	$prev_bal = $_POST['balance'];
	$amount = $_POST['amount'];
	$transaction_type = "Credit";
	$transaction_details = "Notice of Successful Account Top Up";
	$_af_sel = $conn->prepare("SELECT fullname, balance, acc_type FROM users WHERE acc_no = ?");
	$_af_sel->bind_param("s", $acc_no);
	$_af_sel->execute();
	$user = $_af_sel->get_result()->fetch_assoc();
	$_af_sel->close();
	$date_added = $_POST['fdate'];
	if ($user) {
	    $currbal = $user['balance'];
	    $newbal = $amount + $currbal;
	    $name = $user['fullname'];
	    $account_type = $user['acc_type'];

	    $_af_upd = $conn->prepare("UPDATE users SET balance = ? WHERE acc_no = ?");
	    $_af_upd->bind_param("ds", $newbal, $acc_no);
	    $query_ok = $_af_upd->execute();
	    $_af_upd->close();

	    $_af_ins = $conn->prepare("INSERT INTO transactions (name, account_number, account_type, amount, prev_bal, new_bal, new_balance, transaction_type, transaction_details, status, stat, date_added) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'Success', 'Deposit', ?)");
	    $_af_ins->bind_param("ssssssssss", $name, $acc_no, $account_type, $amount, $currbal, $newbal, $newbal, $transaction_type, $transaction_details, $date_added);
	    $_af_ins->execute();
	    $_af_ins->close();

	    if ($query_ok) {
		echo "<script>swal({text:'Account has been funded successfully', icon:'success'})
			.then(function(){
				window.location = 'members'
				})
		</script>";
	    } else {
		echo "<script>swal({text:'Failed to fund Account', icon:'error'})
			.then(function(){
				window.location = 'members'
				})
		</script>";
	    }
	}


	
}