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

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

$stmt = $pdo->prepare("SELECT k.nama, SUM(t.jumlah) as total FROM transaksi t JOIN kategori k ON k.id=t.kategori_id WHERE t.tipe='pemasukan' AND t.tanggal BETWEEN :dari AND :sampai GROUP BY k.id ORDER BY total DESC");
$stmt->execute(['dari'=>$dari,'sampai'=>$sampai]);
$pemasukan = $stmt->fetchAll();
$totalPemasukan = array_sum(array_column($pemasukan, 'total'));

$stmt = $pdo->prepare("SELECT k.nama, SUM(t.jumlah) as total FROM transaksi t JOIN kategori k ON k.id=t.kategori_id WHERE t.tipe='pengeluaran' AND t.tanggal BETWEEN :dari AND :sampai GROUP BY k.id ORDER BY total DESC");
$stmt->execute(['dari'=>$dari,'sampai'=>$sampai]);
$pengeluaran = $stmt->fetchAll();
$totalPengeluaran = array_sum(array_column($pengeluaran, 'total'));

$selisih = $totalPemasukan - $totalPengeluaran;
$pengaturan = getPengaturan($pdo);

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 Laba Rugi Sederhana</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>

<div class="card">
    <div class="card-body">
        <div class="kop-surat d-none d-print-flex">
            <?php if (!empty($pengaturan['logo'])): ?><img src="assets/uploads/logo/<?= htmlspecialchars($pengaturan['logo']) ?>"><?php endif; ?>
            <div><h4><?= htmlspecialchars($pengaturan['nama_sekolah']) ?></h4><p><?= htmlspecialchars($pengaturan['alamat']) ?></p></div>
        </div>
        <h6 class="text-center mb-1">LAPORAN LABA RUGI</h6>
        <p class="text-center text-muted mb-4">Periode <?= tanggalIndo($dari) ?> s/d <?= tanggalIndo($sampai) ?></p>

        <h6 class="text-success">PEMASUKAN</h6>
        <table class="table table-sm">
            <?php foreach ($pemasukan as $row): ?>
                <tr><td><?= htmlspecialchars($row['nama']) ?></td><td class="text-end"><?= rupiah($row['total']) ?></td></tr>
            <?php endforeach; ?>
            <tr class="fw-bold border-top"><td>Total Pemasukan</td><td class="text-end"><?= rupiah($totalPemasukan) ?></td></tr>
        </table>

        <h6 class="text-danger mt-4">PENGELUARAN</h6>
        <table class="table table-sm">
            <?php foreach ($pengeluaran as $row): ?>
                <tr><td><?= htmlspecialchars($row['nama']) ?></td><td class="text-end"><?= rupiah($row['total']) ?></td></tr>
            <?php endforeach; ?>
            <tr class="fw-bold border-top"><td>Total Pengeluaran</td><td class="text-end"><?= rupiah($totalPengeluaran) ?></td></tr>
        </table>

        <table class="table table-sm mt-4">
            <tr class="fw-bold fs-5 <?= $selisih >= 0 ? 'table-success' : 'table-danger' ?>">
                <td><?= $selisih >= 0 ? 'SURPLUS (Laba)' : 'DEFISIT (Rugi)' ?></td>
                <td class="text-end"><?= rupiah(abs($selisih)) ?></td>
            </tr>
        </table>

        <div class="row mt-5 d-none d-print-flex">
            <div class="col-6 text-center">
                <p>Mengetahui,<br>Kepala Sekolah</p>
                <p class="mt-5 mb-0">(<?= htmlspecialchars($pengaturan['kepala_sekolah'] ?? '..........................') ?>)</p>
            </div>
            <div class="col-6 text-center">
                <p>Bekasi, <?= tanggalIndo(date('Y-m-d')) ?><br>Bendahara</p>
                <p class="mt-5 mb-0">(<?= htmlspecialchars($pengaturan['bendahara'] ?? '..........................') ?>)</p>
            </div>
        </div>
    </div>
</div>

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