<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php
$mode = (string)($mode ?? 'create');
$r = $row ?? [];
$id = (int)($r['id'] ?? 0);
$action = ($mode==='edit') ? base_path('admin/conferences/update') : base_path('admin/conferences/store');
?>
<div class="container" style="max-width:1050px">
  <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-pen-to-square me-2"></i><?= e($mode==='edit' ? ($lang==='ar'?'تعديل مؤتمر':'Edit Conference') : ($lang==='ar'?'إضافة مؤتمر':'Create Conference')) ?></h4>
    <a class="btn btn-outline-secondary btn-rounded" href="<?= e(base_path('admin/conferences')) ?>"><?= e($lang==='ar'?'رجوع':'Back') ?></a>
    <?php if ($mode==='edit'): ?>
      <a class="btn btn-outline-primary btn-rounded" href="<?= e(base_path('admin/conferences/logo?id='.(int)$id)) ?>"><i class="fa-solid fa-image me-2"></i><?= e($lang==='ar'?'شعار المؤتمر':'Logo') ?></a>
    <?php endif; ?>
  </div>

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

  <div class="card border-0 shadow-sm">
    <div class="card-body">
      <form method="post" action="<?= e($action) ?>" class="row g-3">
        <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
        <?php if ($mode==='edit'): ?><input type="hidden" name="id" value="<?= e((string)$id) ?>"><?php endif; ?>

        <div class="col-md-6">
          <label class="form-label"><?= e($lang==='ar'?'العنوان (EN)':'Title (EN)') ?></label>
          <input class="form-control" name="title_en" value="<?= e((string)($r['title_en'] ?? '')) ?>" required>
        </div>
        <div class="col-md-6">
          <label class="form-label"><?= e($lang==='ar'?'العنوان (AR)':'Title (AR)') ?></label>
          <input class="form-control" name="title_ar" value="<?= e((string)($r['title_ar'] ?? '')) ?>" required>
        </div>

        <div class="col-md-6">
          <label class="form-label"><?= e($lang==='ar'?'نبذة (EN)':'Brief (EN)') ?></label>
          <textarea class="form-control" name="brief_en" rows="4"><?= e((string)($r['brief_en'] ?? '')) ?></textarea>
        </div>
        <div class="col-md-6">
          <label class="form-label"><?= e($lang==='ar'?'نبذة (AR)':'Brief (AR)') ?></label>
          <textarea class="form-control" name="brief_ar" rows="4"><?= e((string)($r['brief_ar'] ?? '')) ?></textarea>
        </div>

        <div class="col-md-6">
          <label class="form-label"><?= e($lang==='ar'?'CFP (EN)':'CFP (EN)') ?></label>
          <textarea class="form-control" name="cfp_en" rows="4"><?= e((string)($r['cfp_en'] ?? '')) ?></textarea>
        </div>
        <div class="col-md-6">
          <label class="form-label"><?= e($lang==='ar'?'CFP (AR)':'CFP (AR)') ?></label>
          <textarea class="form-control" name="cfp_ar" rows="4"><?= e((string)($r['cfp_ar'] ?? '')) ?></textarea>
        </div>

        <div class="col-md-4">
          <label class="form-label">Short Name</label>
          <input class="form-control" name="short_name" value="<?= e((string)($row['short_name']??'')) ?>" placeholder="e.g. jpu2026">
        </div>
<div class="mb-4">
    <label class="form-label">Organization</label>

    <select name="org_id" class="form-select" required>

        <option value="">Select Organization</option>

        <?php foreach (($organizations ?? []) as $org): ?>
            <option value="<?= (int)$org['id'] ?>">
                <?= htmlspecialchars($org['name']) ?>
            </option>
        <?php endforeach; ?>

    </select>
</div>    

        <div class="col-md-4">
          <label class="form-label"><?= e($lang==='ar'?'الموقع الإلكتروني':'Website') ?></label>
          <input class="form-control" name="website" value="<?= e((string)($r['website'] ?? '')) ?>">
        </div>
        <div class="col-md-4">
          <label class="form-label"><?= e($lang==='ar'?'المكان':'Location') ?></label>
          <input class="form-control" name="location" value="<?= e((string)($r['location'] ?? '')) ?>">
        </div>
        <div class="col-md-2">
          <label class="form-label"><?= e($lang==='ar'?'بداية':'Start') ?></label>
          <input class="form-control" type="date" name="start_date" value="<?= e((string)($r['start_date'] ?? '')) ?>">
        </div>
        <div class="col-md-2">
          <label class="form-label"><?= e($lang==='ar'?'نهاية':'End') ?></label>
          <input class="form-control" type="date" name="end_date" value="<?= e((string)($r['end_date'] ?? '')) ?>">
        </div>

        <div class="col-md-3">
          <label class="form-label"><?= e($lang==='ar'?'مفعل':'Active') ?></label>
          <select class="form-select" name="is_active">
            <option value="1" <?= ((int)($r['is_active'] ?? 1)===1?'selected':'') ?>>1</option>
            <option value="0" <?= ((int)($r['is_active'] ?? 1)===0?'selected':'') ?>>0</option>
          </select>
        </div>

        <div class="col-12 d-flex gap-2">
          <button class="btn btn-primary btn-rounded"><i class="fa-solid fa-floppy-disk me-2"></i><?= e($lang==='ar'?'حفظ':'Save') ?></button>
        </div>
      </form>
    </div>
  </div>
</div>
<?php require __DIR__ . '/../../layouts/footer.php'; ?>
