<?php
require_once 'config.php';

// Filter default bulan & tahun ini
$bulan = isset($_GET['bulan']) ? (int)$_GET['bulan'] : (int)date('m');
$tahun = isset($_GET['tahun']) ? (int)$_GET['tahun'] : (int)date('Y');
$nama_bulan = ['','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember'];

// ===== PENDAPATAN =====
// 1. Pendapatan Air (dari buku_kas kategori Pembayaran Air)
$pendapatan_air = (float)$pdo->query("SELECT COALESCE(SUM(nominal),0) FROM buku_kas WHERE jenis='masuk' AND kategori='Pembayaran Air' AND MONTH(tanggal)=$bulan AND YEAR(tanggal)=$tahun")->fetchColumn();

// 2. Pendapatan Pasang Baru
$pendapatan_pasang = (float)$pdo->query("SELECT COALESCE(SUM(nominal),0) FROM buku_kas WHERE jenis='masuk' AND kategori='Pasang Baru' AND MONTH(tanggal)=$bulan AND YEAR(tanggal)=$tahun")->fetchColumn();

// 3. Pendapatan Lain
$pendapatan_lain = (float)$pdo->query("SELECT COALESCE(SUM(nominal),0) FROM buku_kas WHERE jenis='masuk' AND kategori NOT IN ('Pembayaran Air','Pasang Baru') AND MONTH(tanggal)=$bulan AND YEAR(tanggal)=$tahun")->fetchColumn();

$total_pendapatan = $pendapatan_air + $pendapatan_pasang + $pendapatan_lain;

// ===== PENGELUARAN =====
$beban_operasional = (float)$pdo->query("SELECT COALESCE(SUM(nominal),0) FROM buku_kas WHERE jenis='keluar' AND kategori='Operasional' AND MONTH(tanggal)=$bulan AND YEAR(tanggal)=$tahun")->fetchColumn();
$beban_honor = (float)$pdo->query("SELECT COALESCE(SUM(nominal),0) FROM buku_kas WHERE jenis='keluar' AND kategori='Honor Petugas' AND MONTH(tanggal)=$bulan AND YEAR(tanggal)=$tahun")->fetchColumn();
$beban_lain = (float)$pdo->query("SELECT COALESCE(SUM(nominal),0) FROM buku_kas WHERE jenis='keluar' AND kategori NOT IN ('Operasional','Honor Petugas') AND MONTH(tanggal)=$bulan AND YEAR(tanggal)=$tahun")->fetchColumn();

$total_pengeluaran = $beban_operasional + $beban_honor + $beban_lain;
$saldo_bersih = $total_pendapatan - $total_pengeluaran;

