<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php $t=fn($ar,$en)=>($lang==='ar'?$ar:$en); ?>
<?php $conf=$conf??null; $blocks=$blocks??[]; $id=(int)($id??0); ?>

<?php
$slides = array_values(array_filter($blocks, fn($b)=>($b['block_type']??'')==='hero_slide'));
$panels = array_values(array_filter($blocks, fn($b)=>in_array(($b['block_type']??''),['panel','topic','section_html','important_date'],true)));
$title = $lang==='ar' ? (string)($conf['title_ar']??$conf['title']??'') : (string)($conf['title_en']??$conf['title']??'');
if($title==='') $title = $t('مؤتمر','Conference').' #'.$id;
?>
<div class="container py-3">

  <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
    <h3 class="mb-0"><?= e($title) ?></h3>
    <div class="d-flex gap-2">
      <a class="btn btn-outline-primary" href="<?= e(base_path('login.php')) ?>"><i class="fa-solid fa-right-to-bracket"></i> <?= e($t('دخول','Login')) ?></a>
      <a class="btn btn-outline-secondary" href="<?= e(base_path('admin/homepage-blocks?conference_id='.$id)) ?>"><i class="fa-solid fa-gear"></i> <?= e($t('إدارة الصفحة','Manage Homepage')) ?></a>
    </div>
  </div>

  <?php if(!empty($slides)): ?>
    <div class="card border-0 shadow-sm mb-3 overflow-hidden">
      <div id="ecCarousel" class="carousel slide" data-bs-ride="carousel">
        <div class="carousel-inner">
          <?php foreach($slides as $i=>$s): ?>
            <?php
              $h = ($lang==='ar'?(string)($s['title_ar']??''):(string)($s['title_en']??''));
              $b = ($lang==='ar'?(string)($s['body_ar']??''):(string)($s['body_en']??''));
              $img = (string)($s['image_path']??'');
              $active = $i===0 ? 'active' : '';
            ?>
            <div class="carousel-item <?= e($active) ?>">
              <?php if($img!==''): ?>
                <img src="<?= e($img) ?>" class="d-block w-100" style="max-height:360px;object-fit:cover">
              <?php endif; ?>
              <div class="carousel-caption d-none d-md-block" style="background:rgba(0,0,0,.45);border-radius:14px;padding:12px">
                <h5><?= e($h) ?></h5>
                <p><?= e($b) ?></p>
              </div>
            </div>
          <?php endforeach; ?>
        </div>
        <?php if(count($slides)>1): ?>
          <button class="carousel-control-prev" type="button" data-bs-target="#ecCarousel" data-bs-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
          </button>
          <button class="carousel-control-next" type="button" data-bs-target="#ecCarousel" data-bs-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
          </button>
        <?php endif; ?>
      </div>
    </div>
  <?php endif; ?>

  <div class="row g-3">
    <?php foreach($panels as $p): ?>
      <?php
        $h = ($lang==='ar'?(string)($p['title_ar']??''):(string)($p['title_en']??''));
        $b = ($lang==='ar'?(string)($p['body_ar']??''):(string)($p['body_en']??''));
        $img = (string)($p['image_path']??'');
        $link = (string)($p['link_url']??'');
      ?>
      <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
          <?php if($img!==''): ?>
            <img src="<?= e($img) ?>" style="width:100%;max-height:180px;object-fit:cover">
          <?php endif; ?>
          <div class="card-body">
            <div class="fw-bold mb-2"><?= e($h!==''?$h:'—') ?></div>
            <?php if($b!==''): ?><div class="text-muted"><?= nl2br(e($b)) ?></div><?php endif; ?>
            <?php if($link!==''): ?>
              <div class="mt-3">
                <a class="btn btn-sm btn-outline-primary" href="<?= e($link) ?>" target="_blank">
                  <i class="fa-solid fa-arrow-up-right-from-square"></i> <?= e($t('فتح','Open')) ?>
                </a>
              </div>
            <?php endif; ?>
          </div>
        </div>
      </div>
    <?php endforeach; ?>

    <?php if(empty($slides) && empty($panels)): ?>
      <div class="col-12">
        <div class="alert alert-info"><?= e($t('لا توجد بلوكات مفعّلة لهذا المؤتمر بعد.','No active blocks for this conference yet.')) ?></div>
      </div>
    <?php endif; ?>
  </div>

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