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

include '_inc/dbconn.php';

// Fetch users from the 'users' table
$sql = "SELECT * FROM users ORDER BY id DESC";
$result = mysqli_query($con, $sql) or die(mysqli_error($con));

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

<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">

<style>
    :root {
        --primary-blue: #0052FF;
        --soft-bg: #F8FAFC;
        --border-color: #E2E8F0;
        --text-dark: #1E293B;
    }

    body {
        font-family: 'Inter', sans-serif;
        background-color: var(--soft-bg);
    }

    .main-content {
        margin-left: 260px; /* Sidebar Width */
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        padding: 30px;
    }

    @media (max-width: 992px) {
        .main-content { margin-left: 0 !important; padding: 15px; }
    }

    /* Page Header */
    .page-title-box {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 25px;
    }

    .page-title {
        font-size: 1.5rem;
        font-weight: 700;
        color: var(--text-dark);
        letter-spacing: -0.5px;
    }

    /* Modern Table Card */
    .glass-card {
        background: #ffffff;
        border: 1px solid var(--border-color);
        border-radius: 16px;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
        overflow: hidden;
    }

    .table-responsive {
        border: none;
    }

    .table {
        margin-bottom: 0;
        width: 100%;
        border-collapse: separate;
        border-spacing: 0;
    }

    .table thead th {
        background: #F1F5F9;
        padding: 16px 24px;
        color: #64748B;
        font-weight: 600;
        text-transform: uppercase;
        font-size: 11px;
        letter-spacing: 0.5px;
        border: none;
    }

    .table tbody td {
        padding: 18px 24px;
        vertical-align: middle;
        color: #334155;
        border-top: 1px solid #F1F5F9;
        font-size: 14px;
    }

    /* Custom Radio Styling */
    .custom-radio-container {
        display: block;
        position: relative;
        cursor: pointer;
        user-select: none;
    }

    .custom-radio-container input {
        position: absolute;
        opacity: 0;
    }

    .checkmark {
        height: 20px;
        width: 20px;
        background-color: #eee;
        border-radius: 50%;
        display: inline-block;
        border: 2px solid var(--border-color);
    }

    .custom-radio-container input:checked ~ .checkmark {
        background-color: var(--primary-blue);
        border-color: var(--primary-blue);
        box-shadow: 0 0 0 4px rgba(0, 82, 255, 0.1);
    }

    /* Status Badges */
    .status-badge {
        padding: 6px 12px;
        border-radius: 9999px;
        font-size: 12px;
        font-weight: 600;
    }

    .badge-active { background: #DCFCE7; color: #166534; }
    .badge-pending { background: #FEF3C7; color: #92400E; }

    /* Account styling */
    .acc-number {
        font-family: 'Monaco', 'Consolas', monospace;
        background: #F8FAFC;
        padding: 4px 8px;
        border-radius: 6px;
        color: var(--primary-blue);
        font-weight: 600;
    }

    /* Floating Submit Button */
    .btn-edit-float {
        background: linear-gradient(135deg, #0052FF 0%, #003DB3 100%);
        color: white;
        padding: 16px 40px;
        border-radius: 12px;
        border: none;
        font-weight: 700;
        box-shadow: 0 10px 15px -3px rgba(0, 82, 255, 0.3);
        transition: all 0.2s ease;
        text-transform: uppercase;
        letter-spacing: 1px;
    }

    .btn-edit-float:hover {
        transform: translateY(-2px);
        box-shadow: 0 20px 25px -5px rgba(0, 82, 255, 0.4);
        color: white;
    }
</style>

<div class="main-content">
    <div class="container-fluid">
        
        <div class="page-title-box">
            <h1 class="page-title">User Management</h1>
            <a href="addcustomer.php" class="btn btn-outline-primary px-4 font-weight-bold" style="border-radius: 10px;">
                + Create User
            </a>
        </div>

        <form action="editcustomer.php" method="POST">
            <div class="glass-card">
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>Select</th>
                                <th>Client Details</th>
                                <th>Account No.</th>
                                <th>Current Balance</th>
                                <th>Status</th>
                                <th>Auth Pin</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                            $first = true;
                            while($rws = mysqli_fetch_assoc($result)): 
                                $statusClass = ($rws['verified'] == 1) ? 'badge-active' : 'badge-pending';
                                $statusText = ($rws['verified'] == 1) ? 'Active' : 'Pending';
                            ?>
                            <tr>
                                <td>
                                    <label class="custom-radio-container">
                                        <input type="radio" name="customer_id" value="<?php echo $rws['id']; ?>" <?php echo $first ? 'checked' : ''; ?>>
                                        <span class="checkmark"></span>
                                    </label>
                                </td>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <div class="avatar-circle mr-3 bg-light text-primary d-flex align-items-center justify-content-center" 
                                             style="width: 40px; height: 40px; border-radius: 10px; font-weight: 700;">
                                            <?php echo strtoupper(substr($rws['fullname'], 0, 1)); ?>
                                        </div>
                                        <div>
                                            <div class="font-weight-bold"><?php echo htmlspecialchars($rws['fullname']); ?></div>
                                            <div class="text-muted small"><?php echo $rws['email']; ?></div>
                                        </div>
                                    </div>
                                </td>
                                <td><span class="acc-number"><?php echo $rws['account_number']; ?></span></td>
                                <td>
                                    <div class="font-weight-bold">$<?php echo number_format($rws['balance'], 2); ?></div>
                                </td>
                                <td>
                                    <span class="status-badge <?php echo $statusClass; ?>">
                                        ● <?php echo $statusText; ?>
                                    </span>
                                </td>
                                <td>
                                    <span class="badge badge-light px-2 py-1 text-muted" style="border-radius: 4px;">
                                        <?php echo $rws['account_pin']; ?>
                                    </span>
                                </td>
                            </tr>
                            <?php 
                            $first = false;
                            endwhile; ?>
                        </tbody>
                    </table>
                </div>

                <div class="card-footer bg-white py-5 text-center border-0">
                    <button type="submit" name="submit_id" class="btn-edit-float">
                        <i class="mdi mdi-account-edit mr-2"></i> Modify Account Records
                    </button>
                    <p class="mt-3 text-muted small">Choose a user and click above to update credentials, balance, or security status.</p>
                </div>
            </div>
        </form>

    </div>
</div>

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