<?php
/**
 * @var string $month
 * @var string $year
 * @var array $sales
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-calendar-alt me-2 text-gold"></i>Monthly Sales Report</h5>
</div>

<div class="glass-card p-3 mb-4">
    <form class="row g-3 align-items-end" method="GET" action="<?= url('reports/monthly-sales') ?>">
        <div class="col-md-3">
            <label class="form-label">Month</label>
            <select name="month" class="form-select">
                <?php for ($m = 1; $m <= 12; $m++): ?>
                    <option value="<?= str_pad($m, 2, '0', STR_PAD_LEFT) ?>" <?= $month === str_pad($m, 2, '0', STR_PAD_LEFT) ? 'selected' : '' ?>>
                        <?= date('F', mktime(0, 0, 0, $m, 1)) ?>
                    </option>
                <?php endfor; ?>
            </select>
        </div>
        <div class="col-md-3">
            <label class="form-label">Year</label>
            <select name="year" class="form-select">
                <?php for ($y = date('Y'); $y >= date('Y') - 5; $y--): ?>
                    <option value="<?= $y ?>" <?= $year == $y ? 'selected' : '' ?>><?= $y ?></option>
                <?php endfor; ?>
            </select>
        </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/monthly_sales?month=' . $month . '&year=' . $year) ?>" class="btn btn-outline-gold ms-2">
                <i class="fas fa-download me-1"></i>CSV
            </a>
        </div>
    </form>
</div>

<?php
$totalSales = array_sum(array_column($sales, 'total_sales'));
$totalInvoices = array_sum(array_column($sales, 'total_invoices'));
$netTotal = array_sum(array_column($sales, 'net_total'));
?>
<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-file-invoice"></i>
            </div>
            <div class="stats-info">
                <span class="stats-label">Total Invoices</span>
                <span class="stats-value"><?= $totalInvoices ?></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 Sales</span>
                <span class="stats-value"><?= currency($totalSales) ?></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-hand-holding-usd"></i>
            </div>
            <div class="stats-info">
                <span class="stats-label">Net Total</span>
                <span class="stats-value"><?= currency($netTotal) ?></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">Daily Breakdown</h6>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table table-dark table-hover mb-0">
            <thead>
                <tr>
                    <th>Date</th>
                    <th class="text-center">Invoices</th>
                    <th class="text-end">Total Sales</th>
                    <th class="text-end">Discounts</th>
                    <th class="text-end">Net Total</th>
                    <th>Payment Method</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($sales)): ?>
                    <tr><td colspan="6" class="text-center text-muted py-4">No data for this period</td></tr>
                <?php else: ?>
                    <?php foreach ($sales as $sale): ?>
                        <tr>
                            <td><?= formatDate($sale->date ?? '') ?></td>
                            <td class="text-center"><?= $sale->total_invoices ?? 0 ?></td>
                            <td class="text-end"><?= currency($sale->total_sales ?? 0) ?></td>
                            <td class="text-end"><?= currency($sale->total_discounts ?? 0) ?></td>
                            <td class="text-end"><?= currency($sale->net_total ?? 0) ?></td>
                            <td><?= e(ucwords(str_replace('_', ' ', $sale->payment_method ?? ''))) ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
