<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container my-4">
  <h3>Edit Inventory Movement</h3>

  <form action="<?= site_url('inventory/movements/update/'.$movement['id']) ?>" method="post">
    <?= csrf_field() ?>
    <div class="mb-3">
      <label class="form-label">Item</label>
      <input type="text" class="form-control" value="<?= esc($movement['item_name'] ?? $movement['item_id']) ?>" disabled>
    </div>

    <div class="mb-3">
      <label class="form-label">Quantity Change</label>
      <input type="number" name="qty_change" class="form-control" value="<?= esc($movement['qty_change']) ?>" required>
    </div>

    <div class="mb-3">
      <label class="form-label">Movement Type</label>
      <select name="movement_type" class="form-select" required>
        <?php foreach (['initial','in','out','adjust','distribution'] as $type): ?>
          <option value="<?= $type ?>" <?= $movement['movement_type']===$type ? 'selected' : '' ?>>
            <?= ucfirst($type) ?>
          </option>
        <?php endforeach; ?>
      </select>
    </div>

    <div class="mb-3">
      <label class="form-label">Reason</label>
      <input type="text" name="reason" class="form-control" maxlength="120" value="<?= esc($movement['reason']) ?>">
    </div>

    <div class="mb-3">
      <label class="form-label">Note</label>
      <textarea name="note" class="form-control"><?= esc($movement['note']) ?></textarea>
    </div>

    <div class="row">
      <div class="col-md-6 mb-3">
        <label class="form-label">Semester</label>
        <select name="semester" class="form-select">
          <option value="">-- None --</option>
          <option value="Spring" <?= $movement['semester']==='Spring'?'selected':'' ?>>Spring</option>
          <option value="Fall" <?= $movement['semester']==='Fall'?'selected':'' ?>>Fall</option>
        </select>
      </div>
      <div class="col-md-6 mb-3">
        <label class="form-label">School Year</label>
        <input type="text" name="school_year" class="form-control" value="<?= esc($movement['school_year']) ?>">
      </div>
    </div>

    <button type="submit" class="btn btn-primary">Save Changes</button>
    <a href="<?= site_url('inventory/movements') ?>" class="btn btn-secondary">Cancel</a>
  </form>
</div>
<?= $this->endSection() ?>
