<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php $t=fn($ar,$en)=>($lang==='ar'?$ar:$en); $rows=$rows??[]; ?>

<div class="container py-3">
  <h4 class="mb-3"><i class="fa-solid fa-shield-halved"></i> <?= e($t('إدارة صلاحيات المؤسسة','Organization RBAC')) ?></h4>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-body">
      <form method="post" action="<?= e(base_path('admin/rbac/upsert')) ?>" class="row g-2">
        <div class="col-md-5">
          <input class="form-control" name="email" type="email" placeholder="<?= e($t('بريد المستخدم','User email')) ?>" required>
        </div>
        <div class="col-md-4">
          <select class="form-select" name="role_code">
            <option value="org_admin"><?= e($t('مدير مؤسسة','Org Admin')) ?></option>
            <option value="chair"><?= e($t('رئيس/Chair','Chair')) ?></option>
            <option value="tpc"><?= e($t('TPC','TPC')) ?></option>
            <option value="editor"><?= e($t('محرر','Editor')) ?></option>
            <option value="reviewer"><?= e($t('محكم','Reviewer')) ?></option>
            <option value="author" selected><?= e($t('مؤلف','Author')) ?></option>
          </select>
        </div>
        <div class="col-md-3 d-grid">
          <button class="btn btn-success"><i class="fa-solid fa-floppy-disk"></i> <?= e($t('حفظ','Save')) ?></button>
        </div>
        <div class="col-md-2">
          <select class="form-select" name="is_active">
            <option value="1" selected>ON</option>
            <option value="0">OFF</option>
          </select>
        </div>
      </form>
      <div class="text-muted small mt-2">
        <i class="fa-solid fa-circle-info"></i> <?= e($t('الصلاحيات هنا معزولة داخل المؤسسة الحالية عبر org_users فقط.','RBAC here is isolated per current organization via org_users only.')) ?>
      </div>
    </div>
  </div>

  <div class="card border-0 shadow-sm">
    <div class="table-responsive">
      <table class="table table-hover mb-0">
        <thead class="table-light">
          <tr>
            <th>#</th><th><?=e($t('البريد','Email'))?></th><th><?=e($t('الدور','Role'))?></th><th><?=e($t('الحالة','Active'))?></th><th><?=e($t('إجراء','Action'))?></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach($rows as $r): ?>
            <tr>
              <td class="text-muted"><?= e((string)$r['id']) ?></td>
              <td><?= e((string)$r['email']) ?></td>
              <td><span class="badge bg-secondary"><?= e((string)$r['role_code']) ?></span></td>
              <td><?= ((int)$r['is_active']===1)?'<span class="badge bg-success">ON</span>':'<span class="badge bg-danger">OFF</span>' ?></td>
              <td>
                <a class="btn btn-sm btn-outline-danger" href="<?= e(base_path('admin/rbac/deactivate?id='.(int)$r['id'])) ?>">
                  <i class="fa-solid fa-ban"></i> <?= e($t('تعطيل','Deactivate')) ?>
                </a>
              </td>
            </tr>
          <?php endforeach; ?>
          <?php if(empty($rows)): ?><tr><td colspan="5" class="text-center text-muted py-4">—</td></tr><?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

<?php require __DIR__ . '/../../layouts/footer.php'; ?>
