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

<div class="container py-3">
  <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
    <h4 class="mb-0"><i class="fa-solid fa-sitemap"></i> <?= e($t('لوحة المالك: المؤسسات','Owner: Organizations')) ?></h4>
    <a class="btn btn-outline-primary" href="<?= e(base_path('choose-org?force=1')) ?>">
      <i class="fa-solid fa-building"></i> <?= e($t('تغيير المؤسسة','Switch Org')) ?>
    </a>
  </div>

  <div class="row g-3">
    <div class="col-lg-5">
      <div class="card border-0 shadow-sm">
        <div class="card-header bg-white fw-bold">
          <i class="fa-solid fa-plus"></i> <?= e($t('إضافة / تعديل مؤسسة','Add / Edit Organization')) ?>
        </div>
        <div class="card-body">
          <form method="post" action="<?= e(base_path('admin/owner/orgs/save')) ?>" class="row g-2">
            <input type="hidden" name="id" value="0">
            <div class="col-12">
              <label class="form-label"><?= e($t('الاسم','Name')) ?></label>
              <input class="form-control" name="name" required>
            </div>
            <div class="col-12">
              <label class="form-label"><?= e($t('الكود','Code')) ?></label>
              <input class="form-control" name="code" required placeholder="org_code">
            </div>
            <div class="col-12">
              <label class="form-label"><?= e($t('البريد','Contact Email')) ?></label>
              <input class="form-control" name="contact_email" type="email">
            </div>
            <div class="col-12 d-grid">
              <button class="btn btn-success"><i class="fa-solid fa-floppy-disk"></i> <?= e($t('حفظ','Save')) ?></button>
            </div>
          </form>
        </div>
      </div>

      <div class="card border-0 shadow-sm mt-3">
        <div class="card-header bg-white fw-bold">
          <i class="fa-solid fa-user-shield"></i> <?= e($t('تعيين مدير مؤسسة','Assign Org Admin')) ?>
        </div>
        <div class="card-body">
          <form method="post" action="<?= e(base_path('admin/owner/orgs/assign-admin')) ?>" class="row g-2">
            <div class="col-12">
              <label class="form-label"><?= e($t('المؤسسة','Organization')) ?></label>
              <select class="form-select" name="org_id" required>
                <?php foreach($orgs as $o): ?>
                  <option value="<?= e((string)$o['id']) ?>"><?= e((string)$o['name']) ?> (<?= e((string)$o['code']) ?>)</option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="col-12">
              <label class="form-label"><?= e($t('المستخدم','User')) ?></label>
              <select class="form-select" name="user_id" required>
                <?php foreach($admins as $u): ?>
                  <option value="<?= e((string)$u['id']) ?>">#<?= e((string)$u['id']) ?> — <?= e((string)$u['email']) ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="col-12 d-grid">
              <button class="btn btn-primary"><i class="fa-solid fa-user-check"></i> <?= e($t('تعيين','Assign')) ?></button>
            </div>
          </form>
        </div>
      </div>

    </div>

    <div class="col-lg-7">
      <div class="card border-0 shadow-sm">
        <div class="card-header bg-white fw-bold">
          <i class="fa-solid fa-table"></i> <?= e($t('قائمة المؤسسات','Organizations List')) ?>
        </div>
        <div class="table-responsive">
          <table class="table table-hover mb-0">
            <thead class="table-light">
              <tr>
                <th>#</th>
                <th><?= e($t('الاسم','Name')) ?></th>
                <th><?= e($t('الكود','Code')) ?></th>
                <th><?= e($t('مؤتمرات','Conferences')) ?></th>
                <th><?= e($t('أعضاء','Members')) ?></th>
                <th><?= e($t('حالة','Status')) ?></th>
                <th><?= e($t('إجراءات','Actions')) ?></th>
              </tr>
            </thead>
            <tbody>
              <?php foreach($orgs as $o): ?>
                <tr>
                  <td class="text-muted"><?= e((string)$o['id']) ?></td>
                  <td><?= e((string)$o['name']) ?></td>
                  <td class="text-muted"><?= e((string)$o['code']) ?></td>
                  <td><?= e((string)$o['conferences_count']) ?></td>
                  <td><?= e((string)$o['members_count']) ?></td>
                  <td><?= ((int)$o['is_active']===1) ? '<span class="badge bg-success">ON</span>' : '<span class="badge bg-danger">OFF</span>' ?></td>
                  <td class="d-flex gap-2 flex-wrap">
                    <a class="btn btn-sm btn-outline-warning" href="<?= e(base_path('admin/owner/orgs/toggle?id='.(int)$o['id'])) ?>">
                      <i class="fa-solid fa-toggle-on"></i> <?= e($t('تبديل','Toggle')) ?>
                    </a>
                    <a class="btn btn-sm btn-outline-primary" href="<?= e(base_path('admin/owner/impersonate?org_id='.(int)$o['id'])) ?>">
                      <i class="fa-solid fa-right-to-bracket"></i> <?= e($t('دخول كسياق','Impersonate')) ?>
                    </a>
                  </td>
                </tr>
              <?php endforeach; ?>
              <?php if(empty($orgs)): ?><tr><td colspan="7" class="text-center text-muted py-4">—</td></tr><?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>

      <div class="alert alert-info border-0 shadow-sm mt-3">
        <i class="fa-solid fa-circle-info"></i>
        <?= e($t('العزل التام سيتم تفعيله في Pack 203 بحيث لا يرى أي مستخدم إلا مؤتمرات منظمته فقط.',
                'Full tenant isolation will be enforced in Pack 203 so users only see their organization data.')) ?>
      </div>
    </div>
  </div>
</div>

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