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

<?= $this->section('content') ?>

<!-- KOTAK WIDGET INFORMASI UTAMA (PUPUK TERLARIS) -->
<div class="row">
    <div class="col-md-12">
        <div class="small-box bg-gradient-warning shadow-sm border border-warning text-dark">
            <div class="inner p-4">
                <h5 class="text-uppercase font-weight-bold tracking-wide"><i class="fas fa-crown text-amber mr-2"></i> Produk Pupuk Paling Banyak Beli</h5>
                <?php if ($terlaris['jumlah'] > 0): ?>
                    <h2 class="font-weight-black mb-1"><?= $terlaris['nama_pupuk'] ?></h2>
                    <p class="mb-0 text-md">Total ditebus petani sebesar <b><?= number_format($terlaris['jumlah'], 0, ',', '.') ?> Kg</b> pada periode terpilih.</p>
                <?php else: ?>
                    <h3 class="mb-1 text-muted font-italic">Belum Ada Transaksi</h3>
                    <p class="mb-0">Tidak ditemukan aktivitas penebusan pupuk pada rentang tanggal filter.</p>
                <?php endif; ?>
            </div>
            <div class="icon" style="color: rgba(0,0,0,0.08);">
                <i class="fas fa-award"></i>
            </div>
        </div>
    </div>
</div>

