<?php
/**
 * @var string $startDate
 * @var string $endDate
 * @var array $customers
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-users me-2 text-gold"></i>Customer Report</h5>
</div>

<div class="glass-card p-3 mb-4">
    <form class="row g-3 align-items-end" method="GET" action="<?= url('reports/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/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
$totalCustomers = count($customers);
$totalVisits = array_sum(array_column($customers, 'total_visits'));
$totalSpent = array_sum(array_column($customers, 'total_spent'));
?>
<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(13,202,240,0.15);border-color:rgba(13,202,240,0.3);color:#0dcaf0;"><i class="fas fa-user-friends"></i></div>
            <div class="stats-info">
                <span class="stats-label">Active Customers</span>
                <span class="stats-value"><?= $totalCustomers ?></span>
            </div>
        </div>
    </div>
    <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-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 Details</h6>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table table-dark table-hover mb-0">
            <thead>
                <tr>
                    <th>Customer</th>
                    <th>Email</th>
                    <th>Phone</th>
                    <th class="text-center">Visits</th>
                    <th class="text-end">Total Spent</th>
                    <th>Last Visit</th>
                    <th>Registered</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 foreach ($customers as $c): ?>
                        <tr>
                            <td><?= e($c->first_name . ' ' . $c->last_name) ?></td>
                            <td><?= e($c->email ?? '-') ?></td>
                            <td><?= e($c->phone ?? '-') ?></td>
                            <td class="text-center"><?= $c->total_visits ?? 0 ?></td>
                            <td class="text-end"><?= currency($c->total_spent ?? 0) ?></td>
                            <td><?= $c->last_visit ? formatDate($c->last_visit) : '-' ?></td>
                            <td><?= formatDate($c->registration_date ?? '') ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
