<?php // vars: $item, $categories, $errors, $is_edit ?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0"><?= $is_edit ? 'تعديل بند مصروف' : 'إضافة بند مصروف' ?></h4>
  <a class="btn btn-outline-secondary" href="<?= e(url('/finance/expenses/items')) ?>">رجوع</a>
</div>

<?php if (!empty($errors)): ?>
  <div class="alert alert-danger"><ul class="mb-0"><?php foreach ($errors as $er): ?><li><?= e($er) ?></li><?php endforeach; ?></ul></div>
<?php endif; ?>

<form class="card p-3" method="post">
  <?= csrf_field() ?>
  <div class="row g-3">
    <div class="col-md-4">
      <label class="form-label">مجموعة المصروف</label>
      <select class="form-select" name="category_id" required>
        <option value="0">اختر المجموعة</option>
        <?php foreach ($categories as $cat): ?>
          <option value="<?= (int)$cat['id'] ?>" <?= ((int)$item['category_id'] === (int)$cat['id'] ? 'selected' : '') ?>><?= e($cat['name']) ?><?= ((int)$cat['is_active']!==1 ? ' (غير نشطة)' : '') ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-4">
      <label class="form-label">اسم البند</label>
      <input type="text" class="form-control" name="name" value="<?= e($item['name']) ?>" required>
    </div>
    <div class="col-md-2">
      <label class="form-label">الكود</label>
      <input type="text" class="form-control" name="code" value="<?= e($item['code']) ?>">
    </div>
    <div class="col-md-2">
      <label class="form-label">الترتيب</label>
      <input type="number" class="form-control" name="sort_order" value="<?= e((string)$item['sort_order']) ?>">
    </div>
    <div class="col-md-2">
      <label class="form-label">الحالة</label>
      <select class="form-select" name="is_active">
        <option value="1" <?= ((int)$item['is_active']===1?'selected':'') ?>>نشط</option>
        <option value="0" <?= ((int)$item['is_active']===0?'selected':'' ) ?>>غير نشط</option>
      </select>
    </div>
    <div class="col-md-10">
      <label class="form-label">ملاحظات</label>
      <input type="text" class="form-control" name="notes" value="<?= e($item['notes']) ?>">
    </div>
  </div>
  <div class="mt-3 d-flex gap-2">
    <button class="btn btn-primary" type="submit"><i class="fa-solid fa-floppy-disk"></i> حفظ</button>
    <a class="btn btn-outline-secondary" href="<?= e(url('/finance/expenses/items')) ?>">إلغاء</a>
  </div>
</form>
