<?php
// view: pages/masters/categories
$rows = $rows ?? [];
$edit = $edit ?? null;
$all = $all ?? [];
?>
<?php
function cat_label(array $c, array $allById): string {
  $name = (string)($c['name'] ?? '');
  $pid = (int)($c['parent_id'] ?? 0);
  if ($pid > 0 && isset($allById[$pid])) {
    return (string)$allById[$pid]['name'] . ' / ' . $name;
  }
  return $name;
}
$allById = [];
foreach ($all as $c) $allById[(int)$c['id']] = $c;
?>

<div class="row g-3">
  <div class="col-lg-7">
    <div class="card shadow-sm">
      <div class="card-header d-flex justify-content-between align-items-center">
        <strong>فئات الأصناف</strong>
        <span class="text-muted small"><?= count($rows) ?> فئة</span>
      </div>
      <div class="card-body p-0">
        <div class="table-responsive">
          <table class="table table-hover table-striped table-bordered align-middle mb-0">
            <thead class="table-light">
              <tr>
                <th style="width:90px;">#</th>
                <th>الاسم</th>
                <th>الفئة الرئيسية</th>
                <th class="text-center" style="width:120px;">حالة</th>
                <th class="text-center" style="width:110px;">تعديل</th>
                <th class="text-center" style="width:110px;">حذف</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($rows as $r): ?>
                <tr>
                  <td><?= (int)$r['id'] ?></td>
                  <td class="fw-semibold"><?= e($r['name']) ?></td>
                  <td><?= !empty($r['parent_id']) && isset($allById[(int)$r['parent_id']]) ? e($allById[(int)$r['parent_id']]['name']) : '-' ?></td>
                  <td class="text-center"><?= ((int)$r['is_active']===1) ? '<span class="badge text-bg-success">فعال</span>' : '<span class="badge text-bg-secondary">متوقف</span>' ?></td>
                  <td class="text-center">
                    <a class="btn btn-sm btn-outline-primary" href="<?= e(url('/inventory/categories')) ?>?action=edit&id=<?= (int)$r['id'] ?>"><i class="fa-solid fa-pen"></i></a>
                  </td>
                  <td class="text-center">
                    <form method="post" action="<?= e(url('/inventory/categories')) ?>" onsubmit="return confirm('تأكيد حذف الفئة؟')" class="m-0">
                      <input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
                      <input type="hidden" name="action" value="delete">
                      <input type="hidden" name="id" value="<?= (int)$r['id'] ?>">
                      <button class="btn btn-sm btn-outline-danger" type="submit"><i class="fa-solid fa-trash"></i></button>
                    </form>
                  </td>
                </tr>
              <?php endforeach; ?>
              <?php if (!$rows): ?><tr><td colspan="5" class="text-center text-muted py-4">لا توجد بيانات</td></tr><?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

  <div class="col-lg-5">
    <div class="card shadow-sm">
      <div class="card-header"><strong><?= $edit ? 'تعديل فئة' : 'إضافة فئة' ?></strong></div>
      <div class="card-body">
        <form method="post" class="vstack gap-3">
          <input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
          <input type="hidden" name="action" value="save">
          <input type="hidden" name="id" value="<?= (int)($edit['id'] ?? 0) ?>">
          
          <div>
            <label class="form-label">الفئة الرئيسية</label>
            <select class="form-select" name="parent_id">
              <option value="0">- لا يوجد -</option>
              <?php foreach ($all as $c): ?>
                <?php if ((int)($edit['id'] ?? 0) !== (int)$c['id']): ?>
                  <option value="<?= (int)$c['id'] ?>" <?= ((int)($edit['parent_id'] ?? 0) === (int)$c['id']) ? 'selected' : '' ?>>
                    <?= e(cat_label($c, $allById)) ?>
                  </option>
                <?php endif; ?>
              <?php endforeach; ?>
            </select>
            <div class="form-text">اختر فئة رئيسية لإنشاء فئة فرعية تحتها.</div>
          </div>

          <div>
            <label class="form-label">اسم الفئة</label>
            <input class="form-control" name="name" value="<?= e($edit['name'] ?? '') ?>" required>
          </div>
          <div class="form-check">
            <input class="form-check-input" type="checkbox" name="is_active" id="cat_active" value="1" <?= (empty($edit) || (int)($edit['is_active'] ?? 1)===1) ? 'checked' : '' ?>>
            <label class="form-check-label" for="cat_active">فعال</label>
          </div>
          <div class="d-flex gap-2">
            <button class="btn btn-primary flex-fill"><i class="fa-solid fa-save me-2"></i>حفظ</button>
            <a class="btn btn-outline-secondary" href="<?= e(url('/inventory/categories')) ?>">جديد</a>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
