<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php $cid=(int)($conference_id??0); $confs=$confs??[]; $rows=$rows??[]; ?>
<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-chalkboard-user me-2"></i><?= e($lang==='ar'?'الجلسات':'Sessions') ?></h4>
  </div>

  <?php if (!empty($err)): ?>
    <div class="alert alert-danger"><?= e((string)$err) ?></div>
  <?php endif; ?>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-body">
      <form method="get" class="row g-2 align-items-end">
        <div class="col-md-8">
          <label class="form-label"><?= e($lang==='ar'?'اختر المؤتمر':'Select conference') ?></label>
          <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']===$cid?'selected':'') ?>>
                #<?= e((string)$c['id']) ?> — <?= e($lang==='ar'?(string)($c['title_ar']??''):(string)($c['title_en']??'')) ?>
              </option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-2"><button class="btn btn-outline-secondary w-100"><?= e($lang==='ar'?'تحديث':'Refresh') ?></button></div>
      </form>
    </div>
  </div>

  <?php if ($cid<=0): ?>
    <div class="alert alert-info"><?= e($lang==='ar'?'لا يوجد مؤتمر افتراضي. اختر مؤتمرًا من القائمة.':'No default conference. Select one from the list.') ?></div>
  <?php else: ?>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-body">
      <form method="post" action="<?= e(base_path('admin/sessions/save')) ?>" class="row g-2">
        <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
        <input type="hidden" name="conference_id" value="<?= e((string)$cid) ?>">
        <input type="hidden" name="id" value="0">
        <div class="col-md-3"><input class="form-control" name="title_en" placeholder="Title EN" required></div>
        <div class="col-md-3"><input class="form-control" name="title_ar" placeholder="العنوان AR" required></div>
        <div class="col-md-2"><input class="form-control" name="room_name" placeholder="<?= e($lang==='ar'?'قاعة':'Room') ?>"></div>
        <div class="col-md-2"><input class="form-control" type="date" name="session_date"></div>
        <div class="col-md-1"><input class="form-control" type="time" name="start_time"></div>
        <div class="col-md-1"><input class="form-control" type="time" name="end_time"></div>
        <div class="col-12 d-flex gap-2">
          <button class="btn btn-primary"><?= e($lang==='ar'?'إضافة جلسة':'Add session') ?></button>
        </div>
      </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'?'العنوان':'Title') ?></th><th><?= e($lang==='ar'?'الموعد':'Date/Time') ?></th><th><?= e($lang==='ar'?'قاعة':'Room') ?></th><th></th></tr></thead>
          <tbody>
          <?php foreach($rows as $r): ?>
            <tr>
              <td><?= e((string)$r['id']) ?></td>
              <td class="fw-bold"><?= e($lang==='ar'?(string)$r['title_ar']:(string)$r['title_en']) ?></td>
              <td class="text-muted"><?= e((string)($r['session_date']??'')) ?> <?= e((string)($r['start_time']??'')) ?>-<?= e((string)($r['end_time']??'')) ?></td>
              <td><?= e((string)($r['room_name']??'')) ?></td>
              <td>
                <form method="post" action="<?= e(base_path('admin/sessions/delete')) ?>">
                  <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
                  <input type="hidden" name="id" value="<?= e((string)$r['id']) ?>">
                  <input type="hidden" name="conference_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>

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