<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php $c = $comm ?? []; $cid=(int)($c['id']??0); ?>
<div class="container">
  <div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
    <h4 class="mb-0"><i class="fa-solid fa-users me-2"></i><?= e($lang==='ar'?'أعضاء اللجنة':'Committee Members') ?> — <?= e($lang==='ar'?(string)($c['name_ar']??''):(string)($c['name_en']??'')) ?></h4>
    <a class="btn btn-outline-secondary btn-rounded" href="<?= e(base_path('admin/committees')) ?>"><?= e($lang==='ar'?'رجوع':'Back') ?></a>
  </div>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-body">
      <form method="post" action="<?= e(base_path('admin/committees/member-add')) ?>" enctype="multipart/form-data" class="row g-2">
        <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
        <input type="hidden" name="committee_id" value="<?= e((string)$cid) ?>">
        <div class="col-md-3"><input class="form-control" name="full_name_en" placeholder="Full name EN" required></div>
        <div class="col-md-3"><input class="form-control" name="full_name_ar" placeholder="الاسم AR" required></div>
        <div class="col-md-2"><input class="form-control" name="email" placeholder="email"></div>
        <div class="col-md-2"><input class="form-control" type="file" name="image"></div>
        <div class="col-md-2 d-grid"><button class="btn btn-primary"><?= e($lang==='ar'?'إضافة':'Add') ?></button></div>
      </form>
    </div>
  </div>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-body">
      <form method="post" action="<?= e(base_path('admin/committees/import-members')) ?>" enctype="multipart/form-data" class="d-flex gap-2 flex-wrap">
        <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
        <input type="hidden" name="committee_id" value="<?= e((string)$cid) ?>">
        <input class="form-control" type="file" name="csv" accept=".csv" required>
        <button class="btn btn-outline-primary"><?= e($lang==='ar'?'استيراد CSV للأعضاء':'Import Members CSV') ?></button>
        <span class="text-muted small">CSV: full_name_en, full_name_ar, title_en, title_ar, email</span>
      </form>
    </div>
  </div>

  <div class="card border-0 shadow-sm">
    <div class="card-body">
      <div class="table-responsive">
        <table class="table align-middle">
          <thead><tr><th>#</th><th><?= e($lang==='ar'?'الصورة':'Image') ?></th><th><?= e($lang==='ar'?'الاسم':'Name') ?></th><th>Email</th><th></th></tr></thead>
          <tbody>
            <?php foreach (($rows??[]) as $r): ?>
              <tr>
                <td><?= e((string)$r['id']) ?></td>
                <td><?php if(!empty($r['image_path'])): ?><img src="<?= e(base_path((string)$r['image_path'])) ?>" style="width:44px;height:44px;border-radius:50%;object-fit:cover"><?php endif; ?></td>
                <td class="fw-bold"><?= e($lang==='ar'?(string)$r['full_name_ar']:(string)$r['full_name_en']) ?></td>
                <td><?= e((string)($r['email'] ?? '')) ?></td>
                <td class="d-flex gap-2 flex-wrap">
                  <form method="post" action="<?= e(base_path('admin/committees/member-toggle')) ?>">
                    <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
                    <input type="hidden" name="id" value="<?= e((string)$r['id']) ?>">
                    <input type="hidden" name="committee_id" value="<?= e((string)$cid) ?>">
                    <button class="btn btn-sm btn-outline-secondary"><?= e($lang==='ar'?'تفعيل/تعطيل':'Toggle') ?></button>
                  </form>
                  <form method="post" action="<?= e(base_path('admin/committees/member-delete')) ?>">
                    <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
                    <input type="hidden" name="id" value="<?= e((string)$r['id']) ?>">
                    <input type="hidden" name="committee_id" value="<?= e((string)$cid) ?>">
                    <button class="btn btn-sm btn-outline-danger"><?= e($lang==='ar'?'حذف':'Delete') ?></button>
                  </form>
                </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>
</div>
<?php require __DIR__ . '/../../layouts/footer.php'; ?>
