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

<?= $this->section('content') ?>
<?php
    $rupiah = static fn ($nilai) => 'Rp ' . number_format((float) $nilai, 0, ',', '.');
    $filterQuery = http_build_query(array_filter($filters, static fn ($value) => $value !== ''));
    $laporanUrl = site_url('kas/laporan') . ($filterQuery !== '' ? '?' . $filterQuery : '');
    $jumlahTransaksi = count($transaksi);
    $periode = 'Semua periode';

    if ($filters['tanggal_mulai'] !== '' || $filters['tanggal_sampai'] !== '') {
        $mulai = $filters['tanggal_mulai'] !== '' ? date('d/m/Y', strtotime($filters['tanggal_mulai'])) : 'Awal';
        $sampai = $filters['tanggal_sampai'] !== '' ? date('d/m/Y', strtotime($filters['tanggal_sampai'])) : 'Akhir';
        $periode = $mulai . ' s/d ' . $sampai;
    }
?>

<?php if (session()->getFlashdata('success')) : ?>
    <div class="alert"><?= esc(session()->getFlashdata('success')) ?></div>
<?php endif ?>

<section class="dashboard-hero">
    <div>
        <p class="eyebrow">Dashboard</p>
        <h2>Ringkasan Buku Kas Umum</h2>
        <p>Periode: <?= esc($periode) ?><?= $filters['jenis'] !== '' ? ' | ' . esc(ucfirst($filters['jenis'])) : '' ?></p>
    </div>
    <div class="hero-actions">
        <a class="button button-secondary" href="<?= esc($laporanUrl) ?>">Buka Laporan</a>
        <a class="button" href="<?= site_url('kas/create') ?>">Tambah Transaksi</a>
    </div>
</section>

<section class="summary-grid">
    <article class="summary-card income">
        <div class="summary-icon">KM</div>
        <span>Kas Masuk</span>
        <strong><?= $rupiah($ringkasan['masuk']) ?></strong>
        <small>Total penerimaan yang tampil</small>
    </article>
    <article class="summary-card expense">
        <div class="summary-icon">KK</div>
        <span>Kas Keluar</span>
        <strong><?= $rupiah($ringkasan['keluar']) ?></strong>
        <small>Total pengeluaran yang tampil</small>
    </article>
    <article class="summary-card balance">
        <div class="summary-icon">SA</div>
        <span>Saldo Akhir</span>
        <strong><?= $rupiah($ringkasan['saldo']) ?></strong>
        <small><?= $jumlahTransaksi ?> transaksi dalam daftar</small>
    </article>
</section>

<section class="panel">
    <div class="panel-head">
        <div>
            <p class="eyebrow">Pencatatan Harian</p>
            <h2>Daftar Transaksi</h2>
            <p class="panel-subtitle"><?= $jumlahTransaksi ?> transaksi ditampilkan</p>
        </div>
        <div class="panel-actions">
            <a class="button button-secondary" href="<?= esc($laporanUrl) ?>">Laporan</a>
            <a class="button" href="<?= site_url('kas/create') ?>">Tambah Baru</a>
        </div>
    </div>

    <form action="<?= site_url('kas') ?>" method="get" class="filter-form">
        <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 Filter</button>
            <?php if ($hasFilter) : ?>
                <a class="button button-secondary" href="<?= site_url('kas') ?>">Reset</a>
            <?php endif ?>
        </div>
    </form>

    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Tanggal</th>
                    <th>Kode</th>
                    <th>Uraian</th>
                    <th>Jenis</th>
                    <th class="numeric">Jumlah</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($transaksi === []) : ?>
                    <tr>
                        <td colspan="6" class="empty">
                            <?= $hasFilter ? 'Tidak ada transaksi yang sesuai dengan filter.' : 'Belum ada transaksi. Tambahkan catatan kas pertama.' ?>
                        </td>
                    </tr>
                <?php endif ?>

                <?php foreach ($transaksi as $row) : ?>
                    <tr>
                        <td><?= esc(date('d/m/Y', strtotime($row['tanggal']))) ?></td>
                        <td><?= esc($row['kode']) ?></td>
                        <td><?= esc($row['uraian']) ?></td>
                        <td><span class="badge <?= esc($row['jenis']) ?>"><?= esc(ucfirst($row['jenis'])) ?></span></td>
                        <td class="numeric"><?= $rupiah($row['jumlah']) ?></td>
                        <td class="actions">
                            <a href="<?= site_url('kas/' . $row['id'] . '/edit') ?>">Edit</a>
                            <form action="<?= site_url('kas/' . $row['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Hapus transaksi ini?')">
                                <button type="submit">Hapus</button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach ?>
            </tbody>
        </table>
    </div>
</section>
<?= $this->endSection() ?>
