<?php
$monthNames = ['', 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'];
$paymentIcons = [
    'cash' => '<rect x="2" y="6" width="20" height="12" rx="2"></rect><circle cx="12" cy="12" r="2"></circle><path d="M6 12h.01"></path><path d="M18 12h.01"></path>',
    'debit' => '<rect x="1" y="4" width="22" height="16" rx="2" ry="2"></rect><line x1="1" y1="10" x2="23" y2="10"></line>',
    'qris' => '<rect x="3" y="3" width="7" height="7"></rect><rect x="14" y="3" width="7" height="7"></rect><rect x="3" y="14" width="7" height="7"></rect><path d="M14 14h3v3h-3zM19 14h2v2h-2zM14 19h2v2h-2zM19 19h2v2h-2z"></path>',
    'transfer' => '<polyline points="17 1 21 5 17 9"></polyline><path d="M3 11V9a4 4 0 0 1 4-4h14"></path><polyline points="7 23 3 19 7 15"></polyline><path d="M21 13v2a4 4 0 0 1-4 4H3"></path>',
];
$paymentColors = ['cash' => 'green', 'debit' => 'blue', 'qris' => 'violet', 'transfer' => 'amber'];
?>
<div class="hero">
    <div>
        <h2 style="margin:0 0 6px;">Laporan Bulanan</h2>
        <p style="margin:0; opacity:0.95;">Ringkasan penjualan per hari untuk bulan yang dipilih.</p>
    </div>
</div>

<div class="panel">
    <h3 style="margin-top:0;">Pilih Periode</h3>
    <form method="get" action="/owner/report-monthly" class="form-grid">
        <select name="month">
            <?php for ($m = 1; $m <= 12; $m++) : ?>
                <option value="<?= $m ?>" <?= $selectedMonth === $m ? 'selected' : '' ?>><?= esc($monthNames[$m]) ?></option>
            <?php endfor; ?>
        </select>
        <input type="number" name="year" min="2024" max="2100" value="<?= esc($selectedYear) ?>">
        <button class="btn-primary" type="submit">Tampilkan</button>
    </form>
</div>

<div class="grid">
    <div class="stat-card">
        <span class="stat-icon green"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="1" x2="12" y2="23"></line><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"></path></svg></span>
        <div>
            <h3>Total Penjualan <?= esc($monthNames[$selectedMonth]) ?> <?= esc($selectedYear) ?></h3>
            <div class="big">Rp <?= number_format($totalSales, 0, ',', '.') ?></div>
        </div>
    </div>
    <div class="stat-card">
        <span class="stat-icon violet"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 11l3 3L22 4"></path><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"></path></svg></span>
        <div>
            <h3>Total Transaksi</h3>
            <div class="big"><?= esc($totalTransactions) ?></div>
        </div>
    </div>
    <div class="stat-card">
        <span class="stat-icon amber"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"></polygon></svg></span>
        <div>
            <h3>Total Poin Ditukar</h3>
            <div class="big"><?= esc(number_format($totalPointsRedeemed, 0, ',', '.')) ?> poin</div>
        </div>
    </div>
</div>

<div class="panel">
    <span class="pill">Metode Pembayaran</span>
    <h3 style="margin:6px 0 14px;">Total penjualan bulan ini berdasarkan metode bayar</h3>
    <div class="grid" style="margin-bottom:0;">
        <?php foreach ($paymentBreakdown as $payment) : ?>
            <div class="stat-card">
                <span class="stat-icon <?= esc($paymentColors[$payment['method']] ?? 'green') ?>"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><?= $paymentIcons[$payment['method']] ?? '' ?></svg></span>
                <div>
                    <h3><?= esc($payment['label']) ?></h3>
                    <div class="big">Rp <?= number_format($payment['total'], 0, ',', '.') ?></div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<div class="panel">
    <h3>Rincian per Hari</h3>
    <div class="table-wrap">
        <table>
            <thead>
                <tr><th>Tanggal</th><th>Jumlah Transaksi</th><th>Total Penjualan</th></tr>
            </thead>
            <tbody>
                <?php foreach ($breakdown as $day) : ?>
                    <tr>
                        <td><?= esc($day['label']) ?></td>
                        <td><?= esc($day['count']) ?></td>
                        <td>Rp <?= number_format($day['total'], 0, ',', '.') ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
