<?php 
session_start();
// Enable error reporting to stop blank pages
error_reporting(E_ALL);
ini_set('display_errors', 1);

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

include '_inc/dbconn.php';

// Fetch users - Verify if your column is 'mobile' or 'phone' in the database
$sql = "SELECT * FROM users ORDER BY id DESC";
$result = mysqli_query($con, $sql) or die(mysqli_error($con));

// Fetch the most recent ID for default selection
$sql_min = "SELECT id FROM users ORDER BY id DESC LIMIT 1";
$result_min = mysqli_query($con, $sql_min);
$rws_min = mysqli_fetch_assoc($result_min);

include 'admin_header.php'; 
?>

<style>
    :root {
        --danger: #EF4444;
        --bg: #F8FAFC;
    }
    .main-content {
        margin-left: 260px;
        transition: 0.3s;
        background: var(--bg);
        min-height: 100vh;
        padding: 30px;
    }
    @media (max-width: 992px) { .main-content { margin-left: 0 !important; padding: 15px; } }

    .glass-card {
        background: #fff;
        border-radius: 16px;
        border: 1px solid #E2E8F0;
        box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05);
    }
    .table thead th { background: #F1F5F9; color: #64748B; font-size: 11px; text-transform: uppercase; border: none; padding: 15px; }
    .table td { padding: 15px; vertical-align: middle; border-top: 1px solid #F1F5F9; }
    
    .btn-delete {
        background: var(--danger);
        color: white;
        border: none;
        padding: 14px 30px;
        border-radius: 10px;
        font-weight: 700;
        cursor: pointer;
        width: 100%;
        max-width: 300px;
        transition: 0.2s;
    }
    .btn-delete:hover { background: #B91C1C; transform: scale(1.02); }
</style>

<div class="main-content">
    <?php include 'admin_navbar.php'?>

    <div class="container-fluid mt-4">
        <div class="mb-4">
            <h2 class="font-weight-bold" style="color: #1E293B;">Delete Customer Account</h2>
            <p class="text-muted">Select a user carefully. Deleting an account removes all transaction history.</p>
        </div>

        <form action="delete_customer_logic.php" method="POST" onsubmit="return confirm('CRITICAL: Are you sure? This will permanently erase this user.');">
            <div class="glass-card">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th class="text-center">Select</th>
                                <th>Name</th>
                                <th>Account No</th>
                                <th>Email</th>
                                <th>Balance</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                            if(mysqli_num_rows($result) > 0) {
                                while($rws = mysqli_fetch_assoc($result)): 
                                    // Safety: Fallback if 'mobile' is actually 'phone' in your DB
                                    $phone_display = $rws['mobile'] ?? $rws['phone'] ?? 'N/A';
                                ?>
                                <tr>
                                    <td class="text-center">
                                        <input type="radio" name="customer_id" value="<?php echo $rws['id']; ?>" 
                                        <?php if(isset($rws_min['id']) && $rws['id'] == $rws_min['id']) echo 'checked'; ?>>
                                    </td>
                                    <td>
                                        <div class="font-weight-bold text-dark"><?php echo htmlspecialchars($rws['fullname']); ?></div>
                                        <small class="text-muted"><?php echo $phone_display; ?></small>
                                    </td>
                                    <td><code class="text-primary font-weight-bold"><?php echo $rws['account_number']; ?></code></td>
                                    <td><?php echo $rws['email']; ?></td>
                                    <td class="font-weight-bold text-dark">$<?php echo number_format($rws['balance'], 2); ?></td>
                                </tr>
                                <?php endwhile; 
                            } else {
                                echo "<tr><td colspan='5' class='text-center py-5'>No users found.</td></tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>

                <div class="card-footer bg-white border-0 py-5 text-center">
                    <button type="submit" name="submit2_id" class="btn-delete">
                        <i class="mdi mdi-trash-can-outline mr-2"></i> Confirm Permanent Deletion
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<?php include 'footer.php'?>
