<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php
$t=fn($ar,$en)=>($lang==='ar'?$ar:$en);
$row=$row??null; $conf=$conf??null; $confId=(int)($confId??0);
$isEdit = !empty($row);
?>
<div class="container py-3">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0"><i class="fa-solid fa-layer-group"></i> <?= e($isEdit?$t('تعديل بلوك','Edit Block'):$t('إضافة بلوك','Add Block')) ?></h4>
    <a class="btn btn-outline-secondary" href="<?= e(base_path('admin/homepage-blocks?conference_id='.$confId)) ?>"><i class="fa-solid fa-arrow-right"></i> <?= e($t('عودة','Back')) ?></a>
  </div>

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

        <div class="col-md-4">
          <label class="form-label"><?= e($t('نوع البلوك','Block Type')) ?></label>
          <select class="form-select" name="block_type">
            <?php
              $types=['hero_slide','panel','topic','section_html','important_date','committee'];
              $cur=(string)($row['block_type']??'panel');
              foreach($types as $tp){
                echo '<option value="'.e($tp).'" '.($tp===$cur?'selected':'').'>'.e($tp).'</option>';
              }
            ?>
          </select>
        </div>

        <div class="col-md-4">
          <label class="form-label"><?= e($t('مفتاح اختياري','Optional Key')) ?></label>
          <input class="form-control" name="block_key" value="<?= e((string)($row['block_key']??'')) ?>" placeholder="e.g. slider_1">
        </div>

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

        <div class="col-md-2">
          <label class="form-label"><?= e($t('الحالة','Active')) ?></label>
          <select class="form-select" name="is_active">
            <option value="1" <?= ((int)($row['is_active']??1)===1?'selected':'') ?>>ON</option>
            <option value="0" <?= ((int)($row['is_active']??1)!==1?'selected':'') ?>>OFF</option>
          </select>
        </div>

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

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

        <div class="col-md-12">
          <label class="form-label"><?= e($t('الرابط','Link URL')) ?></label>
          <input class="form-control" name="link_url" value="<?= e((string)($row['link_url']??'')) ?>" placeholder="https://...">
        </div>

        <div class="col-md-12">
          <label class="form-label"><?= e($t('المحتوى (EN)','Body (EN)')) ?></label>
          <textarea class="form-control" name="body_en" rows="4"><?= e((string)($row['body_en']??'')) ?></textarea>
        </div>

        <div class="col-md-12">
          <label class="form-label"><?= e($t('المحتوى (AR)','Body (AR)')) ?></label>
          <textarea class="form-control" name="body_ar" rows="4"><?= e((string)($row['body_ar']??'')) ?></textarea>
        </div>

        <div class="col-md-12">
          <label class="form-label"><?= e($t('صورة (للـ Slider أو Panels)','Image (for slider/panels)')) ?></label>
          <input class="form-control" type="file" name="image" accept="image/*">
          <?php if(!empty($row['image_path'])): ?>
            <div class="mt-2">
              <img src="<?= e((string)$row['image_path']) ?>" style="width:220px;height:120px;object-fit:cover;border-radius:14px">
            </div>
          <?php endif; ?>
        </div>

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

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