<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="container-fluid">
    <h4 class="mb-3">Edit Stok Masuk</h4>

    <div class="card mb-4">
        <div class="card-body">
            <form action="<?= base_url('admin/product_in/update/' . $data['id']) ?>" method="post">
                <?= csrf_field() ?>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label for="item_id">Barang</label>
                        <div class="input-group">
                            <input type="hidden" name="item_id" id="item_id" value="<?= $data['item_id'] ?>" required>
                            <input type="text" class="form-control" id="item_nama" value="<?= esc($data['nama_item']) ?>" placeholder="Pilih barang" readonly required>
                            <button class="btn btn-outline-secondary" type="button" data-bs-toggle="modal" data-bs-target="#itemModal">Pilih</button>
                        </div>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="supplier_id">Supplier</label>
                        <select name="supplier_id" class="form-select" required>
                            <option value="">-- Pilih Supplier --</option>
                            <?php foreach ($suppliers as $sup): ?>
                                <option value="<?= $sup['id'] ?>" <?= $sup['id'] == $data['supplier_id'] ? 'selected' : '' ?>>
                                    <?= esc($sup['nama']) ?>
                                </option>
                            <?php endforeach ?>
                        </select>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label for="id_gudang">Gudang</label>
                        <select name="id_gudang" class="form-select" required>
                            <option value="">-- Pilih Gudang --</option>
                            <?php foreach ($warehouses as $g): ?>
                                <option value="<?= $g['id'] ?>" <?= $g['id'] == $data['id_gudang'] ? 'selected' : '' ?>>
                                    <?= esc($g['nama']) ?>
                                </option>
                            <?php endforeach ?>
                        </select>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label for="barcode">Barcode</label>
                        <input type="text" name="barcode" class="form-control" value="<?= esc($data['barcode']) ?>" placeholder="Barcode Opsional">
                    </div>
                    <div class="col-md-2 mb-3">
                        <label for="qty">Qty</label>
                        <input type="number" name="qty" class="form-control" value="<?= esc($data['qty']) ?>" required>
                    </div>
                    <div class="col-md-2 mb-3">
                        <label for="tanggal_masuk">Tanggal</label>
                        <input type="date" name="tanggal_masuk" class="form-control" value="<?= esc($data['tanggal_masuk']) ?>" required>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save"></i> Update
                </button>
                <a href="<?= base_url('admin/product_in') ?>" class="btn btn-secondary">Kembali</a>
            </form>
        </div>
    </div>
</div>

<!-- Modal Pilih Barang -->
<div class="modal fade" id="itemModal" tabindex="-1" aria-labelledby="itemModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="itemModalLabel">Pilih Barang</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <table class="table table-bordered table-striped">
          <thead>
            <tr>
              <th>Nama Barang</th>
              <th>Stok Tersisa</th>
              <th>Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($items as $item): ?>
            <tr>
              <td><?= esc($item['nama']) ?></td>
              <td><?= esc($item['stok']) ?></td>
              <td>
                <button type="button" class="btn btn-sm btn-primary"
                    onclick="selectItem(<?= $item['id'] ?>, '<?= esc(addslashes($item['nama'])) ?>')">Pilih</button>
              </td>
            <?php endforeach ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

<script>
function selectItem(id, nama) {
  document.getElementById('item_id').value = id;
  document.getElementById('item_nama').value = nama;
  const modal = bootstrap.Modal.getInstance(document.getElementById('itemModal'));
  modal.hide();
}
</script>

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