<?php
/**
 * @var string $startDate
 * @var string $endDate
 * @var array $customers
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-trophy me-2 text-gold"></i>Top Customers by Spending</h5>
</div>

<div class="glass-card p-3 mb-4">
    <form class="row g-3 align-items-end" method="GET" action="<?= url('reports/top-customers') ?>">
        <div class="col-md-3">
            <label class="form-label">Start Date</label>
            <input type="date" name="start_date" class="form-control" value="<?= e($startDate) ?>">
        </div>
        <div class="col-md-3">
            <label class="form-label">End Date</label>
            <input type="date" name="end_date" class="form-control" value="<?= e($endDate) ?>">
        </div>
        <div class="col-md-4">
            <button type="submit" class="btn btn-gold"><i class="fas fa-search me-1"></i>Generate</button>
            <a href="<?= url('reports/export/top_customers?start_date=' . $startDate . '&end_date=' . $endDate) ?>" class="btn btn-outline-gold ms-2">
                <i class="fas fa-download me-1"></i>CSV
            </a>
        </div>
    </form>
</div>

<?php
$totalSpent = array_sum(array_column($customers, 'total_spent'));
$totalVisits = array_sum(array_column($customers, 'total_visits'));
?>
<div class="row g-4 mb-4">
    <div class="col-md-4">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(200,164,78,0.15);border-color:rgba(200,164,78,0.3);color:var(--gold-primary);"><i class="fas fa-users"></i></div>
            <div class="stats-info">
                <span class="stats-label">Top Customers</span>
                <span class="stats-value"><?= count($customers) ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(13,202,240,0.15);border-color:rgba(13,202,240,0.3);color:#0dcaf0;"><i class="fas fa-calendar-check"></i></div>
            <div class="stats-info">
                <span class="stats-label">Total Visits</span>
                <span class="stats-value"><?= $totalVisits ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(25,135,84,0.15);border-color:rgba(25,135,84,0.3);color:#198754;"><i class="fas fa-dollar-sign"></i></div>
            <div class="stats-info">
                <span class="stats-label">Total Spent</span>
                <span class="stats-value"><?= currency($totalSpent) ?></span>
            </div>
        </div>
    </div>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">Customer Ranking</h6>
        </div>
        <div class="text-muted small">Ranked by total spending</div>
    </div>
    <div class="table-responsive">
        <table class="table table-dark table-hover mb-0">
            <thead>
                <tr>
                    <th class="text-center">#</th>
                    <th>Customer</th>
                    <th>Email</th>
                    <th class="text-center">Visits</th>
                    <th class="text-end">Total Spent</th>
                    <th class="text-end">Avg/Visit</th>
                    <th>Last Visit</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($customers)): ?>
                    <tr><td colspan="7" class="text-center text-muted py-4">No customer data for this period</td></tr>
                <?php else: ?>
                    <?php $rank = 1; ?>
                    <?php foreach ($customers as $c): ?>
                        <tr>
                            <td class="text-center"><?= $rank++ ?></td>
                            <td><?= e($c->first_name . ' ' . $c->last_name) ?></td>
                            <td><?= e($c->email ?? '-') ?></td>
                            <td class="text-center"><?= $c->total_visits ?? 0 ?></td>
                            <td class="text-end"><?= currency($c->total_spent ?? 0) ?></td>
                            <td class="text-end"><?= currency($c->avg_per_visit ?? 0) ?></td>
                            <td><?= $c->last_visit ? formatDate($c->last_visit) : '-' ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
