<?= $this->extend('layout/main') ?>
<?= $this->section('content') ?>

<div class="row mb-3">
    <div class="col-md-4">
        <div class="info-box bg-emerald shadow">
            <span class="info-box-icon"><i class="fas fa-wallet"></i></span>
            <div class="info-box-content">
                <span class="info-box-text">Saldo Akhir Kas</span>
                <span class="info-box-number" style="font-size: 24px;">
                    Rp <?= number_format(end($kas)['saldo_akhir'] ?? 0, 0, ',', '.') ?>
                </span>
            </div>
        </div>
    </div>
</div>

<div class="card card-emerald card-outline shadow">
    <div class="card-header">
        <h3 class="card-title">Buku Kas Kelompok Tani</h3>
        <div class="card-tools">
            <button class="btn btn-emerald btn-sm" data-toggle="modal" data-target="#modalKas">
                <i class="fas fa-plus"></i> Input Transaksi
            </button>
            <a href="<?= base_url('keuangan/cetak') ?>" target="_blank" class="btn btn-default btn-sm">
                <i class="fas fa-print"></i> Cetak Laporan Resmi
            </a>
        </div>
    </div>
    <div class="card-body p-0">
        <table class="table table-bordered table-hover table-striped mb-0">
            <thead class="bg-light text-center">
                <tr>
                    <th width="10%">Tanggal</th>
                    <th width="35%">Uraian / Keterangan</th>
                    <th width="15%">Pemasukan (Debit)</th>
                    <th width="15%">Pengeluaran (Kredit)</th>
                    <th width="15%">Saldo Akhir</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($kas)): ?>
                    <tr>
                        <td colspan="5" class="text-center text-muted">Belum ada transaksi keuangan.</td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($kas as $k): ?>
                        <tr>
                            <td class="text-center"><?= date('d/m/Y', strtotime($k['tanggal'])) ?></td>
                            <td>
                                <?= $k['keterangan'] ?>

                                <?php if (!empty($k['nama_petani'])): ?>
                                    <br><small class="text-success">Petani: <b><?= $k['nama_petani'] ?></b></small>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?= $k['keterangan'] ?>
                                <?php if ($k['kategori'] == 'penebusan'): ?>
                                    <span class="badge badge-info float-right">Sistem</span>
                                <?php else: ?>
                                    <span class="badge badge-secondary float-right">Manual</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-right text-success font-weight-bold">
                                <?= $k['jenis'] == 'masuk' ? '+ Rp ' . number_format($k['nominal'], 0, ',', '.') : '-' ?>
                            </td>
                            <td class="text-right text-danger font-weight-bold">
                                <?= $k['jenis'] == 'keluar' ? '- Rp ' . number_format($k['nominal'], 0, ',', '.') : '-' ?>
                            </td>
                            <td class="text-right font-weight-bold bg-light">
                                Rp <?= number_format($k['saldo_akhir'], 0, ',', '.') ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="modal fade" id="modalKas" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-emerald text-white">
                <h5 class="modal-title"><i class="fas fa-edit mr-2"></i> Input Kas Manual</h5>
                <button type="button" class="close text-white" data-dismiss="modal"><span>&times;</span></button>
            </div>
            <form action="<?= base_url('keuangan/simpan_manual') ?>" method="post">
                <div class="modal-body">
                    <div class="form-group">
                        <label>Tanggal</label>
                        <input type="date" name="tanggal" class="form-control" value="<?= date('Y-m-d') ?>" required>
                    </div>
                    <div class="form-group">
                        <label>Jenis Transaksi</label>
                        <select name="jenis" class="form-control" required>
                            <option value="masuk">Pemasukan Kas (Uang Masuk)</option>
                            <option value="keluar">Pengeluaran Kas (Uang Keluar)</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Kategori</label>
                        <select name="kategori" class="form-control" required>
                            <option value="operasional">Biaya Operasional (Beli Kertas, Transport, dll)</option>
                            <option value="simpanan">Iuran / Simpanan Anggota</option>
                            <option value="lainnya">Lain-lain</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Keterangan Lengkap</label>
                        <input type="text" name="keterangan" class="form-control" placeholder="Contoh: Beli ATK untuk catatan RDKK" required>
                    </div>
                    <div class="form-group">
                        <label>Nominal (Rp)</label>
                        <input type="number" name="nominal" class="form-control" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-emerald btn-block">Simpan ke Buku Kas</button>
                </div>
            </form>
        </div>
    </div>
</div>

<style>
    /* CSS untuk menyembunyikan elemen saat diprint */
    @media print {

        .main-sidebar,
        .main-header,
        .card-tools,
        .info-box,
        .btn {
            display: none !important;
        }

        .content-wrapper {
            margin-left: 0 !important;
            background-color: white !important;
        }

        .card {
            border: none !important;
            box-shadow: none !important;
        }
    }
</style>
<?= $this->endSection() ?>