<?php
session_start();

if (isset($_SESSION['username']) == true) {
	include 'master.php';
	$username = $_SESSION['username'];
}else{
	header("location:login");
}

if (isset($_GET['id']) == true) {
	$id = $_GET['id'];

	$_em_stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
	$_em_stmt->bind_param("i", $id);
	$_em_stmt->execute();
	$val = $_em_stmt->get_result()->fetch_assoc();
	$_em_stmt->close();
	$fullname = $val['fullname'];
	$address = $val['residential_address'];
	$city = $val['city'];
	$state = $val['state'];
	$country = $val['country'];
	$email = $val['email'];
	$phone_number = $val['phone_number'];
	$date_of_birth = $val['date_of_birth'];
	$gender = $val['gender'];
	$acc_type = $val['acc_type'];
	$currency = $val['currency'];
	$acc_no = $val['acc_no'];
	$username = $val['username'];
	$pin = $val['pin'];
	$balance = $val['balance'];
	$acc_limit = $val['acc_limit'];
	$profile_img = $val['profile_img'];
	$otp = $val['otp'];
	$account_status = $val['account_status'];
	$allow_transactions = $val['allowTransactions'] ?? 'No';
}


?>

		<!--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-->
						<div class="row justify-content-center">
						<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>Full Name</label>
											<input type="text" class="form-control border-left" name="fullname" value="<?php echo $fullname;?>">
										</div>
										<div class="form-group">
											<label>Address</label>
											<input type="text" class="form-control border-left" name="residential_address" value="<?php echo $address;?>">
										</div>
										<div class="form-group">
											<label>City</label>
											<input type="text" class="form-control border-left" name="city" value="<?php echo $city;?>">
										</div>
										<div class="form-group">
											<label>State</label>
											<input type="text" class="form-control border-left" name="state" value="<?php echo $state;?>">
										</div>
										<div class="form-group">
											<label>Country</label>
											<input type="text" class="form-control border-left" name="country" value="<?php echo $country;?>">
										</div>
										<div class="form-group">
											<label>Email</label>
											<input type="text" class="form-control border-left" name="email" value="<?php echo $email;?>">
										</div>
										<div class="form-group">
											<label>Phone Number</label>
											<input type="text" class="form-control border-left" name="phone_number" value="<?php echo $phone_number;?>">
										</div>
										<div class="form-group">
											<label>Date Of Birth</label>
											<input type="text" class="form-control border-left" name="date_of_birth" value="<?php echo $date_of_birth;?>">
										</div>
										<div class="form-group">
											<label>Gender</label>
											<select class="form-control border-left" name="gender">
												<option value="Male"   <?php echo $gender === 'Male'   ? 'selected' : ''; ?>>Male</option>
												<option value="Female" <?php echo $gender === 'Female' ? 'selected' : ''; ?>>Female</option>
												<option value="Other"  <?php echo $gender === 'Other'  ? 'selected' : ''; ?>>Other</option>
											</select>
										</div>
										<div class="form-group">
											<label>Account Type</label>
											<select class="form-control border-left" name="acc_type">
												<option value="Checking Account"     <?php echo $acc_type === 'Checking Account'     ? 'selected' : ''; ?>>Checking Account</option>
												<option value="Savings Account"      <?php echo $acc_type === 'Savings Account'      ? 'selected' : ''; ?>>Savings Account</option>
												<option value="Fixed Deposit Account"<?php echo $acc_type === 'Fixed Deposit Account'? 'selected' : ''; ?>>Fixed Deposit Account</option>
												<option value="Current Account"      <?php echo $acc_type === 'Current Account'      ? 'selected' : ''; ?>>Current Account</option>
												<option value="Business Account"     <?php echo $acc_type === 'Business Account'     ? 'selected' : ''; ?>>Business Account</option>
											</select>
										</div>
										<div class="form-group">
											<label>Currency</label>
											<select class="form-control border-left" name="currency">
												<option value="AED" <?php echo $currency==='AED'?'selected':'';?>>AED - د.إ</option>
												<option value="AFN" <?php echo $currency==='AFN'?'selected':'';?>>AFN - Af</option>
												<option value="ALL" <?php echo $currency==='ALL'?'selected':'';?>>ALL - Lek</option>
												<option value="ARS" <?php echo $currency==='ARS'?'selected':'';?>>ARS - $</option>
												<option value="AUD" <?php echo $currency==='AUD'?'selected':'';?>>AUD - $</option>
												<option value="AZN" <?php echo $currency==='AZN'?'selected':'';?>>AZN - ман</option>
												<option value="BAM" <?php echo $currency==='BAM'?'selected':'';?>>BAM - KM</option>
												<option value="BDT" <?php echo $currency==='BDT'?'selected':'';?>>BDT - ৳</option>
												<option value="BGN" <?php echo $currency==='BGN'?'selected':'';?>>BGN - лв</option>
												<option value="BHD" <?php echo $currency==='BHD'?'selected':'';?>>BHD - .د.ب</option>
												<option value="BRL" <?php echo $currency==='BRL'?'selected':'';?>>BRL - R$</option>
												<option value="BTC" <?php echo $currency==='BTC'?'selected':'';?>>BTC - BTC</option>
												<option value="CAD" <?php echo $currency==='CAD'?'selected':'';?>>CAD - $</option>
												<option value="CHF" <?php echo $currency==='CHF'?'selected':'';?>>CHF - CHF</option>
												<option value="CNY" <?php echo $currency==='CNY'?'selected':'';?>>CNY - ¥</option>
												<option value="CZK" <?php echo $currency==='CZK'?'selected':'';?>>CZK - Kč</option>
												<option value="DKK" <?php echo $currency==='DKK'?'selected':'';?>>DKK - kr</option>
												<option value="EGP" <?php echo $currency==='EGP'?'selected':'';?>>EGP - £</option>
												<option value="ETB" <?php echo $currency==='ETB'?'selected':'';?>>ETB - Br</option>
												<option value="ETH" <?php echo $currency==='ETH'?'selected':'';?>>ETH - ETH</option>
												<option value="EUR" <?php echo $currency==='EUR'?'selected':'';?>>EUR - €</option>
												<option value="GBP" <?php echo $currency==='GBP'?'selected':'';?>>GBP - £</option>
												<option value="GHS" <?php echo $currency==='GHS'?'selected':'';?>>GHS - ¢</option>
												<option value="HKD" <?php echo $currency==='HKD'?'selected':'';?>>HKD - $</option>
												<option value="HUF" <?php echo $currency==='HUF'?'selected':'';?>>HUF - Ft</option>
												<option value="IDR" <?php echo $currency==='IDR'?'selected':'';?>>IDR - Rp</option>
												<option value="ILS" <?php echo $currency==='ILS'?'selected':'';?>>ILS - ₪</option>
												<option value="INR" <?php echo $currency==='INR'?'selected':'';?>>INR - ₹</option>
												<option value="IQD" <?php echo $currency==='IQD'?'selected':'';?>>IQD - ع.د</option>
												<option value="IRR" <?php echo $currency==='IRR'?'selected':'';?>>IRR - ﷼</option>
												<option value="JPY" <?php echo $currency==='JPY'?'selected':'';?>>JPY - ¥</option>
												<option value="KES" <?php echo $currency==='KES'?'selected':'';?>>KES - KSh</option>
												<option value="KRW" <?php echo $currency==='KRW'?'selected':'';?>>KRW - ₩</option>
												<option value="KWD" <?php echo $currency==='KWD'?'selected':'';?>>KWD - د.ك</option>
												<option value="LBP" <?php echo $currency==='LBP'?'selected':'';?>>LBP - £</option>
												<option value="LKR" <?php echo $currency==='LKR'?'selected':'';?>>LKR - ₨</option>
												<option value="MAD" <?php echo $currency==='MAD'?'selected':'';?>>MAD - د.م.</option>
												<option value="MXN" <?php echo $currency==='MXN'?'selected':'';?>>MXN - $</option>
												<option value="MYR" <?php echo $currency==='MYR'?'selected':'';?>>MYR - RM</option>
												<option value="NGN" <?php echo $currency==='NGN'?'selected':'';?>>NGN - ₦</option>
												<option value="NOK" <?php echo $currency==='NOK'?'selected':'';?>>NOK - kr</option>
												<option value="NPR" <?php echo $currency==='NPR'?'selected':'';?>>NPR - ₨</option>
												<option value="NZD" <?php echo $currency==='NZD'?'selected':'';?>>NZD - $</option>
												<option value="OMR" <?php echo $currency==='OMR'?'selected':'';?>>OMR - ﷼</option>
												<option value="PHP" <?php echo $currency==='PHP'?'selected':'';?>>PHP - ₱</option>
												<option value="PKR" <?php echo $currency==='PKR'?'selected':'';?>>PKR - ₨</option>
												<option value="PLN" <?php echo $currency==='PLN'?'selected':'';?>>PLN - zł</option>
												<option value="QAR" <?php echo $currency==='QAR'?'selected':'';?>>QAR - ﷼</option>
												<option value="RON" <?php echo $currency==='RON'?'selected':'';?>>RON - lei</option>
												<option value="RUB" <?php echo $currency==='RUB'?'selected':'';?>>RUB - руб</option>
												<option value="SAR" <?php echo $currency==='SAR'?'selected':'';?>>SAR - ﷼</option>
												<option value="SEK" <?php echo $currency==='SEK'?'selected':'';?>>SEK - kr</option>
												<option value="SGD" <?php echo $currency==='SGD'?'selected':'';?>>SGD - $</option>
												<option value="THB" <?php echo $currency==='THB'?'selected':'';?>>THB - ฿</option>
												<option value="TRY" <?php echo $currency==='TRY'?'selected':'';?>>TRY - ₤</option>
												<option value="TWD" <?php echo $currency==='TWD'?'selected':'';?>>TWD - NT$</option>
												<option value="UAH" <?php echo $currency==='UAH'?'selected':'';?>>UAH - ₴</option>
												<option value="UGX" <?php echo $currency==='UGX'?'selected':'';?>>UGX - USh</option>
												<option value="USD" <?php echo $currency==='USD'?'selected':'';?>>USD - $</option>
												<option value="VND" <?php echo $currency==='VND'?'selected':'';?>>VND - ₫</option>
												<option value="XAF" <?php echo $currency==='XAF'?'selected':'';?>>XAF - FCFA</option>
												<option value="ZAR" <?php echo $currency==='ZAR'?'selected':'';?>>ZAR - R</option>
											</select>
										</div>
										<div class="form-group">
											<label>Account Number</label>
											<input type="text" class="form-control border-left" name="acc_no" value="<?php echo $acc_no;?>">
										</div>
										<div class="form-group">
											<label>Username</label>
											<input type="text" class="form-control border-left" name="username" value="<?php echo $username;?>">
										</div>
										<div class="form-group">
											<label>Pin</label>
											<input type="text" class="form-control border-left" name="pin" value="<?php echo $pin;?>">
										</div>
										<div class="form-group">
											<label>Balance</label>
											<input type="number" step="0.01" min="0" class="form-control border-left" name="balance" value="<?php echo $balance;?>">
										</div>
										<div class="form-group">
											<label>Account Limit</label>
											<input type="number" step="0.01" min="0" class="form-control border-left" name="acc_limit" value="<?php echo $acc_limit;?>">
										</div>
										<div class="form-group">
											<label>Profile Image</label>
											<?php if (!empty($profile_img)): ?>
												<div class="mb-2">
													<img src="../uploads/profile_pictures/<?php echo htmlspecialchars($profile_img); ?>" alt="Current Photo" style="width:80px;height:80px;object-fit:cover;border-radius:50%;border:2px solid #dee2e6;">
													<small class="d-block text-muted mt-1">Current: <?php echo htmlspecialchars($profile_img); ?></small>
												</div>
											<?php endif; ?>
											<input type="file" class="form-control border-left" name="profile_img_file" accept="image/*">
											<small class="text-muted">Leave empty to keep current image.</small>
										</div>
										<div class="form-group">
											<label>OTP</label>
											<input type="text" class="form-control border-left" name="otp" value="<?php echo $otp;?>">
										</div>
										<div class="form-group">
											<label>Account Status</label>
											<select class="form-control border-left" name="account_status">
												<option value="Active"   <?php echo $account_status === 'Active'   ? 'selected' : ''; ?>>Active</option>
												<option value="Pending"  <?php echo $account_status === 'Pending'  ? 'selected' : ''; ?>>Pending</option>
												<option value="Disabled" <?php echo $account_status === 'Disabled' ? 'selected' : ''; ?>>Disabled</option>
												<option value="on-hold"  <?php echo $account_status === 'on-hold'  ? 'selected' : ''; ?>>On Hold</option>
												<option value="suspended"<?php echo $account_status === 'suspended'? 'selected' : ''; ?>>Suspended</option>
											</select>
										</div>
										<div class="form-group">
											<label>Allow Transactions</label>
											<select class="form-control border-left" name="allowTransactions">
												<option value="Yes" <?php echo $allow_transactions === 'Yes' ? 'selected' : ''; ?>>Yes — Allowed</option>
												<option value="No"  <?php echo $allow_transactions !== 'Yes' ? 'selected' : ''; ?>>No — Restricted</option>
											</select>
										</div>

										<div class="form-group">
											<button type="submit" class="btn btn-primary" name="update" onclick="return confirm('Are you sure you want to make changes?')">Update</button>
										</div>
									</form>
								</div>
							</div>
						</div>
					</div>
					<!--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-->
