<?php
require __DIR__ . '/../layout/header.php';
require __DIR__ . '/../layout/sidebar.php';
?>

<div class="main">

    <div class="header">Laporan Keuangan</div>

    <!-- MODE -->
    <div class="card">
        <a href="/membership/laporan?mode=bulanan" class="btn btn-primary">Bulanan</a>
        <a href="/membership/laporan?mode=tahunan" class="btn btn-warning">Tahunan</a>
    </div>

    <!-- FILTER -->
    <div class="card" style="margin-top:20px;">
        <form method="GET" action="/membership/laporan">

            <input type="hidden" name="mode" value="<?= $mode ?>">

            <?php if ($mode == 'bulanan'): ?>
                <input type="month" name="bulan_tahun"
                    value="<?= $tahun ?>-<?= str_pad($bulan,2,'0',STR_PAD_LEFT) ?>">
            <?php else: ?>
                <input type="number" name="tahun" value="<?= $tahun ?>">
            <?php endif; ?>

            <button class="btn btn-primary">Filter</button>
        </form>
    </div>

    <!-- SUMMARY -->
    <div class="grid" style="margin-top:20px;">
        <div class="card"><h3>Iuran Masuk</h3><p>Rp <?= number_format($iuran_masuk) ?></p></div>
        <div class="card"><h3>Iuran Keluar</h3><p>Rp <?= number_format($iuran_keluar) ?></p></div>
        <div class="card"><h3>Saldo Iuran</h3><p>Rp <?= number_format($saldo_iuran) ?></p></div>
        <div class="card"><h3>Vendor Masuk</h3><p>Rp <?= number_format($vendor_masuk) ?></p></div>
        <div class="card"><h3>Vendor Keluar</h3><p>Rp <?= number_format($vendor_keluar) ?></p></div>
        <div class="card"><h3>Saldo Vendor</h3><p>Rp <?= number_format($saldo_vendor) ?></p></div>
    </div>

    <!-- ================= IURAN ================= -->
    <div class="card" style="margin-top:20px;">
        <h3>Laporan Iuran</h3>

        <a href="/membership/laporan-pdf-iuran?mode=<?= $mode ?>&bulan_tahun=<?= $tahun ?>-<?= $bulan ?>&tahun=<?= $tahun ?>" 
           target="_blank" class="btn btn-danger">
           Download PDF Iuran
        </a>

        <table class="table" style="margin-top:10px;">
            <thead>
                <tr>
                    <th>Tanggal</th>
                    <th>Jenis</th>
                    <th>Keterangan</th>
                    <th>Jumlah</th>
                </tr>
            </thead>
            <tbody>

            <?php if (empty($data_iuran)): ?>
                <tr><td colspan="4">Tidak ada data</td></tr>
            <?php else: ?>

                <?php foreach ($data_iuran as $row): ?>
                    <tr>
                        <td><?= $row['tanggal'] ?></td>
                        <td><?= $row['jenis'] ?></td>
                        <td><?= $row['keterangan'] ?? '-' ?></td>
                        <td style="color: <?= $row['jenis']=='Keluar'?'red':'green' ?>">
                            Rp <?= number_format($row['jumlah']) ?>
                        </td>
                    </tr>
                <?php endforeach; ?>

            <?php endif; ?>

            </tbody>
        </table>
    </div>

    <!-- ================= VENDOR ================= -->
    <div class="card" style="margin-top:20px;">
        <h3>Laporan Vendor</h3>

        <a href="/membership/laporan-pdf-vendor?mode=<?= $mode ?>&bulan_tahun=<?= $tahun ?>-<?= $bulan ?>&tahun=<?= $tahun ?>" 
           target="_blank" class="btn btn-danger">
           Download PDF Vendor
        </a>

        <table class="table" style="margin-top:10px;">
            <thead>
                <tr>
                    <th>Tanggal</th>
                    <th>Jenis</th>
                    <th>Keterangan</th>
                    <th>Jumlah</th>
                </tr>
            </thead>
            <tbody>

            <?php if (empty($data_vendor)): ?>
                <tr><td colspan="4">Tidak ada data</td></tr>
            <?php else: ?>

                <?php foreach ($data_vendor as $row): ?>
                    <tr>
                        <td><?= $row['tanggal'] ?></td>
                        <td><?= $row['jenis'] ?></td>
                        <td><?= $row['keterangan'] ?? '-' ?></td>
                        <td style="color: <?= $row['jenis']=='Keluar'?'red':'green' ?>">
                            Rp <?= number_format($row['jumlah']) ?>
                        </td>
                    </tr>
                <?php endforeach; ?>

            <?php endif; ?>

            </tbody>
        </table>
    </div>

</div>

<?php require __DIR__ . '/../layout/footer.php'; ?>