<?= $this->extend('layouts/main') ?>

<?= $this->section('content') ?>
<?php
    $rupiah = static fn ($nilai) => 'Rp ' . number_format((float) $nilai, 0, ',', '.');
    $jenis = $filters['jenis'] !== '' ? ucfirst($filters['jenis']) : 'Semua Jenis';
    $jumlahTransaksi = count($transaksi);
    $periode = 'Semua Periode';

    if ($filters['tanggal_mulai'] !== '' && $filters['tanggal_sampai'] !== '') {
        $periode = date('d/m/Y', strtotime($filters['tanggal_mulai'])) . ' s/d ' . date('d/m/Y', strtotime($filters['tanggal_sampai']));
    } elseif ($filters['tanggal_mulai'] !== '') {
        $periode = 'Mulai ' . date('d/m/Y', strtotime($filters['tanggal_mulai']));
    } elseif ($filters['tanggal_sampai'] !== '') {
        $periode = 'Sampai ' . date('d/m/Y', strtotime($filters['tanggal_sampai']));
    }
?>

<section class="panel report-panel">
    <div class="panel-head no-print">
        <div>
            <p class="eyebrow">Laporan</p>
            <h2>Buku Kas Umum</h2>
        </div>
        <div class="panel-actions">
            <button class="button" type="button" onclick="window.print()">Cetak / Simpan PDF</button>
            <a class="button button-secondary" href="<?= site_url('kas') ?>">Kembali</a>
        </div>
    </div>

    <form action="<?= site_url('kas/laporan') ?>" method="get" class="filter-form no-print">
        <label>
            <span>Cari Kode atau Uraian</span>
            <input type="search" name="q" value="<?= esc($filters['q']) ?>" placeholder="Contoh: ATK atau BKM-001">
        </label>

        <label>
            <span>Tanggal Mulai</span>
            <input type="date" name="tanggal_mulai" value="<?= esc($filters['tanggal_mulai']) ?>">
        </label>

        <label>
            <span>Tanggal Sampai</span>
            <input type="date" name="tanggal_sampai" value="<?= esc($filters['tanggal_sampai']) ?>">
        </label>

        <label>
            <span>Jenis</span>
            <select name="jenis">
                <option value="">Semua Jenis</option>
                <option value="masuk" <?= $filters['jenis'] === 'masuk' ? 'selected' : '' ?>>Kas Masuk</option>
                <option value="keluar" <?= $filters['jenis'] === 'keluar' ? 'selected' : '' ?>>Kas Keluar</option>
            </select>
        </label>

        <div class="filter-actions">
            <button class="button" type="submit">Terapkan</button>
            <?php if ($hasFilter) : ?>
                <a class="button button-secondary" href="<?= site_url('kas/laporan') ?>">Reset</a>
            <?php endif ?>
        </div>
    </form>

    <div class="report-body">
        <div class="report-title">
            <h2>Laporan Buku Kas Umum</h2>
            <div class="report-meta">
                <span>Periode: <?= esc($periode) ?></span>
                <span>Jenis: <?= esc($jenis) ?></span>
                <span>Transaksi: <?= $jumlahTransaksi ?></span>
            </div>
            <?php if ($filters['q'] !== '') : ?>
                <p>Pencarian: <?= esc($filters['q']) ?></p>
            <?php endif ?>
        </div>

        <div class="report-summary">
            <div>
                <span>Kas Masuk</span>
                <strong><?= $rupiah($ringkasan['masuk']) ?></strong>
            </div>
            <div>
                <span>Kas Keluar</span>
                <strong><?= $rupiah($ringkasan['keluar']) ?></strong>
            </div>
            <div>
                <span>Saldo Akhir</span>
                <strong><?= $rupiah($ringkasan['saldo']) ?></strong>
            </div>
        </div>

        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Tanggal</th>
                        <th>Kode</th>
                        <th>Uraian</th>
                        <th>Jenis</th>
                        <th class="numeric">Jumlah</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($transaksi === []) : ?>
                        <tr>
                            <td colspan="6" class="empty">Tidak ada transaksi untuk laporan ini.</td>
                        </tr>
                    <?php endif ?>

                    <?php foreach ($transaksi as $index => $row) : ?>
                        <tr>
                            <td><?= $index + 1 ?></td>
                            <td><?= esc(date('d/m/Y', strtotime($row['tanggal']))) ?></td>
                            <td><?= esc($row['kode']) ?></td>
                            <td><?= esc($row['uraian']) ?></td>
                            <td><?= esc(ucfirst($row['jenis'])) ?></td>
                            <td class="numeric"><?= $rupiah($row['jumlah']) ?></td>
                        </tr>
                    <?php endforeach ?>
                </tbody>
            </table>
        <style>
    .tabel-tanda-tangan {
        width: 100%;
        /* Menghilangkan semua garis */
        border-collapse: collapse;
        border: none; 
        margin-top: 20px;
        font-family: Arial, sans-serif;
    }
    
    .tabel-tanda-tangan td {
        width: 50%;
        text-align: center;
        vertical-align: top;
        padding: 10px;
        /* Memastikan tidak ada garis di sel */
        border: none; 
    }

    .spacer {
        height: 80px; /* Ruang kosong untuk tanda tangan */
    }

    .nama-ttd {
        text-transform: uppercase;
        font-weight: bold;
        text-decoration: none;
    }
</style>

<table class="tabel-tanda-tangan">
    <tr>
        <td>
            <p>Bendahara</p>
            <div class="spacer"></div>
            <p class="nama-ttd">( ADLHIYA NUR SABILA ROSYADA )</p>
        </td>
        
        <td>
            <p><?= esc(date('d/m/Y')) ?></p>
            <p>Penerima</p>
            <div class="spacer"></div>
            <p class="nama-ttd">( ................................ )</p>
        </td>
    </tr>
    <tr>
        <td colspan="2" style="padding-top: 30px;">
            <p>Mengetahui,</p>
            <p>Kepala Madrasah</p>
            <div class="spacer"></div>
            <p class="nama-ttd">( SAHRONI SIDIQ, S.Pd. )</p>
        </td>
    </tr>
</table>
</section>
<?= $this->endSection() ?>
