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

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

<?php if (session()->getFlashdata('success')): ?>
    <div class="alert alert-success alert-dismissible fade show mb-3" role="alert">
        <i class="fas fa-check-circle mr-2"></i> <?= session()->getFlashdata('success') ?>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
<?php endif; ?>

<?php if (session()->getFlashdata('error')): ?>
    <div class="alert alert-danger alert-dismissible fade show mb-3" role="alert">
        <i class="fas fa-ban mr-2"></i> <?= session()->getFlashdata('error') ?>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
<?php endif; ?>

<div class="card card-emerald card-outline shadow">
    <div class="card-header">
        <h3 class="card-title">Log Kedatangan Pasokan Pupuk</h3>
        <div class="card-tools">
            <button class="btn btn-emerald btn-sm" data-toggle="modal" data-target="#modalMasuk">
                <i class="fas fa-download"></i> Catat Pupuk Datang
            </button>
        </div>
    </div>
    <div class="card-body p-0">
        <table class="table table-hover table-striped mb-0">
            <thead class="bg-light">
                <tr>
                    <th>Tanggal Datang</th>
                    <th>Kode / Nama Pupuk</th>
                    <th>Asal / Distributor</th>
                    <th>Jumlah Pasokan</th>
                    <th class="text-center" width="15%">Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php if(empty($pupuk_masuk)): ?>
                    <tr><td colspan="5" class="text-center text-muted py-4">Belum ada riwayat pasokan pupuk masuk.</td></tr>
                <?php else: ?>
                    <?php foreach($pupuk_masuk as $pm): ?>
                    <tr>
                        <td><?= date('d/m/Y', strtotime($pm['tanggal_masuk'])) ?></td>
                        <td><span class="badge badge-secondary"><?= $pm['kode_pupuk'] ?></span> <b><?= $pm['nama_pupuk'] ?></b></td>
                        <td><?= $pm['asal_pupuk'] ?></td>
                        <td class="text-success font-weight-bold">+ <?= number_format($pm['jumlah_masuk'], 0, ',', '.') ?> Kg</td>
                        <td class="text-center">
                            <button class="btn btn-xs btn-info btn-edit-masuk"
                                data-id="<?= $pm['id_masuk'] ?>"
                                data-tanggal="<?= $pm['tanggal_masuk'] ?>"
                                data-idpupuk="<?= $pm['id_pupuk'] ?>"
                                data-asal="<?= $pm['asal_pupuk'] ?>"
                                data-jumlah="<?= (int)$pm['jumlah_masuk'] ?>"
                                data-toggle="modal" data-target="#modalEditMasuk" title="Edit Log">
                                <i class="fas fa-edit"></i>
                            </button>
                            <a href="<?= base_url('pupukmasuk/hapus/' . $pm['id_masuk']) ?>" class="btn btn-xs btn-danger" onclick="return confirm('Apakah Anda yakin ingin menghapus data pasokan masuk ini? Tindakan ini akan memotong stok gudang global otomatis.')" title="Hapus Log">
                                <i class="fas fa-trash"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="modal fade" id="modalMasuk" 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-truck mr-2"></i> Form Pasokan Pupuk Masuk</h5>
                <button type="button" class="close text-white" data-dismiss="modal"><span>&times;</span></button>
            </div>
            <form action="<?= base_url('pupukmasuk/simpan') ?>" method="post">
                <div class="modal-body">
                    <div class="form-group">
                        <label>Tanggal Kedatangan</label>
                        <input type="date" name="tanggal_masuk" class="form-control" value="<?= date('Y-m-d') ?>" required
                               oninvalid="this.setCustomValidity('Tanggal kedatangan wajib diisi!')" 
                               oninput="this.setCustomValidity('')">
                    </div>
                    <div class="form-group">
                        <label>Pilih Jenis Pupuk</label>
                        <select name="id_pupuk" class="form-control" required
                                oninvalid="this.setCustomValidity('Silakan pilih jenis pupuk terlebih dahulu!')" 
                                oninput="this.setCustomValidity('')">
                            <option value="">-- Pilih Pupuk --</option>
                            <?php foreach($pupuk as $p): ?>
                                <option value="<?= $p['id_pupuk'] ?>"><?= $p['nama_pupuk'] ?> (Stok Saat Ini: <?= number_format($p['stok_global'], 0, ',', '.') ?> Kg)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Asal / Nama Distributor Resmi</label>
                        <input type="text" name="asal_pupuk" class="form-control" placeholder="Contoh: Gudang Lini III / PT. Pupuk Kaltim" required
                               oninvalid="this.setCustomValidity('Asal pupuk atau nama distributor wajib diisi!')" 
                               oninput="this.setCustomValidity('')">
                    </div>
                    <div class="form-group">
                        <label>Jumlah Masuk (Kg)</label>
                        <input type="number" name="jumlah_masuk" class="form-control" placeholder="Contoh: 1000" required min="1"
                               oninvalid="this.setCustomValidity('Jumlah pasokan masuk wajib diisi dengan angka bulat minimal 1 Kg!')" 
                               oninput="this.setCustomValidity('')">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-emerald btn-block font-weight-bold">Simpan & Update Stok Global</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="modalEditMasuk" 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 Pasokan Pupuk</h5>
                <button type="button" class="close text-white" data-dismiss="modal"><span>&times;</span></button>
            </div>
            <form id="formEditMasuk" method="post">
                <div class="modal-body">
                    <div class="form-group">
                        <label>Tanggal Kedatangan</label>
                        <input type="date" name="tanggal_masuk" id="edit_tanggal_masuk" class="form-control" required
                               oninvalid="this.setCustomValidity('Tanggal kedatangan perubahan wajib diisi!')" 
                               oninput="this.setCustomValidity('')">
                    </div>
                    <div class="form-group">
                        <label>Pilih Jenis Pupuk</label>
                        <select name="id_pupuk" id="edit_id_pupuk" class="form-control" required
                                oninvalid="this.setCustomValidity('Silakan tentukan pilihan jenis pupuk!')" 
                                oninput="this.setCustomValidity('')">
                            <option value="">-- Pilih Pupuk --</option>
                            <?php foreach($pupuk as $p): ?>
                                <option value="<?= $p['id_pupuk'] ?>"><?= $p['nama_pupuk'] ?> (Stok: <?= number_format($p['stok_global'], 0, ',', '.') ?> Kg)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Asal / Nama Distributor Resmi</label>
                        <input type="text" name="asal_pupuk" id="edit_asal_pupuk" class="form-control" required
                               oninvalid="this.setCustomValidity('Data nama distributor wajib diisi!')" 
                               oninput="this.setCustomValidity('')">
                    </div>
                    <div class="form-group">
                        <label>Jumlah Masuk (Kg)</label>
                        <input type="number" name="jumlah_masuk" id="edit_jumlah_masuk" class="form-control" required min="1"
                               oninvalid="this.setCustomValidity('Kuantitas pasokan wajib diisi bilangan bulat minimal 1 Kg!')" 
                               oninput="this.setCustomValidity('')">
                    </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 font-weight-bold">Simpan Perubahan & Koreksi Stok</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        const tombolEdit = document.querySelectorAll('.btn-edit-masuk');
        const formEdit = document.getElementById('formEditMasuk');
        
        tombolEdit.forEach(button => {
            button.addEventListener('click', function() {
                const id = button.getAttribute('data-id');
                const tanggal = button.getAttribute('data-tanggal');
                const idpupuk = button.getAttribute('data-idpupuk');
                const asal = button.getAttribute('data-asal');
                const jumlah = button.getAttribute('data-jumlah');

                // Set rute update secara dinamis menggunakan ID log pasokan yang benar (id_masuk)
                formEdit.setAttribute('action', '<?= base_url('pupukmasuk/update') ?>/' + id);

                // Isikan nilai data ke dalam elemen inputan modal edit
                document.getElementById('edit_tanggal_masuk').value = tanggal;
                document.getElementById('edit_id_pupuk').value = idpupuk;
                document.getElementById('edit_asal_pupuk').value = asal;
                document.getElementById('edit_jumlah_masuk').value = jumlah;
            });
        });
    });
</script>

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