<!-- PANEL ALAT FILTER PENCARIAN (HARIAN / BULANAN / TAHUNAN) -->
<div class="card card-emerald card-outline shadow-sm mb-4">
    <div class="card-header bg-light">
        <h3 class="card-title text-bold"><i class="fas fa-filter mr-1 text-emerald"></i> Panel Filter Pembukuan Laporan</h3>
    </div>
    <div class="card-body">
        <form action="<?= base_url('transaksi/rekap_pupuk') ?>" method="get" id="formFilterLaporan">
            <div class="row align-items-end">
                <!-- Dropdown Model Tipe Filter -->
                <div class="col-md-3 form-group">
                    <label>Pilih Model Laporan</label>
                    <select name="filter_type" id="filter_type" class="form-control font-weight-bold text-emerald">
                        <option value="harian" <?= $filter_type == 'harian' ? 'selected' : '' ?>>Laporan Harian (Per Tanggal)</option>
                        <option value="bulanan" <?= $filter_type == 'bulanan' ? 'selected' : '' ?>>Laporan Bulanan (Per Bulan)</option>
                        <option value="tahunan" <?= $filter_type == 'tahunan' ? 'selected' : '' ?>>Laporan Tahunan (Per Tahun)</option>
                    </select>
                </div>

                <!-- Input Inputan Tanggal (Khusus Harian) -->
                <div class="col-md-4 form-group zona-filter d-none" id="zona_harian">
                    <label>Pilih Tanggal</label>
                    <input type="date" name="tanggal" class="form-control" value="<?= $tanggal ?>">
                </div>

                <!-- Input Pilihan Bulan (Khusus Bulanan) -->
                <div class="col-md-3 form-group zona-filter d-none" id="zona_bulanan">
                    <label>Pilih Bulan</label>
                    <select name="bulan" class="form-control">
                        <?php 
                        $namaBulan = [
                            '01'=>'Januari','02'=>'Februari','03'=>'Maret','04'=>'April','05'=>'Mei','06'=>'Juni',
                            '07'=>'Juli','08'=>'Agustus','09'=>'September','10'=>'Oktober','11'=>'November','12'=>'Desember'
                        ];
                        foreach($namaBulan as $key => $val): 
                        ?>
                            <option value="<?= $key ?>" <?= $bulan == $key ? 'selected' : '' ?>><?= $val ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <!-- Input Pilihan Tahun (Bulanan & Tahunan) -->
                <div class="col-md-2 form-group zona-filter d-none" id="zona_tahun">
                    <label>Tahun</label>
                    <select name="tahun" class="form-control">
                        <?php for($t = date('Y'); $t >= date('Y') - 5; $t--): ?>
                            <option value="<?= $t ?>" <?= $tahun == $t ? 'selected' : '' ?>><?= $t ?></option>
                        <?php endfor; ?> <!-- PERBAIKAN: Sekarang menggunakan endfor untuk menutup loop 'for' -->
                    </select>
                </div>

                <!-- Tombol Eksekusi Cek Data -->
                <div class="col-md-3 form-group">
                    <button type="submit" class="btn btn-emerald btn-block text-bold shadow-sm">
                        <i class="fas fa-sync-alt mr-1"></i> Buka Rekap Laporan
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- TABEL UTAMA MUTASI KELUAR MASUK STOK PUPUK -->
<div class="card card-emerald card-outline shadow-sm">
    <div class="card-header">
        <h3 class="card-title text-bold py-1">
            <i class="fas fa-chart-bar text-emerald mr-1"></i> Data Mutasi Perputaran Komoditas Pupuk
        </h3>
    </div>
    <div class="card-body p-0">
        <table class="table table-hover table-striped mb-0">
            <thead class="bg-light text-secondary">
                <tr>
                    <th width="5%" class="text-center">No</th>
                    <th>Kode</th>
                    <th>Nama Jenis Pupuk</th>
                    <th class="text-right">Total Pasokan Masuk</th>
                    <th class="text-right">Total Penebusan Keluar</th>
                    <th class="text-right bg-light">Sisa Stok Gudang Aktual</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $no = 1; 
                foreach ($rekap as $r): 
                    // Tandai baris jika pupuk tersebut merupakan barang terlaris
                    $isTerlaris = ($r['total_keluar'] > 0 && $r['nama_pupuk'] === $terlaris['nama_pupuk']);
                ?>
                    <tr class="<?= $isTerlaris ? 'table-warning font-weight-bold' : '' ?>">
                        <td class="text-center"><?= $no++ ?></td>
                        <td><span class="badge badge-secondary"><?= $r['kode_pupuk'] ?></span></td>
                        <td>
                            <?= $r['nama_pupuk'] ?>
                            <?php if ($isTerlaris): ?>
                                <span class="badge badge-warning ml-2 text-xs"><i class="fas fa-fire mr-1"></i> Terlaris</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-right text-info font-weight-bold">+ <?= number_format($r['total_masuk'], 0, ',', '.') ?> Kg</td>
                        <td class="text-right text-success font-weight-bold">- <?= number_format($r['total_keluar'], 0, ',', '.') ?> Kg</td>
                        <td class="text-right font-weight-bold bg-light"><?= number_format($r['stok_aktual'], 0, ',', '.') ?> Kg</td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- JAVASCRIPT HIDING LOGIC UNTUK FORM FILTER -->
<script>
    document.addEventListener("DOMContentLoaded", function() {
        const selectFilter = document.getElementById('filter_type');
        const zonaHarian = document.getElementById('zona_harian');
        const zonaBulanan = document.getElementById('zona_bulanan');
        const zonaTahun = document.getElementById('zona_tahun');

        function aturTampilanForm() {
            const tipe = selectFilter.value;

            // Sembunyikan semua elemen filter terlebih dahulu
            document.querySelectorAll('.zona-filter').forEach(el => el.classList.add('d-none'));

            // Tampilkan elemen form yang sesuai secara dinamis
            if (tipe === 'harian') {
                zonaHarian.classList.remove('d-none');
            } else if (tipe === 'bulanan') {
                zonaBulanan.classList.remove('d-none');
                zonaTahun.classList.remove('d-none');
            } else if (tipe === 'tahunan') {
                zonaTahun.classList.remove('d-none');
            }
        }

        // Jalankan saat pertama kali halaman dibuka & setiap kali dropdown diganti
        selectFilter.addEventListener('change', aturTampilanForm);
        aturTampilanForm();
    });
</script>

<?= $this->endSection() ?>