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

<h4 class="mb-4">Tambah Produk</h4>

<form action="<?= base_url('admin/item/save') ?>" method="post">
  <div class="row mb-2">
    <div class="col-md-4">
      <label>Kode / Barcode</label>
      <input type="text" name="barcode" class="form-control" required>
    </div>
    <div class="col-md-4">
      <label>Nama Produk</label>
      <input type="text" name="nama" class="form-control" required>
    </div>
    <div class="col-md-4">
      <label>Kategori</label>
      <select name="id_category" class="form-select" required>
        <option value="">-- Pilih Kategori --</option>
        <?php foreach($categories as $c): ?>
          <option value="<?= $c['id'] ?>"><?= esc($c['nama']) ?></option>
        <?php endforeach ?>
      </select>
    </div>
  </div>

  <div class="row mb-2">
    <div class="col-md-4">
      <label>Unit</label>
      <select name="id_unit" class="form-select" required>
        <option value="">-- Pilih Satuan --</option>
        <?php foreach($units as $u): ?>
          <option value="<?= $u['id'] ?>"><?= esc($u['nama']) ?></option>
        <?php endforeach ?>
      </select>
    </div>
    <div class="col-md-4">
      <label>Harga</label>
      <input type="number" name="harga" class="form-control" id="harga" required>
    </div>
    <div class="col-md-4">
      <label>Stok</label>
      <input type="number" name="stok" class="form-control" required>
    </div>
    <div class="col-md-4 mt-3">
      <label>Diskon (%)</label>
      <input type="number" name="diskon" class="form-control" id="diskon" min="0" max="100" value="0">
    </div>
  </div>

  <div class="row mb-2" id="total-wrapper" style="display: none;">
    <div class="col-md-4">
      <label for="total_harga" class="form-label">Total Harga</label>
      <input type="number" name="total_harga" id="total_harga" class="form-control" readonly>
    </div>
  </div>

  <div class="mb-3">
    <label>Gudang</label>
    <select name="id_gudang" class="form-select" required>
      <option value="">-- Pilih Gudang --</option>
      <?php foreach($gudangs as $g): ?>
        <option value="<?= $g['id'] ?>">
          <?= esc($g['nama']) ?>
        </option>
      <?php endforeach ?>
    </select>
  </div>

  <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Simpan</button>
  <a href="<?= base_url('admin/item') ?>" class="btn btn-secondary">Batal</a>
</form>

<script>
  const hargaInput = document.getElementById('harga');
  const diskonInput = document.getElementById('diskon');
  const totalHargaInput = document.getElementById('total_harga');
  const totalWrapper = document.getElementById('total-wrapper');

  function updateTotalHarga() {
    const harga = parseFloat(hargaInput.value);
    const diskon = parseFloat(diskonInput.value);

    const isValid = !isNaN(harga) && !isNaN(diskon);

    if (isValid) {
      const total = harga - (harga * diskon / 100);
      totalHargaInput.value = Math.round(total);
      totalWrapper.style.display = 'flex'; // tampilkan jika valid
    } else {
      totalHargaInput.value = '';
      totalWrapper.style.display = 'none'; // sembunyikan jika belum valid
    }
  }

  hargaInput.addEventListener('input', updateTotalHarga);
  diskonInput.addEventListener('input', updateTotalHarga);
</script>

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