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

<div class="container py-5">
  <div class="row justify-content-center">
    <div class="col-lg-7">
      <div class="card border-0 shadow-sm">
        <div class="card-body p-4">
          <h4 class="mb-3">
            <i class="fa-solid fa-building"></i> <?= e($t('اختر المؤسسة','Choose Organization')) ?>
          </h4>

          <?php if(empty($orgs)): ?>
            <div class="alert alert-warning border-0">
              <i class="fa-solid fa-triangle-exclamation"></i>
              <?= e($t('لا توجد عضويات مؤسسات لحسابك. تواصل مع مالك النظام.','No organization memberships found for your account. Contact system owner.')) ?>
            </div>
          <?php else: ?>

            <form method="post" action="<?= e(base_path('choose-org/select')) ?>">
              <div class="list-group">
                <?php foreach($orgs as $o): ?>
                  <label class="list-group-item d-flex justify-content-between align-items-center">
                    <span>
                      <input class="form-check-input me-2" type="radio" name="org_id" value="<?= e((string)$o['id']) ?>" required>
                      <strong><?= e((string)$o['name']) ?></strong>
                      <span class="text-muted"> (<?= e((string)$o['code']) ?>)</span>
                      <span class="badge bg-secondary ms-2"><?= e((string)$o['role_code']) ?></span>
                    </span>
                    <?php if((int)($o['is_active']??1)===0): ?>
                      <span class="badge bg-danger"><?= e($t('موقوفة','Suspended')) ?></span>
                    <?php endif; ?>
                  </label>
                <?php endforeach; ?>
              </div>

              <div class="d-grid mt-3">
                <button class="btn btn-primary btn-lg">
                  <i class="fa-solid fa-check"></i> <?= e($t('دخول','Continue')) ?>
                </button>
              </div>
            </form>

          <?php endif; ?>
        </div>
      </div>
    </div>
  </div>
</div>

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