<?= $this->extend('tipster/layout') ?>

<?= $this->section('content') ?>
<div class="container-fluid">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">Dashboard</h1>
        <div class="btn-toolbar mb-2 mb-md-0">
            <a href="<?= site_url('tipster/add-tip') ?>" class="btn btn-primary">
                <i class="bi bi-plus-circle me-2"></i>Add New Tip
            </a>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-2">Total Tips</h6>
                            <h3 class="mb-0"><?= $totalTips ?></h3>
                        </div>
                        <div class="text-primary" style="font-size: 2.5rem;">
                            <i class="bi bi-list-ul"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-2">Recent Tips (7 days)</h6>
                            <h3 class="mb-0"><?= $recentTips ?></h3>
                        </div>
                        <div class="text-success" style="font-size: 2.5rem;">
                            <i class="bi bi-calendar-check"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-2">Success Rate</h6>
                            <h3 class="mb-0"><?= isset($tipster['success_rate']) ? number_format($tipster['success_rate'], 1) : '0.0' ?>%</h3>
                        </div>
                        <div class="text-info" style="font-size: 2.5rem;">
                            <i class="bi bi-graph-up-arrow"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-speedometer2 me-2"></i>Quick Actions</h5>
                </div>
                <div class="card-body">
                    <div class="row g-3">
                        <div class="col-md-6 col-lg-3">
                            <a href="<?= site_url('tipster/add-tip') ?>" class="btn btn-primary w-100">
                                <i class="bi bi-plus-circle me-2"></i>Add New Tip
                            </a>
                        </div>
                        <div class="col-md-6 col-lg-3">
                            <a href="<?= site_url('tipster/my-tips') ?>" class="btn btn-outline-primary w-100">
                                <i class="bi bi-list-ul me-2"></i>View My Tips
                            </a>
                        </div>
                        <div class="col-md-6 col-lg-3">
                            <a href="<?= site_url('tipster/sales') ?>" class="btn btn-outline-success w-100">
                                <i class="bi bi-cash-stack me-2"></i>Sales & Earnings
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="bi bi-clipboard-data me-2"></i>My Latest Tickets</h5>
                    <span class="text-muted small">Last 5 tickets</span>
                </div>
                <div class="card-body">
                    <?php if (empty($latestTickets)): ?>
                        <div class="text-center text-muted py-4">
                            No tickets available yet.
                        </div>
                    <?php else: ?>
                        <div class="accordion" id="tipsterLatestTicketsAccordion">
                            <?php foreach ($latestTickets as $index => $ticket): ?>
                                <?php
                                    $headingId = 'tipsterTicketHeading' . $index;
                                    $collapseId = 'tipsterTicketCollapse' . $index;
                                    $title = $ticket['title'] ? $ticket['title'] : 'Tip #' . $ticket['tip_id'];
                                    $successPct = $ticket['success_pct'] !== null ? $ticket['success_pct'] . '%' : '-';
                                ?>
                                <div class="accordion-item">
                                    <h2 class="accordion-header" id="<?= $headingId ?>">
                                        <button class="accordion-button collapsed" type="button"
                                            data-bs-toggle="collapse"
                                            data-bs-target="#<?= $collapseId ?>"
                                            aria-expanded="false"
                                            aria-controls="<?= $collapseId ?>">
                                            <span class="me-2"><?= esc($title) ?></span>
                                            <span class="badge bg-secondary me-2"><?= (int)$ticket['total_picks'] ?> picks</span>
                                            <span class="badge bg-success me-1">W <?= (int)$ticket['correct'] ?></span>
                                            <span class="badge bg-danger me-1">L <?= (int)$ticket['wrong'] ?></span>
                                            <span class="badge bg-warning text-dark me-2">TBD <?= (int)$ticket['pending'] ?></span>
                                            <span class="badge bg-info text-dark"><?= $successPct ?></span>
                                        </button>
                                    </h2>
                                    <div id="<?= $collapseId ?>" class="accordion-collapse collapse"
                                        aria-labelledby="<?= $headingId ?>"
                                        data-bs-parent="#tipsterLatestTicketsAccordion">
                                        <div class="accordion-body">
                                            <div class="table-responsive">
                                                <table class="table table-sm table-bordered">
                                                    <thead class="table-light">
                                                        <tr>
                                                            <th>Match</th>
                                                            <th>Prediction</th>
                                                            <th>Odds</th>
                                                            <th>Outcome</th>
                                                            <th>Date</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php foreach ($ticket['picks'] as $pick): ?>
                                                            <?php $match = $pick['match_data'] ?? []; ?>
                                                            <tr>
                                                                <td><?= esc(($match['team1'] ?? 'N/A') . ' vs ' . ($match['team2'] ?? 'N/A')) ?></td>
                                                                <td><?= esc($match['prediction'] ?? 'N/A') ?></td>
                                                                <td><?= esc($match['odds'] ?? '-') ?></td>
                                                                <td>
                                                                    <?php
                                                                        $outcome = $pick['outcome'] ?? 'pending';
                                                                        $badge =
                                                                            $outcome === 'correct'
                                                                                ? 'success'
                                                                                : ($outcome === 'wrong' ? 'danger' : 'warning');
                                                                    ?>
                                                                    <span class="badge bg-<?= $badge ?>">
                                                                        <?= ucfirst($outcome) ?>
                                                                    </span>
                                                                </td>
                                                                <td><?= esc($pick['match_date'] ?? '-') ?></td>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>

