<?= $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)</h3>
        <div class="card-tools">
            <button class="btn btn-emerald btn-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><?= $a['jatah_kg'] ?> Kg</td>
                        <td><?= $a['terpakai_kg'] ?> Kg</td>
                        <td>
                            <?php if ($sisa == 0): ?>
                                <span class="badge badge-danger">Habis</span>
                            <?php else: ?>
                                <span class="badge badge-success bg-emerald"><?= $sisa ?> 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('Hapus 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="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() ?>