<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php
$t=fn($ar,$en)=>($lang==='ar'?$ar:$en);
$orgs=$orgs??[]; $admins=$admins??[]; $kpi=$kpi??[];
?>
<div class="container py-3">

  <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
    <div>
      <h4 class="mb-0"><i class="fa-solid fa-sitemap"></i> <?= e($t('لوحة المالك - المؤسسات','Owner - Organizations')) ?></h4>
      <div class="text-muted small"><?= e($t('إدارة المؤسسات كخدمة SaaS: إنشاء، تعديل، تفعيل/تعطيل، وتعيين مدراء.','Manage organizations as SaaS: create, edit, enable/disable, and assign admins.')) ?></div>
    </div>
    <a class="btn btn-primary" href="<?= e(base_path('admin/owner/orgs/create')) ?>"><i class="fa-solid fa-plus"></i> <?= e($t('مؤسسة جديدة','New Org')) ?></a>
  </div>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white fw-bold"><i class="fa-solid fa-building"></i> <?= e($t('المؤسسات','Organizations')) ?></div>
    <div class="table-responsive">
      <table class="table table-hover align-middle mb-0">
        <thead class="table-light">
          <tr>
            <th>#</th>
            <th><?=e($t('الكود','Code'))?></th>
            <th><?=e($t('الاسم','Name'))?></th>
            <th><?=e($t('الحالة','Status'))?></th>
            <th><?=e($t('إحصاءات','KPIs'))?></th>
            <th><?=e($t('إجراء','Action'))?></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach($orgs as $o): $oid=(int)$o['id']; $stat=$kpi[$oid]??['admins'=>0,'users'=>0,'confs'=>0,'subs'=>0]; ?>
            <tr>
              <td class="text-muted"><?= e((string)$oid) ?></td>
              <td><span class="badge bg-dark"><?= e((string)$o['code']) ?></span></td>
              <td class="fw-semibold"><?= e((string)$o['name']) ?></td>
              <td>
                <?php if((int)($o['is_active']??1)===1): ?>
                  <span class="badge bg-success"><?= e($t('نشط','Active')) ?></span>
                <?php else: ?>
                  <span class="badge bg-danger"><?= e($t('غير نشط','Inactive')) ?></span>
                <?php endif; ?>
              </td>
              <td class="text-muted small">
                <span class="me-2"><i class="fa-solid fa-user-shield"></i> <?= e((string)$stat['admins']) ?></span>
                <span class="me-2"><i class="fa-solid fa-users"></i> <?= e((string)$stat['users']) ?></span>
                <span class="me-2"><i class="fa-solid fa-calendar-days"></i> <?= e((string)$stat['confs']) ?></span>
                <span><i class="fa-solid fa-file-lines"></i> <?= e((string)$stat['subs']) ?></span>
              </td>
              <td class="d-flex gap-2 flex-wrap">
                <a class="btn btn-sm btn-outline-primary" href="<?= e(base_path('admin/owner/orgs/edit?id='.$oid)) ?>">
                  <i class="fa-solid fa-pen"></i> <?= e($t('تعديل','Edit')) ?>
                </a>
                <a class="btn btn-sm btn-outline-warning" href="<?= e(base_path('admin/owner/orgs/toggle?id='.$oid)) ?>">
                  <i class="fa-solid fa-toggle-on"></i> <?= e($t('تفعيل/تعطيل','Toggle')) ?>
                </a>
                <a class="btn btn-sm btn-outline-danger" onclick="return confirm('<?= e($t('هل تريد تعطيل هذه المؤسسة؟ سيتم إيقاف عضوياتها.','Disable this organization? Its memberships will be disabled.')) ?>')" href="<?= e(base_path('admin/owner/orgs/delete?id='.$oid)) ?>">
                  <i class="fa-solid fa-trash"></i> <?= e($t('تعطيل','Disable')) ?>
                </a>
              </td>
            </tr>
          <?php endforeach; ?>
          <?php if(empty($orgs)): ?><tr><td colspan="6" class="text-center text-muted py-4">—</td></tr><?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

  <div class="row g-3">
    <div class="col-lg-6">
      <div class="card border-0 shadow-sm h-100">
        <div class="card-header bg-white fw-bold"><i class="fa-solid fa-user-shield"></i> <?= e($t('تعيين Org Admin (مستخدم موجود)','Assign Org Admin (existing user)')) ?></div>
        <div class="card-body">
          <form method="post" action="<?= e(base_path('admin/owner/orgs/admin-add')) ?>" class="row g-2">
            <input type="hidden" name="mode" value="existing">
            <div class="col-md-5">
              <select class="form-select" name="org_id" required>
                <?php foreach($orgs as $o): if((int)($o['is_active']??1)!==1) continue; ?>
                  <option value="<?= e((string)$o['id']) ?>"><?= e((string)$o['name']) ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="col-md-5">
              <input class="form-control" name="email" type="email" placeholder="<?= e($t('بريد المستخدم','User email')) ?>" required>
            </div>
            <div class="col-md-2 d-grid">
              <button class="btn btn-success"><i class="fa-solid fa-plus"></i> <?= e($t('تعيين','Assign')) ?></button>
            </div>
          </form>
          <div class="text-muted small mt-2">
            <i class="fa-solid fa-circle-info"></i> <?= e($t('إذا لم يكن المستخدم موجودًا استخدم نموذج "إنشاء مستخدم جديد".','If user does not exist, use “Create New User”.')) ?>
          </div>
        </div>
      </div>
    </div>

    <div class="col-lg-6">
      <div class="card border-0 shadow-sm h-100">
        <div class="card-header bg-white fw-bold"><i class="fa-solid fa-user-plus"></i> <?= e($t('إنشاء مستخدم جديد + تعيينه Org Admin','Create New User + Assign Org Admin')) ?></div>
        <div class="card-body">
          <form method="post" action="<?= e(base_path('admin/owner/orgs/admin-add')) ?>" class="row g-2">
            <input type="hidden" name="mode" value="create">

            <div class="col-md-5">
              <select class="form-select" name="org_id" required>
                <?php foreach($orgs as $o): if((int)($o['is_active']??1)!==1) continue; ?>
                  <option value="<?= e((string)$o['id']) ?>"><?= e((string)$o['name']) ?></option>
                <?php endforeach; ?>
              </select>
            </div>

            <div class="col-md-7">
              <input class="form-control" name="full_name" placeholder="<?= e($t('الاسم الكامل','Full name')) ?>" required>
            </div>

            <div class="col-md-6">
              <input class="form-control" name="email" type="email" placeholder="<?= e($t('البريد الإلكتروني','Email')) ?>" required>
            </div>

            <div class="col-md-6">
              <input class="form-control" name="mobile" placeholder="<?= e($t('رقم الهاتف','Phone number')) ?>" required>
            </div>

            <div class="col-md-12">
              <input class="form-control" name="password" type="password" placeholder="<?= e($t('كلمة المرور','Password')) ?>" required>
            </div>

            <div class="col-12 d-grid mt-1">
              <button class="btn btn-primary"><i class="fa-solid fa-floppy-disk"></i> <?= e($t('إنشاء وتعيين','Create & Assign')) ?></button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>

  <div class="card border-0 shadow-sm mt-3">
    <div class="card-header bg-white fw-bold"><i class="fa-solid fa-users"></i> <?= e($t('Org Admins الحاليين','Current Org Admins')) ?></div>
    <div class="table-responsive">
      <table class="table table-hover mb-0">
        <thead class="table-light"><tr><th><?=e($t('المؤسسة','Org'))?></th><th><?=e($t('البريد','Email'))?></th><th><?=e($t('إجراء','Action'))?></th></tr></thead>
        <tbody>
        <?php foreach($admins as $a): ?>
          <tr>
            <td><?= e((string)$a['org_name']) ?></td>
            <td><?= e((string)$a['email']) ?></td>
            <td>
              <a class="btn btn-sm btn-outline-danger" href="<?= e(base_path('admin/owner/orgs/admin-remove?org_id='.(int)$a['org_id'].'&user_id='.(int)$a['user_id'])) ?>">
                <i class="fa-solid fa-trash"></i> <?= e($t('إلغاء','Remove')) ?>
              </a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if(empty($admins)): ?><tr><td colspan="3" class="text-center text-muted py-4">—</td></tr><?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

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