<script type="text/javascript">
    CKEDITOR.replace('editor');
</script>
<?php
include 'footer.php';

if (isset($_POST['update']) == true) {
	$new_fullname = $_POST['fullname'];
	$new_address = $_POST['residential_address'];
	$new_city = $_POST['city'];
	$new_state = $_POST['state'];
	$new_country = $_POST['country'];
	$new_email = $_POST['email'];
	$new_phone_number = $_POST['phone_number'];
	$new_date_of_birth = $_POST['date_of_birth'];
	$new_gender = $_POST['gender'];
	$new_acc_type = $_POST['acc_type'];
	$new_currency = $_POST['currency'];
	$new_acc_no = $_POST['acc_no'];
	$new_username = $_POST['username'];
	$new_pin = $_POST['pin'];
	$new_balance = $_POST['balance'];
	$new_acc_limit = $_POST['acc_limit'];
	$new_otp = $_POST['otp'];
	$new_account_status = $_POST['account_status'];
	$new_allow_transactions = $_POST['allowTransactions'];

	// Handle profile image upload
	$new_profile_img = $profile_img; // keep existing by default
	if (!empty($_FILES['profile_img_file']['name'])) {
		$upload_dir = '../uploads/profile_pictures/';
		$ext = strtolower(pathinfo($_FILES['profile_img_file']['name'], PATHINFO_EXTENSION));
		$allowed = ['jpg','jpeg','png','gif','webp'];
		if (in_array($ext, $allowed)) {
			$new_filename = 'user_' . $id . '_' . time() . '.' . $ext;
			move_uploaded_file($_FILES['profile_img_file']['tmp_name'], $upload_dir . $new_filename);
			$new_profile_img = $new_filename;
		}
	}

    $upd_stmt = $conn->prepare("UPDATE users SET fullname = ?, residential_address = ?, city = ?, state = ?, country = ?, email = ?, phone_number = ?, date_of_birth = ?, gender = ?, acc_type = ?, currency = ?, acc_no = ?, username = ?, pin = ?, balance = ?, acc_limit = ?, profile_img = ?, otp = ?, account_status = ?, allowTransactions = ? WHERE id = ?");
    $upd_stmt->bind_param("ssssssssssssssssssssi",
        $new_fullname, $new_address, $new_city, $new_state, $new_country,
        $new_email, $new_phone_number, $new_date_of_birth, $new_gender,
        $new_acc_type, $new_currency, $new_acc_no, $new_username, $new_pin,
        $new_balance, $new_acc_limit, $new_profile_img, $new_otp, $new_account_status,
        $new_allow_transactions, $id
    );
    $query_ok = $upd_stmt->execute();
    $upd_stmt->close();
    if ($query_ok) {
    	echo "<script>swal({text:'Member Profile Updated Successfully', icon:'success'})
    	.then(function(){
    		window.location = 'members'
    		})
    	</script>";
    }
}