<?php

include "../includes/admin_auth.php";
include "../config/database.php";

if(!isset($_GET['id'])){

    header("Location: members.php");
    exit();

}

$memberId = (int)$_GET['id'];

/*
|--------------------------------------------------------------------------
| Get Member
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT *
    FROM users
    WHERE id=?
    AND role='member'
    LIMIT 1
");

$stmt->execute([$memberId]);

$member = $stmt->fetch(PDO::FETCH_ASSOC);

if(!$member){

    $_SESSION['error'] = "Member not found.";

    header("Location: members.php");
    exit();

}

?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Edit Member</title>

<link rel="stylesheet" href="../assets/css/admin.css">
<link rel="stylesheet" href="../assets/css/responsive.css">

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">

</head>

<body>

<?php include "../includes/admin_sidebar.php"; ?>

<div class="sidebar-overlay"></div>

<?php include "../includes/admin_topbar.php"; ?>


<div class="main-content">

<div class="page-header">

<div>

<h1>
<i class="fas fa-user-edit"></i>
Edit Member
</h1>

<p>
Update member information
</p>

</div>

<a href="member_details.php?id=<?= $memberId; ?>" class="back-btn">

<i class="fas fa-arrow-left"></i>

Back

</a>

</div>


<div class="table-card">

<h3>

<i class="fas fa-user"></i>

Member Information

</h3>


<form action="save_member.php" method="POST">

<input
type="hidden"
name="id"
value="<?= $memberId; ?>">


<div class="form-group">

<label>Full Name</label>

<input
type="text"
name="fullname"
class="form-control"
value="<?= htmlspecialchars($member['fullname']); ?>"
required>

</div>


<br>


<div class="form-group">

<label>Email</label>

<input
type="email"
name="email"
class="form-control"
value="<?= htmlspecialchars($member['email'] ?? ''); ?>">

</div>


<br>


<div class="form-group">

<label>Phone</label>

<input
type="text"
name="phone"
class="form-control"
value="<?= htmlspecialchars($member['phone']); ?>"
required>

</div>


<br>


<div class="form-group">

<label>Status</label>

<select
name="status"
class="form-control"
required>

<option
value="active"
<?= $member['status']=="active" ? "selected" : ""; ?>>

Active

</option>

<option
value="suspended"
<?= $member['status']=="suspended" ? "selected" : ""; ?>>

Suspended

</option>

</select>

</div>


<br>


<div class="form-group">

<label>Virtual Bank</label>

<input
type="text"
class="form-control"
value="<?= htmlspecialchars($member['virtual_bank'] ?? ''); ?>"
readonly>

</div>


<br>


<div class="form-group">

<label>Virtual Account Number</label>

<input
type="text"
class="form-control"
value="<?= htmlspecialchars($member['virtual_account_number'] ?? ''); ?>"
readonly>

</div>


<br>


<div class="form-group">

<label>Virtual Account Name</label>

<input
type="text"
class="form-control"
value="<?= htmlspecialchars($member['virtual_account_name'] ?? ''); ?>"
readonly>

</div>


<br>


<div class="form-actions">

<a href="member_details.php?id=<?= $memberId; ?>"
class="back-btn">

<i class="fas fa-times"></i>

Cancel

</a>


<button type="submit" class="fund-wallet-btn">

<i class="fas fa-save"></i>

Save Changes

</button>

</div>


</form>

</div>

</div>


<script src="../assets/js/admin.js"></script>

</body>

</html>