// Detail Transaksi untuk Laporan
$transaksi_masuk = $pdo->query("SELECT * FROM buku_kas WHERE jenis='masuk' AND MONTH(tanggal)=$bulan AND YEAR(tanggal)=$tahun ORDER BY tanggal ASC")->fetchAll(PDO::FETCH_ASSOC);
$transaksi_keluar = $pdo->query("SELECT * FROM buku_kas WHERE jenis='keluar' AND MONTH(tanggal)=$bulan AND YEAR(tanggal)=$tahun ORDER BY tanggal ASC")->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Laporan Keuangan - SPAM Tirtha Kedawung</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
:root {
--primary-sage: #5C8D89;
--dark-sage: #3A6B69;
--bg-color: #F4F7F6;
}
body { background: var(--bg-color); font-family: 'Segoe UI', sans-serif; color: #2f3440; margin:0; padding:0; }
.header-back { 
    background: white; 
    padding: 15px; 
    border-bottom: 1px solid #eee; 
    position: sticky; 
    top: 0; 
    z-index: 100;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}
.btn-back { 
    text-decoration: none; 
    color: var(--primary-sage); 
    font-weight: 700; 
    font-size: 0.95rem;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}
.header-title {
    text-align: center;
    margin-top: 10px;
}
.header-title h4 {
    margin: 0;
    color: var(--primary-sage);
    font-weight: 800;
}
.header-title small {
    color: #7a8190;
    font-size: 0.85rem;
}
.container-pub { padding: 0 15px 30px; max-width: 800px; margin: 0 auto; }
.card-soft {
    background: white; 
    border: none; 
    border-radius: 20px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.05); 
    padding: 20px; 
    margin-bottom: 15px;
}
.section-title { 
    font-size: 1.1rem; 
    font-weight: 800; 
    color: #354052; 
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    gap: 10px;
}
.laporan-row {
    display: flex; 
    justify-content: space-between; 
    align-items: center;
    padding: 10px 0; 
    border-bottom: 1px dashed #e0e0e0;
}
.laporan-row:last-child { border-bottom: none; }
.laporan-label { 
    color: #5a6270; 
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    gap: 8px;
}
.laporan-value { 
    font-weight: 700; 
    color: #2f3440; 
    font-size: 0.95rem;
}
.total-box {
    background: #f0f7ff; 
    border-radius: 12px; 
    padding: 15px;
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    margin-top: 10px;
}
.total-box.pengeluaran { background: #fff5f5; }
.total-box.saldo { 
    background: linear-gradient(135deg, var(--primary-sage) 0%, var(--dark-sage) 100%); 
    color: white;
}
.total-box.saldo .laporan-label { color: rgba(255,255,255,0.95); }
.total-box.saldo .laporan-value { 
    color: white; 
    font-size: 1.4rem;
    font-weight: 800;
}
.transaksi-item {
    display: flex; 
    justify-content: space-between; 
    align-items: center;
    padding: 10px 0; 
    border-bottom: 1px solid #f0f0f0;
}
.transaksi-item:last-child { border-bottom: none; }
.trans-info { flex: 1; }
.trans-ket { 
    font-weight: 600; 
    font-size: 0.85rem; 
    color: #2f3440;
}
.trans-meta { 
    font-size: 0.75rem; 
    color: #7a8190;
}
.trans-nominal { 
    font-weight: 800; 
    font-size: 0.9rem;
}
.text-masuk { color: #198754; }
.text-keluar { color: #dc3545; }
.form-select { 
    border-radius: 10px;
    border: 1px solid #d0d7de;
}
.btn-filter {
    background: var(--primary-sage);
    color: white;
    border: none;
    border-radius: 10px;
    padding: 8px 15px;
    font-weight: 600;
}
.badge-info {
    background: #e6f5f5;
    color: var(--primary-sage);
    padding: 8px 12px;
    border-radius: 10px;
    font-size: 0.85rem;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 15px;
}
footer { 
    text-align: center; 
    padding: 20px; 
    font-size: 0.75rem; 
    color: #8d94a2;
    background: white;
    border-top: 1px solid #f0f0f0;
    margin-top: 20px;
}
</style>
</head>
<body>

<div class="header-back">
    <div class="container" style="max-width: 800px;">
        <a href="index.php" class="btn-back">
            <i class="fa-solid fa-arrow-left"></i> Kembali ke Beranda
        </a>
        <div class="header-title">
            <h4><i class="fa-solid fa-chart-pie me-2"></i>Laporan Keuangan</h4>
            <small>SPAM Tirtha Kedawung Desa Gemuruh - Transparansi Publik</small>
        </div>
    </div>
</div>

<div class="container-pub mt-4">
    <div class="badge-info">
        <i class="fa-solid fa-circle-info"></i>
        Laporan ini disajikan untuk transparansi pengelolaan keuangan SPAM berbasis masyarakat
    </div>

    <!-- Filter Periode -->
    <form method="GET" class="card-soft">
        <div class="section-title"><i class="fa fa-filter"></i>Filter Periode</div>
        <div class="row g-2 align-items-end">
            <div class="col-6">
                <label class="form-label small text-muted mb-1">Bulan</label>
                <select name="bulan" class="form-select">
                    <?php foreach($nama_bulan as $i => $b): if($i==0) continue; ?>
                        <option value="<?= $i ?>" <?= ($i == $bulan) ? 'selected' : '' ?>><?= $b ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-6">
                <label class="form-label small text-muted mb-1">Tahun</label>
                <select name="tahun" class="form-select">
                    <?php for($y = date('Y'); $y >= date('Y')-3; $y--): ?>
                        <option value="<?= $y ?>" <?= ($y == $tahun) ? 'selected' : '' ?>><?= $y ?></option>
                    <?php endfor; ?>
                </select>
            </div>
        </div>
        <button type="submit" class="btn-filter w-100 mt-3">
            <i class="fa fa-sync-alt me-2"></i>Tampilkan Laporan
        </button>
    </form>

    <!-- Ringkasan Laporan -->
    <div class="card-soft">
        <div class="section-title"><i class="fa fa-chart-pie" style="color: var(--primary-sage);"></i>Ringkasan <?= $nama_bulan[$bulan] ?> <?= $tahun ?></div>
        
        <div class="fw-bold small text-muted mb-2 mt-2">PENDAPATAN</div>
        <div class="laporan-row">
            <span class="laporan-label"><i class="fa fa-droplet text-primary"></i> Pendapatan Air</span>
            <span class="laporan-value text-masuk"><?= format_rupiah($pendapatan_air) ?></span>
        </div>
        <div class="laporan-row">
            <span class="laporan-label"><i class="fa fa-plug text-warning"></i> Pasang Baru</span>
            <span class="laporan-value text-masuk"><?= format_rupiah($pendapatan_pasang) ?></span>
        </div>
        <div class="laporan-row">
            <span class="laporan-label"><i class="fa fa-coins text-secondary"></i> Pendapatan Lain</span>
            <span class="laporan-value text-masuk"><?= format_rupiah($pendapatan_lain) ?></span>
        </div>
        <div class="total-box">
            <span class="laporan-label fw-bold"><i class="fa fa-arrow-trend-up me-1"></i>Total Pendapatan</span>
            <span class="laporan-value text-m"><?= format_rupiah($total_pendapatan) ?></span>
        </div>

        <div class="fw-bold small text-muted mb-2 mt-4">PENGELUARAN</div>
        <div class="laporan-row">
            <span class="laporan-label"><i class="fa fa-store text-danger"></i> Operasional</span>
            <span class="laporan-value text-keluar"><?= format_rupiah($beban_operasional) ?></span>
        </div>
        <div class="laporan-row">
            <span class="laporan-label"><i class="fa fa-user-tie text-danger"></i> Honor Petugas</span>
            <span class="laporan-value text-keluar"><?= format_rupiah($beban_honor) ?></span>
        </div>
        <div class="laporan-row">
            <span class="laporan-label"><i class="fa fa-receipt text-danger"></i> Pengeluaran Lain</span>
            <span class="laporan-value text-keluar"><?= format_rupiah($beban_lain) ?></span>
        </div>
        <div class="total-box pengeluaran">
            <span class="laporan-label fw-bold"><i class="fa fa-arrow-trend-down me-1"></i>Total Pengeluaran</span>
            <span class="laporan-value text-keluar"><?= format_rupiah($total_pengeluaran) ?></span>
        </div>

        <div class="total-box saldo mt-4">
            <span class="laporan-label fw-bold"><i class="fa fa-wallet me-1"></i>Saldo Bersih</span>
            <span class="laporan-value"><?= format_rupiah($saldo_bersih) ?></span>
        </div>
    </div>

    <!-- Detail Transaksi Masuk -->
    <div class="card-soft">
        <div class="section-title"><i class="fa fa-arrow-down" style="color: #198754;"></i>Detail Pemasukan</div>
        <?php if (empty($transaksi_masuk)): ?>
            <div class="text-center text-muted py-3 small">Tidak ada pemasukan pada periode ini.</div>
        <?php else: ?>
            <?php foreach ($transaksi_masuk as $t): ?>
            <div class="transaksi-item">
                <div class="trans-info">
                    <div class="trans-ket"><?= htmlspecialchars($t['kategori']) ?></div>
                    <div class="trans-meta"><?= date('d/m/Y', strtotime($t['tanggal'])) ?> • <?= htmlspecialchars($t['keterangan']) ?></div>
                </div>
                <div class="trans-nominal text-masuk">+ <?= format_rupiah($t['nominal']) ?></div>
            </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <!-- Detail Transaksi Keluar -->
    <div class="card-soft">
        <div class="section-title"><i class="fa fa-arrow-up" style="color: #dc3545;"></i>Detail Pengeluaran</div>
        <?php if (empty($transaksi_keluar)): ?>
            <div class="text-center text-muted py-3 small">Tidak ada pengeluaran pada periode ini.</div>
        <?php else: ?>
            <?php foreach ($transaksi_keluar as $t): ?>
            <div class="transaksi-item">
                <div class="trans-info">
                    <div class="trans-ket"><?= htmlspecialchars($t['kategori']) ?></div>
                    <div class="trans-meta"><?= date('d/m/Y', strtotime($t['tanggal'])) ?> • <?= htmlspecialchars($t['keterangan']) ?></div>
                </div>
                <div class="trans-nominal text-keluar">- <?= format_rupiah($t['nominal']) ?></div>
            </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>

<footer>
    <div class="d-flex align-items-center justify-content-center gap-2 flex-wrap">
        <img src="assets/uploads/logo/tirtha-kedawung.png" 
             alt="Logo Tirtha Kedawung" 
             style="height:20px; width:auto; object-fit:contain;">
        <span>&copy; 2026 <b>SPAM Tirtha Kedawung</b> - Transparansi untuk Masyarakat</span>
    </div>
</footer>

</body>
</html>