<?php 
// 1. Force Error Reporting (This prevents the "Blank Page" by showing you the error)
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();

// 2. Access Control
if(!isset($_SESSION['admin_login'])) {
    header('location:adminlogin.php');
    exit;
}

// 3. Database Connection
require '_inc/dbconn.php'; 

// 4. Input Validation Logic
// We check POST first, then GET. If both are empty, we redirect to prevent a crash.
$raw_id = $_POST['customer_id'] ?? $_GET['customer_id'] ?? null;

if (!$raw_id) {
    header('location:display_customer.php?error=no_id_selected');
    exit;
}

// Sanitize and cast to string to avoid Deprecation warnings
$id = mysqli_real_escape_string($con, (string)$raw_id);

// 5. Fetch User Data
$sql = "SELECT * FROM users WHERE id='$id'";
$result = mysqli_query($con, $sql);

if (!$result || mysqli_num_rows($result) == 0) {
    die("Error: User not found in the 'users' table. <a href='display_customer.php'>Go Back</a>");
}

$rws = mysqli_fetch_assoc($result);

// 6. Include Visual Components
include 'admin_header.php'; 
include 'admin_navbar.php'; 
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit User Profile</title>
    <style>
        :root { --primary: #0052FF; --bg: #F8FAFC; --border: #E2E8F0; }
        
        body { background-color: var(--bg); font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }

        .main-content { 
            margin-left: 260px; 
            transition: all 0.3s ease; 
            padding: 30px; 
            min-height: 100vh;
        }

        .edit-card {
            background: #ffffff;
            border-radius: 16px;
            border: 1px solid var(--border);
            box-shadow: 0 4px 20px rgba(0,0,0,0.05);
            max-width: 850px;
            margin: 0 auto;
            overflow: hidden;
        }

        .card-header { background: #fff; padding: 25px; border-bottom: 1px solid var(--border); }
        .card-body { padding: 30px; }

        .form-group label { font-weight: 600; color: #475569; font-size: 0.85rem; text-transform: uppercase; margin-bottom: 8px; display: block; }
        .form-control { width: 100%; border-radius: 10px; border: 1.5px solid var(--border); padding: 12px; transition: 0.2s; box-sizing: border-box; }
        .form-control:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 4px rgba(0, 82, 255, 0.1); }

        .btn-submit {
            background: linear-gradient(135deg, #0052FF 0%, #003DB3 100%);
            color: white;
            padding: 15px 30px;
            border-radius: 12px;
            border: none;
            font-weight: 700;
            width: 100%;
            cursor: pointer;
            box-shadow: 0 4px 6px rgba(0, 82, 255, 0.2);
            margin-top: 20px;
        }

        /* Mobile View Fix */
        @media (max-width: 992px) {
            .main-content { margin-left: 0 !important; padding: 15px; }
            .row { display: flex; flex-direction: column; }
            .col-md-6 { width: 100% !important; }
        }

        .row { display: flex; flex-wrap: wrap; margin-right: -15px; margin-left: -15px; }
        .col-md-6 { position: relative; width: 50%; padding-right: 15px; padding-left: 15px; margin-bottom: 20px; }
    </style>
</head>
<body>

<div class="main-content">
    <div class="edit-card">
        <div class="card-header">
            <h4 style="margin:0; color:#1E293B;">Modify User Account</h4>
            <p style="margin:5px 0 0; color:#64748B; font-size:0.9rem;">Updating records for: <strong><?php echo htmlspecialchars($rws['fullname']); ?></strong></p>
        </div>

        <div class="card-body">
            <form action="update_customer.php" method="POST">
                <input type="hidden" name="user_id" value="<?php echo $rws['id']; ?>">

                <div class="row">
                    <div class="col-md-6 form-group">
                        <label>Full Name</label>
                        <input type="text" name="fullname" class="form-control" value="<?php echo htmlspecialchars($rws['fullname']); ?>" required>
                    </div>

                    <div class="col-md-6 form-group">
                        <label>Email Address</label>
                        <input type="email" name="email" class="form-control" value="<?php echo htmlspecialchars($rws['email']); ?>" required>
                    </div>

                    <div class="col-md-6 form-group">
                        <label>Account Number (Read Only)</label>
                        <input type="text" class="form-control" style="background:#f1f5f9;" value="<?php echo $rws['account_number']; ?>" readonly>
                    </div>

                    <div class="col-md-6 form-group">
                        <label>Available Balance ($)</label>
                        <input type="number" name="balance" class="form-control" value="<?php echo $rws['balance']; ?>" step="0.01" required>
                    </div>

                    <div class="col-md-6 form-group">
                        <label>Account Status</label>
                        <select name="status" class="form-control">
                            <option value="1" <?php echo ($rws['verified'] == 1) ? 'selected' : ''; ?>>Active / Verified</option>
                            <option value="0" <?php echo ($rws['verified'] == 0) ? 'selected' : ''; ?>>Blocked / Pending</option>
                        </select>
                    </div>

                    <div class="col-md-6 form-group">
                        <label>Login PIN</label>
                        <input type="text" name="pin" class="form-control" value="<?php echo $rws['account_pin']; ?>" maxlength="4">
                    </div>
                </div>

                <button type="submit" name="perform_update" class="btn-submit">
                    SAVE UPDATED INFORMATION
                </button>
            </form>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
</body>
</html>
