<?php
require_once __DIR__ . '/includes/functions.php';
requireLogin();
$pageTitle = 'Laporan per Kategori';

$dari = $_GET['dari'] ?? date('Y-m-01');
$sampai = $_GET['sampai'] ?? date('Y-m-d');

$stmt = $pdo->prepare("
    SELECT k.id, k.kode, k.nama, k.tipe, COALESCE(SUM(t.jumlah),0) as total, COUNT(t.id) as jumlah_transaksi
    FROM kategori k
    LEFT JOIN transaksi t ON t.kategori_id = k.id AND t.tanggal BETWEEN :dari AND :sampai
    GROUP BY k.id
    ORDER BY k.tipe, total DESC
");
$stmt->execute(['dari'=>$dari, 'sampai'=>$sampai]);
$data = $stmt->fetchAll();

$pemasukan = array_filter($data, fn($r) => $r['tipe']==='pemasukan');
$pengeluaran = array_filter($data, fn($r) => $r['tipe']==='pengeluaran');
$totalPemasukan = array_sum(array_column($pemasukan, 'total'));
$totalPengeluaran = array_sum(array_column($pengeluaran, 'total'));

include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';
?>

<div class="d-flex justify-content-between align-items-center mb-3 no-print">
    <h5 class="mb-0">Laporan per Kategori</h5>
    <button class="btn btn-outline-secondary" onclick="window.print()"><i class="bi bi-printer"></i> Cetak</button>
</div>

<div class="card mb-3 no-print">
    <div class="card-body">
        <form method="GET" class="row g-2">
            <div class="col-md-4"><input type="date" name="dari" value="<?= htmlspecialchars($dari) ?>" class="form-control"></div>
            <div class="col-md-4"><input type="date" name="sampai" value="<?= htmlspecialchars($sampai) ?>" class="form-control"></div>
            <div class="col-md-2"><button class="btn btn-primary w-100">Tampilkan</button></div>
        </form>
    </div>
</div>

<?= renderKopSurat(getPengaturan($pdo)) ?>
<div class="judul-laporan-resmi d-none d-print-block">
    <h5>LAPORAN PER KATEGORI</h5>
    <p>Periode <?= tanggalIndo($dari) ?> s/d <?= tanggalIndo($sampai) ?></p>
</div>

<div class="row g-3">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header bg-success-subtle">Pemasukan &mdash; Total: <?= rupiah($totalPemasukan) ?></div>
            <div class="card-body p-0">
                <table class="table mb-0">
                    <thead><tr><th>Kategori</th><th class="text-end">Jml Transaksi</th><th class="text-end">Total</th></tr></thead>
                    <tbody>
                    <?php foreach ($pemasukan as $row): ?>
                        <tr><td><?= htmlspecialchars($row['nama']) ?></td><td class="text-end"><?= $row['jumlah_transaksi'] ?></td><td class="text-end"><?= rupiah($row['total']) ?></td></tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card">
            <div class="card-header bg-danger-subtle">Pengeluaran &mdash; Total: <?= rupiah($totalPengeluaran) ?></div>
            <div class="card-body p-0">
                <table class="table mb-0">
                    <thead><tr><th>Kategori</th><th class="text-end">Jml Transaksi</th><th class="text-end">Total</th></tr></thead>
                    <tbody>
                    <?php foreach ($pengeluaran as $row): ?>
                        <tr><td><?= htmlspecialchars($row['nama']) ?></td><td class="text-end"><?= $row['jumlah_transaksi'] ?></td><td class="text-end"><?= rupiah($row['total']) ?></td></tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="card mt-3">
    <div class="card-body d-flex justify-content-between">
        <span class="fw-bold">Selisih (Pemasukan - Pengeluaran)</span>
        <span class="fw-bold <?= ($totalPemasukan-$totalPengeluaran) >= 0 ? 'text-success' : 'text-danger' ?>"><?= rupiah($totalPemasukan - $totalPengeluaran) ?></span>
    </div>
</div>

<?= renderTandaTanganLaporan(getPengaturan($pdo), $sampai) ?>

<?php include __DIR__ . '/includes/footer.php'; ?>