<?php 
session_start();
if(!isset($_SESSION['admin_login'])) {
    header('location:adminlogin.php');
    exit;
}

include '_inc/dbconn.php';

// FIX 1: Check if customer_id exists before using it
if(!isset($_POST['customer_id']) || empty($_POST['customer_id'])) {
    // If no user was selected, redirect back to the list with an alert
    echo "<script>alert('Please select a user to edit.'); window.location='display_customer.php';</script>";
    exit;
}

$id = mysqli_real_escape_string($con, $_POST['customer_id']);

// FIX 2: Updated query to target 'users' table
$sql = "SELECT * FROM users WHERE id='$id'";
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
$rws = mysqli_fetch_assoc($result);

// If no user found in DB with that ID
if(!$rws) {
    echo "<script>alert('User not found.'); window.location='display_customer.php';</script>";
    exit;
}

include 'admin_header.php';
include 'admin_navbar.php';
?>

<style>
    .main-content { margin-left: 260px; transition: 0.3s; background: #F8FAFC; min-height: 100vh; padding: 30px; }
    @media (max-width: 992px) { .main-content { margin-left: 0 !important; padding: 15px; } }
    
    .edit-card {
        background: #fff;
        border-radius: 16px;
        border: 1px solid #E2E8F0;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        max-width: 800px;
        margin: 0 auto;
    }
    .form-control { border-radius: 8px; padding: 12px; border: 1.5px solid #E2E8F0; }
    .btn-save { background: #0052FF; color: white; font-weight: 700; border-radius: 10px; padding: 12px; border: none; width: 100%; }
</style>

<div class="main-content">
    <div class="container-fluid">
        <div class="edit-card">
            <div class="card-header bg-white py-4 px-4 border-bottom">
                <h4 class="mb-0 font-weight-bold">Modify User: <?php echo htmlspecialchars($rws['fullname']); ?></h4>
            </div>
            
            <div class="card-body p-4">
                <form action="update_customer.php" method="POST">
                    <input type="hidden" name="current_id" value="<?php echo $rws['id']; ?>">
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="small font-weight-bold text-muted">Full Name</label>
                            <input type="text" name="name" class="form-control" value="<?php echo $rws['fullname']; ?>" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="small font-weight-bold text-muted">Email Address</label>
                            <input type="email" name="email" class="form-control" value="<?php echo $rws['email']; ?>" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="small font-weight-bold text-muted">Account Number</label>
                            <input type="text" name="account_no" class="form-control bg-light" value="<?php echo $rws['account_number']; ?>" readonly>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="small font-weight-bold text-muted">Account Balance ($)</label>
                            <input type="number" name="balance" class="form-control" value="<?php echo $rws['balance']; ?>" step="0.01">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="small font-weight-bold text-muted">Security PIN</label>
                            <input type="text" name="pin" class="form-control" value="<?php echo $rws['account_pin']; ?>" maxlength="4">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="small font-weight-bold text-muted">Account Status</label>
                            <select name="status" class="form-control">
                                <option value="1" <?php if($rws['verified'] == 1) echo 'selected'; ?>>Active</option>
                                <option value="0" <?php if($rws['verified'] == 0) echo 'selected'; ?>>Pending / Blocked</option>
                            </select>
                        </div>
                    </div>
                    
                    <div class="mt-4">
                        <button type="submit" name="update_user" class="btn-save shadow-sm">
                            <i class="mdi mdi-content-save mr-2"></i> SAVE UPDATED RECORDS
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
