<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>

<div class="container my-4">
  <?php $isEdit = !empty($item); ?>
  <h3 class="mb-3"><?= $isEdit ? 'Edit' : 'Add' ?> Office Supply</h3>

  <?= view('partials/flash_messages') ?>
<?php
$isEdit  = isset($isEdit) && $isEdit;      // passed from controller on edit
$itemId  = $item['id'] ?? null;
$action  = $isEdit
    ? site_url('inventory/update/'.$itemId)
    : site_url('inventory/store');
?>
  <form method="post" action="<?= $isEdit ? site_url('inventory/update/'.$item['id']) : site_url('inventory/store') ?>">
    <?= csrf_field() ?>
    <input type="hidden" name="type" value="office">

    <div class="row g-3">
      <div class="col-md-6"><label class="form-label">Name</label><input class="form-control" name="name" required value="<?= esc($item['name'] ?? '') ?>"></div>
      <div class="col-md-6">
        <label class="form-label">Category</label>
        <select class="form-select" name="category_id">
          <option value="">— None —</option>
          <?php foreach ($categories as $c): ?>
            <option value="<?= esc($c['id']) ?>" <?= isset($item['category_id']) && (int)$item['category_id']===(int)$c['id'] ? 'selected' : '' ?>><?= esc($c['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-2"><label class="form-label">Quantity</label><input type="number" min="0" class="form-control" name="quantity" value="<?= esc($item['quantity'] ?? 0) ?>"></div>
      <div class="col-md-2"><label class="form-label">Unit</label><input class="form-control" name="unit" value="<?= esc($item['unit'] ?? '') ?>"></div>
      <div class="col-md-4"><label class="form-label">SKU</label><input class="form-control" name="sku" value="<?= esc($item['sku'] ?? '') ?>"></div>
       <div class="col-12"><label class="form-label">Description / Notes</label><textarea class="form-control" rows="3" name="description"><?= esc($item['description'] ?? '') ?></textarea></div>
        </div>

    <div class="mt-3 d-flex gap-2">
      <button class="btn btn-primary"><?= $isEdit ? 'Update' : 'Save' ?></button>
      <a class="btn btn-outline-secondary" href="<?= site_url('inventory/office') ?>">Cancel</a>
    </div>
  </form>
</div>

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