<?php

$pageName  = "All Virtual Cards";
include($_SERVER['DOCUMENT_ROOT'] . "/admin/layout/header.php");

?>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <section class="content-header">
        <h1>All Virtual Cards</h1>
        <ol class="breadcrumb">
            <li><a href="./dashboard.php"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        </ol>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-body">
                        <div class="table-responsive">
                            <table id="example2" class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th>S/N</th>
                                        <th>Name</th>
                                        <th>Card Name</th>
                                        <th>Card Type</th>
                                        <th>Card Level</th>
                                        <th>Number</th>
                                        <th>Exp</th>
                                        <th>Cvc</th>
                                        <th>Balance</th>
                                        <th>Limit</th>
                                        <th>Remaining</th>
                                        <th>Fee</th>
                                        <th>Status</th>
                                        <th>Date</th>
                                        <th class="text-center dt-no-sorting">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $sql = "SELECT 
                                                card.id AS card_id,
                                                card.seria_key,
                                                card.user_id,
                                                card.card_number,
                                                card.card_name,
                                                card.card_level,
                                                card.card_type,
                                                card.cardholder_name,
                                                card.billing_address,
                                                card.billing_city,
                                                card.billing_zipcode,
                                                card.billing_country,
                                                card.card_fee,
                                                card.card_balance,
                                                card.card_expiration,
                                                card.card_security,
                                                card.card_limit,
                                                card.card_limit_remain,
                                                card.card_status,
                                                card.createdAt,
                                                card.created_at,
                                                users.firstname,
                                                users.lastname,
                                                users.acct_currency AS user_currency
                                            FROM card 
                                            LEFT JOIN users ON card.user_id = users.id 
                                            ORDER BY card.id DESC";
                                    $stmt = $conn->prepare($sql);
                                    $stmt->execute();
                                    $sn = 1;
                                    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                                        $fullName = trim(($row['firstname'] ?? '') . ' ' . ($row['lastname'] ?? ''));
                                        $cur = $row['user_currency'] ?? '$';

                                        $statusRaw = (int)($row['card_status'] ?? 0);
                                        $statusLabels = [
                                            0 => '<span class="label label-warning">Pending</span>',
                                            1 => '<span class="label label-success">Active</span>',
                                            2 => '<span class="label label-warning">Processing</span>',
                                            3 => '<span class="label label-danger">On Hold</span>',
                                            4 => '<span class="label label-danger">Rejected</span>',
                                        ];
                                        $statusBadge = $statusLabels[$statusRaw] ?? $statusLabels[0];
                                    ?>
                                        <tr>
                                            <td><?= $sn++ ?></td>
                                            <td><?= htmlspecialchars($fullName ?: 'N/A') ?></td>
                                            <td><?= htmlspecialchars($row['card_name'] ?? '-') ?></td>
                                            <td><?= htmlspecialchars($row['card_type'] ?? '-') ?></td>
                                            <td><?= htmlspecialchars($row['card_level'] ?? '-') ?></td>
                                            <td><code><?= htmlspecialchars($row['card_number'] ?? '-') ?></code></td>
                                            <td><?= htmlspecialchars($row['card_expiration'] ?? '-') ?></td>
                                            <td><?= htmlspecialchars($row['card_security'] ?? '-') ?></td>
                                            <td><?= $cur . number_format((float)($row['card_balance'] ?? 0), 2) ?></td>
                                            <td><?= $cur . number_format((float)($row['card_limit'] ?? 0), 2) ?></td>
                                            <td><?= $cur . number_format((float)($row['card_limit_remain'] ?? 0), 2) ?></td>
                                            <td><?= $cur . number_format((float)($row['card_fee'] ?? 0), 2) ?></td>
                                            <td><?= $statusBadge ?></td>
                                            <td><?= htmlspecialchars($row['createdAt'] ?? $row['created_at'] ?? '-') ?></td>
                                            <td class="text-center">
                                                <a href="./view_cards.php?id=<?= htmlspecialchars($row['seria_key']) ?>" class="btn btn-primary btn-sm">View</a>
                                                <a href="./delete_cards.php?id=<?= htmlspecialchars($row['seria_key']) ?>" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure?');">Del</a>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<?php include($_SERVER['DOCUMENT_ROOT'] . "/admin/layout/footer.php"); ?>