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

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

<div class="card card-emerald card-outline shadow">
    <div class="card-header">
        <h3 class="card-title">Data Alokasi Kuota Petani (e-RDKK) Elektronik Rencana Definitif Kebutuhan Kelompok</h3>
        <div class="card-tools">
            <!-- TOMBOL CETAK BARU (Membuka di Tab Baru) -->
            <a href="<?= base_url('alokasi/cetak') ?>" target="_blank" class="btn btn-secondary btn-sm mr-2 shadow-sm">
                <i class="fas fa-print mr-1"></i> Cetak Laporan
            </a>
            
            <button class="btn btn-emerald btn-sm shadow-sm" data-toggle="modal" data-target="#modalTambahAlokasi">
                <i class="fas fa-plus"></i> Set Kuota Baru
            </button>
        </div>
    </div>
    <div class="card-body p-0">
        <table class="table table-hover table-striped">
            <thead class="bg-light">
                <tr>
                    <th>Tahun</th>
                    <th>Nama Petani (NIK)</th>
                    <th>Jenis Pupuk</th>
                    <th>Jatah Kuota (Kg)</th>
                    <th>Terpakai (Kg)</th>
                    <th>Sisa Kuota (Kg)</th>
                    <th class="text-center">Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($alokasi as $a) :
                    $sisa = $a['jatah_kg'] - $a['terpakai_kg'];
                ?>
                    <tr>
                        <td><b><?= $a['tahun_tanam'] ?></b></td>
                        <td><?= $a['nama_petani'] ?> <br><small class="text-muted"><?= $a['nik'] ?></small></td>
                        <td><span class="badge badge-info"><?= $a['nama_pupuk'] ?></span></td>
                        <td><?= number_format($a['jatah_kg'], 0, ',', '.') ?> Kg</td>
                        <td><?= number_format($a['terpakai_kg'], 0, ',', '.') ?> Kg</td>
                        <td>
                            <?php if ($sisa <= 0): ?>
                                <span class="badge badge-danger">Habis</span>
                            <?php else: ?>
                                <span class="badge badge-success bg-emerald"><?= number_format($sisa, 0, ',', '.') ?> Kg</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-center">
                            <a href="<?= base_url('alokasi/hapus/' . $a['id_alokasi']) ?>" class="btn btn-xs btn-danger" onclick="return confirm('Apakah Anda yakin ingin menghapus kuota ini?')">
                                <i class="fas fa-trash"></i>
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>

                <?php if (empty($alokasi)): ?>
                    <tr>
                        <td colspan="7" class="text-center text-muted py-3">Data alokasi belum diatur.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="card card-emerald card-outline shadow mt-4">
    <div class="card-header">
        <h3 class="card-title">
            <i class="fas fa-info-circle mr-2 text-emerald"></i> Apa itu e-RDKK?
        </h3>
    </div>
    <div class="card-body">
        <p><strong>e-RDKK (Elektronik Rencana Definitif Kebutuhan Kelompok)</strong> adalah sistem database digital yang digunakan oleh Kementerian Pertanian Republik Indonesia untuk merencanakan alokasi dan menyalurkan <strong>pupuk bersubsidi</strong> kepada para petani secara transparan, akurat, dan tepat sasaran.</p>
        <p>Sistem ini mendata kebutuhan pupuk berdasarkan Nomor Induk Kependudukan (NIK), luas lahan, lokasi geografis, serta jenis komoditas tanaman yang diusahakan oleh kelompok tani. Dengan adanya e-RDKK, diharapkan distribusi pupuk bersubsidi dapat meminimalisir risiko salah sasaran dan kelangkaan pasokan di tingkat petani.</p>
    </div>
</div>

<!-- MODAL SET KUOTA -->
<div class="modal fade" id="modalTambahAlokasi" 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-clipboard-list mr-2"></i> Set Kuota Petani</h5>
                <button type="button" class="close text-white" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form action="<?= base_url('alokasi/simpan') ?>" method="post">
                <div class="modal-body">
                    <div class="form-group">
                        <label>Tahun Tanam</label>
                        <input type="number" name="tahun_tanam" class="form-control" value="<?= date('Y') ?>" required>
                    </div>
                    <div class="form-group">
                        <label>Pilih Petani</label>
                        <select name="id_petani" class="form-control" required>
                            <option value="">-- Pilih Petani --</option>
                            <?php foreach ($petani as $p): ?>
                                <option value="<?= $p['id_petani'] ?>"><?= $p['nama_petani'] ?> (<?= $p['nik'] ?>)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Pilih Pupuk</label>
                        <select name="id_pupuk" class="form-control" required>
                            <option value="">-- Pilih Jenis Pupuk --</option>
                            <?php foreach ($pupuk as $pu): ?>
                                <option value="<?= $pu['id_pupuk'] ?>"><?= $pu['nama_pupuk'] ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Jatah Kuota (Kg)</label>
                        <input type="number" step="0.01" name="jatah_kg" class="form-control" placeholder="Contoh: 250" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-emerald">Simpan Kuota</button>
                </div>
            </form>
        </div>
    </div>
</div>

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