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

<div class="container-fluid py-3">
  <div class="d-flex align-items-center justify-content-between flex-wrap gap-2 mb-3">
    <h4 class="mb-0"><?= e(t('sessions') ?? 'Sessions') ?></h4>

    <div class="d-flex gap-2">
      <form method="get" action="<?= e(base_path('action/sessions')) ?>" class="d-flex gap-2">
        <select name="conference_id" class="form-select" onchange="this.form.submit()">
          <?php foreach(($confs??[]) as $c): ?>
            <option value="<?= (int)$c['id'] ?>" <?= ((int)$confId===(int)$c['id'])?'selected':'' ?>>
              <?= e(($c['title_ar']??'') ?: ($c['title_en']??('Conference #'.$c['id']))) ?>
            </option>
          <?php endforeach; ?>
        </select>
      </form>

      <a class="btn btn-outline-secondary" href="<?= e(base_path('admin/schedule?conference_id='.(int)$confId)) ?>">
        <i class="fa fa-calendar"></i> <?= e(t('view_schedule') ?? 'View Schedule') ?>
      </a>
    </div>
  </div>

  <div class="card shadow-sm border-0 mb-3">
    <div class="card-body">
      <form method="post" action="<?= e(base_path('action/sessions')) ?>" class="row g-2">
        <input type="hidden" name="id" value="0">
        <input type="hidden" name="conference_id" value="<?= (int)$confId ?>">

        <div class="col-md-2">
          <label class="form-label"><?= e(t('date') ?? 'Date') ?></label>
          <input type="date" name="session_date" class="form-control">
        </div>

        <div class="col-md-3">
          <label class="form-label"><?= e(t('program_day') ?? 'Program Day') ?></label>
          <select name="program_day_id" class="form-select">
            <option value="0"><?= e(t('auto') ?? 'Auto (by date)') ?></option>
            <?php foreach(($programDays ?? []) as $pd): ?>
              <option value="<?= (int)$pd['id'] ?>"><?= e((string)($pd['d'] ?? '')) ?></option>
            <?php endforeach; ?>
          </select>
        </div>


        <div class="col-md-2">
          <label class="form-label"><?= e(t('start_time') ?? 'Start') ?></label>
          <input type="time" name="start_time" class="form-control" required>
        </div>

        <div class="col-md-2">
          <label class="form-label"><?= e(t('end_time') ?? 'End') ?></label>
          <input type="time" name="end_time" class="form-control" required>
        </div>

        <div class="col-md-2">
          <label class="form-label"><?= e(t('type') ?? 'Type') ?></label>
          <select name="session_type" class="form-select">
            <?php foreach(['opening','keynote','panel','paper_session','workshop','break','closing','other'] as $k): ?>
              <option value="<?= e($k) ?>"><?= e($k) ?></option>
            <?php endforeach; ?>
          </select>
        </div>

        <div class="col-md-4">
          <label class="form-label"><?= e(t('title_en') ?? 'Title (EN)') ?></label>
          <input type="text" name="title_en" class="form-control" required>
        </div>

        <div class="col-md-4">
          <label class="form-label"><?= e(t('title_ar') ?? 'Title (AR)') ?></label>
          <input type="text" name="title_ar" class="form-control">
        </div>

        <div class="col-md-3">
          <label class="form-label"><?= e(t('room') ?? 'Room') ?></label>
          <input type="text" name="room_name" class="form-control">
        </div>

        <div class="col-md-3">
          <label class="form-label"><?= e(t('chair_en') ?? 'Chair (EN)') ?></label>
          <input type="text" name="chair_name_en" class="form-control">
        </div>

        <div class="col-md-2">
          <label class="form-label"><?= e(t('active') ?? 'Active') ?></label>
          <select name="is_active" class="form-select">
            <option value="1" selected><?= e(t('yes') ?? 'Yes') ?></option>
            <option value="0"><?= e(t('no') ?? 'No') ?></option>
          </select>
        </div>

        <div class="col-12 d-flex justify-content-end gap-2 mt-2">
          <button class="btn btn-primary">
            <i class="fa fa-save"></i> <?= e(t('save') ?? 'Save') ?>
          </button>
        </div>
      </form>
    </div>
  </div>

  <div class="card shadow-sm border-0">
    <div class="card-body">
      <div class="table-responsive">
        <table class="table table-hover align-middle">
          <thead>
            <tr>
              <th><?= e(t('date') ?? 'Date') ?></th>
              <th><?= e(t('time') ?? 'Time') ?></th>
              <th><?= e(t('title') ?? 'Title') ?></th>
              <th><?= e(t('type') ?? 'Type') ?></th>
              <th><?= e(t('room') ?? 'Room') ?></th>
              <th><?= e(t('status') ?? 'Status') ?></th>
              <th class="text-end"><?= e(t('actions') ?? 'Actions') ?></th>
            </tr>
          </thead>
          <tbody>
            <?php if(empty($sessions)): ?>
              <tr><td colspan="7" class="text-center text-muted py-4">—</td></tr>
            <?php else: foreach($sessions as $s): ?>
              <tr>
                <td><?= e((string)($s['session_date'] ?? '')) ?></td>
                <td class="text-nowrap"><?= e((string)$s['start_time']) ?> - <?= e((string)$s['end_time']) ?></td>
                <td>
                  <div class="fw-semibold"><?= e((string)$s['title_en']) ?></div>
                  <?php if(!empty($s['title_ar'])): ?><div class="text-muted small"><?= e((string)$s['title_ar']) ?></div><?php endif; ?>
                </td>
                <td><?= e((string)$s['session_type']) ?></td>
                <td><?= e((string)($s['room_name'] ?? '')) ?></td>
                <td><?= ((int)$s['is_active']===1) ? '<span class="badge bg-success">'.e(t('active')??'Active').'</span>' : '<span class="badge bg-secondary">'.e(t('inactive')??'Inactive').'</span>' ?></td>
                <td class="text-end">
                  <a class="btn btn-sm btn-outline-danger" href="<?= e(base_path('admin/sessions/delete?id='.(int)$s['id'])) ?>" onclick="return confirm('Delete?')">
                    <i class="fa fa-trash"></i> <?= e(t('delete') ?? 'Delete') ?>
                  </a>
                </td>
              </tr>
            <?php endforeach; endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

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