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

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

<form action="<?= base_url('admin/item/update/' . $item['id']) ?>" method="post">
  <div class="row mb-2">
    <div class="col-md-4">
      <label>Kode / Barcode</label>
      <input type="text" name="barcode" class="form-control" value="<?= esc($item['barcode']) ?>" required>
    </div>
    <div class="col-md-4">
      <label>Nama Produk</label>
      <input type="text" name="nama" class="form-control" value="<?= esc($item['nama']) ?>" 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'] ?>" <?= $c['id'] == $item['id_category'] ? 'selected' : '' ?>>
            <?= 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'] ?>" <?= $u['id'] == $item['id_unit'] ? 'selected' : '' ?>>
            <?= esc($u['nama']) ?>
          </option>
        <?php endforeach ?>
      </select>
    </div>
    <div class="col-md-4">
      <label>Harga</label>
      <input type="number" name="harga" id="harga" class="form-control" value="<?= esc($item['harga']) ?>" required>
    </div>
    <div class="col-md-4">
      <label>Stok</label>
      <input type="number" name="stok" class="form-control" value="<?= esc($item['stok']) ?>" required>
    </div>
    <div class="col-md-4">
      <label>Diskon %</label>
      <input type="number" name="diskon" id="diskon" class="form-control" min="0" max="100" value="<?= esc($item['diskon']) ?>">
    </div>
    <div class="col-md-4">
      <label>Total Harga (Setelah Diskon)</label>
      <input type="number" name="total_harga" id="total_harga" class="form-control" value="<?= esc($item['total_harga'] ?? 0) ?>" 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'] ?>" <?= $g['id'] == $item['id_gudang'] ? 'selected' : '' ?>>
          <?= esc($g['nama']) ?>
        </option>
      <?php endforeach ?>
    </select>
  </div>

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

<!-- Script perhitungan total harga -->
<script>
  function hitungTotal() {
    const harga = parseFloat(document.getElementById('harga').value) || 0;
    const diskon = parseFloat(document.getElementById('diskon').value) || 0;
    const total = harga - (harga * diskon / 100);
    document.getElementById('total_harga').value = Math.round(total);
  }

  document.getElementById('harga').addEventListener('input', hitungTotal);
  document.getElementById('diskon').addEventListener('input', hitungTotal);

  // Hitung saat pertama kali halaman dimuat
  window.addEventListener('DOMContentLoaded', hitungTotal);
</script>

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