<?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('homepage_blocks') ?? 'Homepage Blocks') ?></h4>

    <div class="d-flex gap-2">
      <form method="get" action="<?= e(base_path('action/blocks')) ?>" 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/homepage?conference_id='.(int)$confId)) ?>">
        <i class="fa fa-arrow-left"></i> <?= e(t('back') ?? 'Back') ?>
      </a>
    </div>
  </div>

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

        <div class="col-md-2">
          <label class="form-label"><?= e(t('type') ?? 'Type') ?></label>
          <select name="block_type" class="form-select">
            <?php foreach(['hero_slide','panel','topic','section_html','important_date'] as $t): ?>
              <option value="<?= e($t) ?>"><?= e($t) ?></option>
            <?php endforeach; ?>
          </select>
        </div>

        <div class="col-md-2">
          <label class="form-label"><?= e(t('key') ?? 'Key') ?></label>
          <input type="text" name="block_key" class="form-control" placeholder="optional">
        </div>

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

        <div class="col-md-3">
          <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-2">
          <label class="form-label"><?= e(t('order') ?? 'Order') ?></label>
          <input type="number" name="sort_order" class="form-control" value="0">
        </div>

        <div class="col-md-6">
          <label class="form-label"><?= e(t('body_en') ?? 'Body (EN)') ?></label>
          <textarea name="body_en" class="form-control" rows="4"></textarea>
        </div>

        <div class="col-md-6">
          <label class="form-label"><?= e(t('body_ar') ?? 'Body (AR)') ?></label>
          <textarea name="body_ar" class="form-control" rows="4"></textarea>
        </div>

        <div class="col-md-4">
          <label class="form-label"><?= e(t('image') ?? 'Image') ?></label>
          <input type="file" name="image" class="form-control" accept="image/*">
        </div>

        <div class="col-md-4">
          <label class="form-label"><?= e(t('link') ?? 'Link') ?></label>
          <input type="url" name="link_url" class="form-control" placeholder="https://">
        </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-md-2 d-flex align-items-end justify-content-end">
          <button class="btn btn-primary w-100">
            <i class="fa fa-save"></i> <?= e(t('save') ?? 'Save') ?>
          </button>
        </div>
      </form>
      <div class="small text-muted mt-2">DB table: <?= e((string)($tbl ?? '')) ?></div>
    </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('order') ?? 'Order') ?></th>
              <th><?= e(t('type') ?? 'Type') ?></th>
              <th><?= e(t('title') ?? 'Title') ?></th>
              <th><?= e(t('image') ?? 'Image') ?></th>
              <th><?= e(t('active') ?? 'Active') ?></th>
              <th class="text-end"><?= e(t('actions') ?? 'Actions') ?></th>
            </tr>
          </thead>
          <tbody>
            <?php if(empty($blocks)): ?>
              <tr><td colspan="6" class="text-center text-muted py-4">—</td></tr>
            <?php else: foreach($blocks as $b): ?>
              <tr>
                <td><?= (int)($b['sort_order'] ?? 0) ?></td>
                <td><?= e((string)($b['block_type'] ?? '')) ?></td>
                <td>
                  <div class="fw-semibold"><?= e((string)($b['title_ar'] ?? '')) ?></div>
                  <div class="text-muted small"><?= e((string)($b['title_en'] ?? '')) ?></div>
                </td>
                <td>
                  <?php if(!empty($b['image_path'])): ?>
                    <a target="_blank" href="<?= e(base_url((string)$b['image_path'])) ?>"><?= e(t('view') ?? 'View') ?></a>
                  <?php else: ?>
                    <span class="text-muted">—</span>
                  <?php endif; ?>
                </td>
                <td><?= ((int)($b['is_active'] ?? 1)===1) ? '<span class="badge bg-success">'.e(t('yes')??'Yes').'</span>' : '<span class="badge bg-secondary">'.e(t('no')??'No').'</span>' ?></td>
                <td class="text-end">
                  <a class="btn btn-sm btn-outline-danger" href="<?= e(base_path('admin/homepage-blocks/delete?id='.(int)$b['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'; ?>
