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

<div class="glass-card p-3 mb-4">
    <form class="row g-3 align-items-end" method="GET" action="<?= url('reports/top-services') ?>">
        <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_services?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
$totalRevenue = array_sum(array_column($services, 'total_revenue'));
$totalBookings = array_sum(array_column($services, 'total_count'));
?>

<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-cut"></i></div>
            <div class="stats-info">
                <span class="stats-label">Services</span>
                <span class="stats-value"><?= count($services) ?></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 Bookings</span>
                <span class="stats-value"><?= $totalBookings ?></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 Revenue</span>
                <span class="stats-value"><?= currency($totalRevenue) ?></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">Service Ranking</h6>
        </div>
        <div class="text-muted small">Ranked by total revenue</div>
    </div>
    <div class="table-responsive">
        <table class="table table-dark table-hover mb-0">
            <thead>
                <tr>
                    <th class="text-center">#</th>
                    <th>Service</th>
                    <th class="text-center">Total Bookings</th>
                    <th class="text-end">Total Revenue</th>
                    <th class="text-end">Avg. Price</th>
                    <th class="text-end">% Share</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($services)): ?>
                    <tr><td colspan="6" class="text-center text-muted py-4">No service data for this period</td></tr>
                <?php else: ?>
                    <?php $rank = 1; ?>
                    <?php foreach ($services as $s): ?>
                        <tr>
                            <td class="text-center"><?= $rank++ ?></td>
                            <td><?= e($s->name) ?></td>
                            <td class="text-center"><?= $s->total_count ?? 0 ?></td>
                            <td class="text-end"><?= currency($s->total_revenue ?? 0) ?></td>
                            <td class="text-end"><?= currency($s->avg_price ?? 0) ?></td>
                            <td class="text-end"><?= $totalRevenue > 0 ? number_format(($s->total_revenue / $totalRevenue) * 100, 1) . '%' : '0%' ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
