<?php
// pages/admin/roles
?>
<div class="d-flex align-items-center justify-content-between mb-3">
  <h4 class="mb-0">الأدوار والصلاحيات</h4>
</div>

<?php if (empty($rbac_ready)): ?>
  <div class="alert alert-warning">
    لا توجد جداول الصلاحيات بعد. شغّل ملف التحديث:
    <b>sql/migrate_v1_19_auth_admin.sql</b>
  </div>
<?php else: ?>
<div class="row g-3">
  <div class="col-lg-4">
    <div class="card">
      <div class="card-header fw-bold">الأدوار</div>
      <div class="card-body">
        <form class="row g-2 mb-3" method="post" action="<?= e(url('/admin/roles')) ?>">
          <?= csrf_field() ?>
          <input type="hidden" name="action" value="create">
          <div class="col-8">
            <input name="name" class="form-control" placeholder="اسم الدور">
          </div>
          <div class="col-4 d-grid">
            <button class="btn btn-primary">إضافة</button>
          </div>
        </form>

        <div class="list-group">
          <?php foreach ($roles as $r): ?>
            <a class="list-group-item list-group-item-action d-flex justify-content-between align-items-center <?= ($editRole && (int)$editRole['id']===(int)$r['id']) ? 'active' : '' ?>"
               href="<?= e(url('/admin/roles?action=edit&role_id='.(int)$r['id'])) ?>">
              <span><?= e($r['name']) ?></span>
              <?php if ((int)$r['is_system']===1): ?>
                <span class="badge bg-secondary">System</span>
              <?php endif; ?>
            </a>
          <?php endforeach; ?>
        </div>
      </div>
    </div>
  </div>

  <div class="col-lg-8">
    <?php if (!$editRole): ?>
      <div class="card">
        <div class="card-body text-muted">
          اختر دورًا من القائمة لتعديل صلاحياته.
        </div>
      </div>
    <?php else: ?>
      <div class="card">
        <div class="card-header d-flex align-items-center justify-content-between">
          <div class="fw-bold">تعديل صلاحيات: <?= e($editRole['name']) ?></div>
          <?php if ((int)$editRole['is_system']===0): ?>
            <form method="post" action="<?= e(url('/admin/roles')) ?>" class="d-inline">
              <?= csrf_field() ?>
              <input type="hidden" name="action" value="delete">
              <input type="hidden" name="role_id" value="<?= (int)$editRole['id'] ?>">
              <button class="btn btn-sm btn-outline-danger" type="submit" onclick="return confirm('حذف الدور؟')">حذف</button>
            </form>
          <?php endif; ?>
        </div>
        <div class="card-body">
          <form method="post" action="<?= e(url('/admin/roles')) ?>">
            <?= csrf_field() ?>
            <input type="hidden" name="action" value="save_perms">
            <input type="hidden" name="role_id" value="<?= (int)$editRole['id'] ?>">

            <?php foreach ($permsByModule as $module => $plist): ?>
              <div class="border rounded-3 p-3 mb-3">
                <div class="d-flex align-items-center justify-content-between mb-2">
                  <div class="fw-bold"><?= e($module) ?></div>
                  <button type="button" class="btn btn-sm btn-outline-secondary"
                          onclick="toggleModule('<?= e($module) ?>', this)">تحديد الكل</button>
                </div>

                <div class="row g-2">
                  <?php foreach ($plist as $p): ?>
                    <div class="col-md-6">
                      <label class="form-check">
                        <input class="form-check-input perm-checkbox module-<?= e($module) ?>"
                               type="checkbox"
                               name="perms[]"
                               value="<?= e($p['code']) ?>"
                               <?= !empty($assigned[$p['code']]) ? 'checked' : '' ?>>
                        <span class="form-check-label">
                          <span class="fw-semibold"><?= e($p['name']) ?></span>
                          <span class="text-muted small d-block"><?= e($p['code']) ?></span>
                        </span>
                      </label>
                    </div>
                  <?php endforeach; ?>
                </div>
              </div>
            <?php endforeach; ?>

            <div class="d-flex justify-content-end">
              <button class="btn btn-primary px-4">حفظ</button>
            </div>
          </form>
        </div>
      </div>

      <script>
        function toggleModule(module, btn){
          const boxes = document.querySelectorAll('.module-' + module);
          let allChecked = true;
          boxes.forEach(b => { if(!b.checked) allChecked = false; });
          boxes.forEach(b => b.checked = !allChecked);
        }
      </script>
    <?php endif; ?>
  </div>
</div>
<?php endif; ?>
