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

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

    <div class="card mb-4">
        <div class="card-body">
            <form action="<?= base_url('admin/product_in/save') ?>" method="post">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label for="item_id">Barang</label>
                        <div class="input-group">
                            <input type="text" id="item_nama" class="form-control" placeholder="Klik untuk pilih barang" readonly data-bs-toggle="modal" data-bs-target="#itemModal">
                            <input type="hidden" name="item_id" id="item_id" required>
                        </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'] ?>"><?= 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'] ?>"><?= 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" placeholder="Barcode Opsional">
                    </div>
                    <div class="col-md-2 mb-3">
                        <label for="qty">Qty</label>
                        <input type="number" name="qty" class="form-control" required>
                    </div>
                    <div class="col-md-3 mb-3">
    <label for="tanggal_keluar">Tanggal & Jam</label>
    <input type="datetime-local" name="tanggal_keluar" class="form-control" 
           value="<?= date('Y-m-d\TH:i') ?>" required>
       </div>
                </div>
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save"></i> Simpan
                </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($item['nama']) ?>')">Pilih</button>
              </td>
            </tr>
            <?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() ?>
