<?php /** @var array $tickets */
/** @var int $total_revenue */
/** @var int $paid_count */ ?>
<?= $this->extend('admin/layout') ?>

<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="h3 mb-0">Bet Builder Tickets</h1>
        <p class="text-muted">Read-only view of user-built tickets and their fulfillment status.</p>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-4">
        <div class="card bg-primary text-white">
            <div class="card-body d-flex justify-content-between align-items-center">
                <div>
                    <h6 class="card-title mb-0">Total Tickets</h6>
                    <h3 class="mb-0"><?= count($tickets) ?></h3>
                </div>
                <i class="bi bi-ticket-perforated fs-1 opacity-50"></i>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card bg-success text-white">
            <div class="card-body d-flex justify-content-between align-items-center">
                <div>
                    <h6 class="card-title mb-0">Paid Tickets</h6>
                    <h3 class="mb-0"><?= $paid_count ?></h3>
                </div>
                <i class="bi bi-check-circle fs-1 opacity-50"></i>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card bg-info text-white">
            <div class="card-body d-flex justify-content-between align-items-center">
                <div>
                    <h6 class="card-title mb-0">Revenue (UGX)</h6>
                    <h3 class="mb-0"><?= number_format($total_revenue) ?></h3>
                </div>
                <i class="bi bi-cash-coin fs-1 opacity-50"></i>
            </div>
        </div>
    </div>
</div>

<div class="card shadow-sm">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="px-3 py-3">#</th>
                        <th class="px-3 py-3">Phone</th>
                        <th class="px-3 py-3">Legs</th>
                        <th class="px-3 py-3">Combined Odds</th>
                        <th class="px-3 py-3">Amount</th>
                        <th class="px-3 py-3">Status</th>
                        <th class="px-3 py-3">Transaction ID</th>
                        <th class="px-3 py-3">Created</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($tickets)): ?>
                        <tr>
                            <td colspan="8" class="text-center py-5 text-muted">
                                <i class="bi bi-inbox display-4 d-block mb-3"></i>
                                <h5>No tickets yet</h5>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($tickets as $t): ?>
                            <tr class="align-middle">
                                <td class="px-3 py-3"><?= $t['id'] ?></td>
                                <td class="px-3 py-3"><code><?= esc($t['msisdn']) ?></code></td>
                                <td class="px-3 py-3">
                                    <span class="badge bg-secondary"><?= (int) ($t['leg_count'] ?? 0) ?></span>
                                </td>
                                <td class="px-3 py-3">
                                    <span class="badge bg-primary fs-6"><?= number_format((float)($t['combined_odds'] ?? 1), 2) ?></span>
                                </td>
                                <td class="px-3 py-3">
                                    <strong><?= number_format((int)($t['amount'] ?? 0)) ?> UGX</strong>
                                </td>
                                <td class="px-3 py-3">
                                    <?php
                                        $status = $t['status'] ?? 'pending';
                                        $cls = 'secondary';
                                        if ($status === 'paid') $cls = 'success';
                                        elseif ($status === 'settled') $cls = 'info';
                                        elseif ($status === 'void') $cls = 'warning';
                                    ?>
                                    <span class="badge bg-<?= $cls ?>"><?= esc($status) ?></span>
                                </td>
                                <td class="px-3 py-3"><code class="small"><?= esc($t['transaction_id'] ?? '—') ?></code></td>
                                <td class="px-3 py-3">
                                    <small><?= !empty($t['created_at']) ? date('M j, Y H:i', strtotime($t['created_at'])) : '—' ?></small>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
