<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php
$t=fn($ar,$en)=>($lang==='ar'?$ar:$en);
$confs=$confs??[]; $confId=(int)($confId??0);
$committees=$committees??[]; $membersByCommittee=$membersByCommittee??[];
?>
<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-people-group"></i> <?= e($t('اللجان','Committees')) ?></h4>
      <div class="text-muted small"><?= e($t('CRUD كامل لكل مؤسسة/مؤتمر مع رفع صور واستيراد CSV.','Full CRUD per org/conference with photos and CSV import.')) ?></div>
    </div>

    <form method="get">
      <select class="form-select" name="conference_id" onchange="this.form.submit()">
        <?php foreach($confs as $c): ?>
          <option value="<?= e((string)$c['id']) ?>" <?= ((int)$c['id']===$confId?'selected':'') ?>>
            <?= e($lang==='ar' ? (string)($c['title_ar']??'') : (string)($c['title_en']??'')) ?>
          </option>
        <?php endforeach; ?>
      </select>
    </form>
  </div>

  <!-- CSV Import -->
  <div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white fw-bold"><i class="fa-solid fa-file-csv"></i> <?= e($t('استيراد أعضاء من CSV','Import members from CSV')) ?></div>
    <div class="card-body">
      <div class="text-muted small mb-2">
        <?= e($t(
          'الأعمدة المطلوبة: committee_type, committee_title_en, committee_title_ar, full_name. أعمدة اختيارية: role_title, email, affiliation, sort_order, is_active',
          'Required columns: committee_type, committee_title_en, committee_title_ar, full_name. Optional: role_title, email, affiliation, sort_order, is_active'
        )) ?>
      </div>
      <form method="post" enctype="multipart/form-data" action="<?= e(base_path('admin/committees/import-members')) ?>" class="d-flex gap-2 flex-wrap">
        <input type="hidden" name="conference_id" value="<?= e((string)$confId) ?>">
        <input class="form-control" type="file" name="csv" accept=".csv,text/csv" required>
        <button class="btn btn-primary"><i class="fa-solid fa-upload"></i> <?= e($t('استيراد','Import')) ?></button>
      </form>
    </div>
  </div>

  <!-- Add / Edit Committee -->
  <div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white fw-bold"><i class="fa-solid fa-square-plus"></i> <?= e($t('إضافة/تعديل لجنة','Add/Edit Committee')) ?></div>
    <div class="card-body">
      <form method="post" action="<?= e(base_path('admin/committees/save-committee')) ?>" class="row g-2">
        <input type="hidden" name="id" value="0">
        <input type="hidden" name="conference_id" value="<?= e((string)$confId) ?>">

        <div class="col-md-3">
          <label class="form-label"><?= e($t('النوع','Type')) ?></label>
          <input class="form-control" name="committee_type" placeholder="tpc / chairs / reviewers / ..." value="committee">
        </div>

        <div class="col-md-3">
          <label class="form-label"><?= e($t('العنوان EN','Title EN')) ?></label>
          <input class="form-control" name="title_en">
        </div>

        <div class="col-md-3">
          <label class="form-label"><?= e($t('العنوان AR','Title AR')) ?></label>
          <input class="form-control" name="title_ar">
        </div>

        <div class="col-md-1">
          <label class="form-label"><?= e($t('ترتيب','Sort')) ?></label>
          <input class="form-control" name="sort_order" type="number" value="0">
        </div>

        <div class="col-md-1">
          <label class="form-label"><?= e($t('نشط','Active')) ?></label>
          <select class="form-select" name="is_active">
            <option value="1">ON</option>
            <option value="0">OFF</option>
          </select>
        </div>

        <div class="col-md-1 d-grid align-items-end">
          <button class="btn btn-success"><i class="fa-solid fa-floppy-disk"></i> <?= e($t('حفظ','Save')) ?></button>
        </div>

        <div class="col-12">
          <label class="form-label"><?= e($t('الوصف AR','Description AR')) ?></label>
          <input class="form-control" name="description_ar">
        </div>
        <div class="col-12">
          <label class="form-label"><?= e($t('الوصف EN','Description EN')) ?></label>
          <input class="form-control" name="description_en">
        </div>
      </form>
      <div class="text-muted small mt-2">
        <i class="fa-solid fa-circle-info"></i> <?= e($t('للتعديل استخدم زر "تعديل" داخل كل لجنة بالأسفل.','To edit, use the Edit button inside each committee below.')) ?>
      </div>
    </div>
  </div>

  <!-- Committees List -->
  <?php foreach($committees as $c): $cid=(int)$c['id']; $mem=$membersByCommittee[$cid]??[]; ?>
    <div class="card border-0 shadow-sm mb-3">
      <div class="card-header bg-white d-flex justify-content-between align-items-center flex-wrap gap-2">
        <div class="fw-bold">
          <i class="fa-solid fa-users"></i>
          <?= e((string)($lang==='ar'?($c['title_ar']??'') : ($c['title_en']??''))) ?>
          <span class="badge bg-secondary ms-2"><?= e((string)($c['committee_type']??'')) ?></span>
          <?= ((int)($c['is_active']??1)===1) ? '<span class="badge bg-success ms-2">ON</span>' : '<span class="badge bg-danger ms-2">OFF</span>' ?>
        </div>
        <div class="d-flex gap-2 flex-wrap">
          <a class="btn btn-sm btn-outline-danger" onclick="return confirm('<?= e($t('حذف اللجنة مع جميع أعضائها؟','Delete committee with all members?')) ?>')" href="<?= e(base_path('admin/committees/delete-committee?id='.$cid)) ?>">
            <i class="fa-solid fa-trash"></i> <?= e($t('حذف','Delete')) ?>
          </a>
        </div>
      </div>

      <div class="card-body">
        <?php if(!empty($c['description_ar']) || !empty($c['description_en'])): ?>
          <div class="text-muted small mb-2"><?= nl2br(e((string)($lang==='ar'?($c['description_ar']??''):($c['description_en']??'')))) ?></div>
        <?php endif; ?>

        <!-- Add member -->
        <div class="border rounded-4 p-3 mb-3">
          <div class="fw-bold mb-2"><i class="fa-solid fa-user-plus"></i> <?= e($t('إضافة عضو','Add Member')) ?></div>

          <form method="post" enctype="multipart/form-data" action="<?= e(base_path('admin/committees/save-member')) ?>" class="row g-2">
            <input type="hidden" name="id" value="0">
            <input type="hidden" name="committee_id" value="<?= e((string)$cid) ?>">

            <div class="col-md-3">
              <input class="form-control" name="full_name" placeholder="<?= e($t('الاسم الكامل','Full name')) ?>" required>
            </div>
            <div class="col-md-2">
              <input class="form-control" name="role_title" placeholder="<?= e($t('الصفة/الدور','Role title')) ?>">
            </div>
            <div class="col-md-2">
              <input class="form-control" name="email" placeholder="<?= e($t('البريد','Email')) ?>">
            </div>
            <div class="col-md-2">
              <input class="form-control" name="affiliation" placeholder="<?= e($t('الجهة/الكلية','Affiliation')) ?>">
            </div>
            <div class="col-md-1">
              <input class="form-control" name="sort_order" type="number" value="0" title="sort">
            </div>
            <div class="col-md-1">
              <select class="form-select" name="is_active">
                <option value="1">ON</option>
                <option value="0">OFF</option>
              </select>
            </div>
            <div class="col-md-1">
              <input class="form-control" type="file" name="photo" accept="image/*" title="photo">
            </div>

            <div class="col-12 d-grid mt-1">
              <button class="btn btn-success"><i class="fa-solid fa-floppy-disk"></i> <?= e($t('حفظ','Save')) ?></button>
            </div>
          </form>
        </div>

        <!-- members table -->
        <div class="table-responsive">
          <table class="table table-hover align-middle mb-0">
            <thead class="table-light">
              <tr>
                <th>#</th>
                <th><?= e($t('صورة','Photo')) ?></th>
                <th><?= e($t('الاسم','Name')) ?></th>
                <th><?= e($t('الدور','Role')) ?></th>
                <th><?= e($t('البريد','Email')) ?></th>
                <th><?= e($t('الجهة','Affiliation')) ?></th>
                <th><?= e($t('ترتيب','Sort')) ?></th>
                <th><?= e($t('حالة','Active')) ?></th>
                <th><?= e($t('إجراء','Action')) ?></th>
              </tr>
            </thead>
            <tbody>
              <?php foreach($mem as $m): ?>
                <tr>
                  <td class="text-muted"><?= e((string)$m['id']) ?></td>
                  <td>
                    <?php if(!empty($m['photo_path'])): ?>
                      <img src="<?= e((string)$m['photo_path']) ?>" style="width:44px;height:44px;object-fit:cover;border-radius:12px">
                    <?php else: ?>
                      <span class="text-muted">—</span>
                    <?php endif; ?>
                  </td>
                  <td class="fw-semibold"><?= e((string)$m['full_name']) ?></td>
                  <td><?= e((string)($m['role_title']??'')) ?></td>
                  <td class="text-muted"><?= e((string)($m['email']??'')) ?></td>
                  <td class="text-muted"><?= e((string)($m['affiliation']??'')) ?></td>
                  <td><?= e((string)$m['sort_order']) ?></td>
                  <td><?= ((int)$m['is_active']===1)?'<span class="badge bg-success">ON</span>':'<span class="badge bg-danger">OFF</span>' ?></td>
                  <td>
                    <a class="btn btn-sm btn-outline-danger" onclick="return confirm('<?= e($t('حذف العضو؟','Delete member?')) ?>')" href="<?= e(base_path('admin/committees/delete-member?id='.(int)$m['id'])) ?>">
                      <i class="fa-solid fa-trash"></i> <?= e($t('حذف','Delete')) ?>
                    </a>
                  </td>
                </tr>
              <?php endforeach; ?>
              <?php if(empty($mem)): ?><tr><td colspan="9" class="text-center text-muted py-3">—</td></tr><?php endif; ?>
            </tbody>
          </table>
        </div>

      </div>
    </div>
  <?php endforeach; ?>

  <?php if(empty($committees)): ?>
    <div class="text-center text-muted py-4">—</div>
  <?php endif; ?>

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