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

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

<div class="card card-emerald card-outline shadow">
    <div class="card-header">
        <h3 class="card-title">Manajemen Stok & Harga Pupuk</h3>
        <div class="card-tools">
            <button class="btn btn-emerald btn-sm" data-toggle="modal" data-target="#modalTambahPupuk">
                <i class="fas fa-plus"></i> Tambah Pupuk
            </button>
        </div>
    </div>
    <div class="card-body p-0">
        <table class="table table-hover table-striped mb-0">
            <thead class="bg-light">
                <tr>
                    <th width="5%" class="text-center">No</th>
                    <th>Kode</th>
                    <th>Nama Pupuk</th>
                    <th>Harga HET (Rp)</th>
                    <th>Stok Gudang (Kg)</th>
                    <th class="text-center">Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php $no = 1;
                foreach ($pupuk as $p) : ?>
                    <tr>
                        <td class="text-center"><?= $no++ ?></td>
                        <td><span class="badge badge-secondary"><?= $p['kode_pupuk'] ?></span></td>
                        <td><b><?= $p['nama_pupuk'] ?></b></td>
                        <td>Rp <?= number_format($p['harga_het'], 0, ',', '.') ?></td>
                        <td>
                            <?php if ($p['stok_global'] <= 100): ?>
                                <span class="badge badge-danger"><?= number_format($p['stok_global'], 0, ',', '.') ?> Kg (Kritis)</span>
                            <?php else: ?>
                                <span class="badge badge-success bg-emerald"><?= number_format($p['stok_global'], 0, ',', '.') ?> Kg</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-center">
                            <button class="btn btn-xs btn-info btn-edit-pupuk"
                                data-id="<?= $p['id_pupuk'] ?>"
                                data-kode="<?= $p['kode_pupuk'] ?>"
                                data-nama="<?= $p['nama_pupuk'] ?>"
                                data-harga="<?= $p['harga_het'] ?>"
                                data-stok="<?= (int)$p['stok_global'] ?>"
                                data-toggle="modal" data-target="#modalEditPupuk" title="Edit Data">
                                <i class="fas fa-edit"></i>
                            </button>
                            <a href="<?= base_url('pupuk/hapus/' . $p['id_pupuk']) ?>" class="btn btn-xs btn-danger" onclick="return confirm('Yakin ingin menghapus pupuk ini dari database?')" title="Hapus Data">
                                <i class="fas fa-trash"></i>
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>

                <?php if (empty($pupuk)): ?>
                    <tr>
                        <td colspan="6" class="text-center text-muted py-4">Belum ada data pupuk yang didaftarkan.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="modal fade" id="modalTambahPupuk" 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-leaf mr-2"></i> Input Pupuk Baru</h5>
                <button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="<?= base_url('pupuk/simpan') ?>" method="post">
                <div class="modal-body">
                    <div class="form-group">
                        <label>Kode Pupuk</label>
                        <input type="text" name="kode_pupuk" class="form-control" placeholder="Contoh: UR-01" required>
                    </div>
                    <div class="form-group">
                        <label>Nama Pupuk</label>
                        <input type="text" name="nama_pupuk" class="form-control" placeholder="Contoh: Urea Bersubsidi" required>
                    </div>
                    <div class="form-group">
                        <label>Harga HET (Rp/Kg)</label>
                        <input type="number" name="harga_het" class="form-control" placeholder="Contoh: 2250" required>
                    </div>
                    <div class="form-group">
                        <label>Stok Awal (Kg)</label>
                        <input type="number" name="stok_global" class="form-control" placeholder="Contoh: 500" 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 Data</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="modalEditPupuk" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-info text-white">
                <h5 class="modal-title"><i class="fas fa-edit mr-2"></i> Edit Data Pupuk</h5>
                <button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form id="formEditPupuk" method="post">
                <div class="modal-body">
                    <div class="form-group">
                        <label>Kode Pupuk</label>
                        <input type="text" name="kode_pupuk" id="edit_kode_pupuk" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <label>Nama Pupuk</label>
                        <input type="text" name="nama_pupuk" id="edit_nama_pupuk" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <label>Harga HET (Rp/Kg)</label>
                        <input type="number" name="harga_het" id="edit_harga_het" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <label>Update Stok (Kg)</label>
                        <input type="number" name="stok_global" id="edit_stok_global" class="form-control" required>
                        <small class="text-muted">Hanya ubah jika ada penyesuaian opname manual.</small>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-info">Simpan Perubahan</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        const tombolEdit = document.querySelectorAll('.btn-edit-pupuk');
        const formEdit = document.getElementById('formEditPupuk');
        
        tombolEdit.forEach(button => {
            button.addEventListener('click', function() {
                const id = this.getAttribute('data-id');
                const kode = this.getAttribute('data-kode');
                const nama = this.getAttribute('data-nama');
                const harga = this.getAttribute('data-harga');
                const stok = this.getAttribute('data-stok');

                formEdit.setAttribute('action', '<?= base_url('pupuk/update') ?>/' + id);

                document.getElementById('edit_kode_pupuk').value = kode;
                document.getElementById('edit_nama_pupuk').value = nama;
                document.getElementById('edit_harga_het').value = harga;
                document.getElementById('edit_stok_global').value = stok;
            });
        });
    });
</script>

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