<?= $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"><?= $p['stok_global'] ?> Kg (Kritis)</span>
                            <?php else: ?>
                                <span class="badge badge-success bg-emerald"><?= $p['stok_global'] ?> 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="<?= $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" required>
                    </div>
                    <div class="form-group">
                        <label>Stok Awal (Kg)</label>
                        <input type="number" step="0.01" name="stok_global" class="form-control" 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" step="0.01" 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).ready(function() {
        $('.btn-edit-pupuk').on('click', function() {
            // Ambil data dari atribut data-* pada tombol
            const id = $(this).data('id');
            const kode = $(this).data('kode');
            const nama = $(this).data('nama');
            const harga = $(this).data('harga');
            const stok = $(this).data('stok');

            // Set action pada form edit
            $('#formEditPupuk').attr('action', '<?= base_url('pupuk/update') ?>/' + id);

            // Masukkan data ke dalam inputan modal
            $('#edit_kode_pupuk').val(kode);
            $('#edit_nama_pupuk').val(nama);
            $('#edit_harga_het').val(harga);
            $('#edit_stok_global').val(stok);
        });
    });
</script>

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