<?php require __DIR__ . '/../../layouts/header.php'; $page_title = trans('committees_manage'); ?>
<?php $isEdit = !empty($row); ?>

<div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
  <h3 class="mb-0"><i class="fa-solid fa-people-group me-2"></i><?= e($isEdit ? trans('edit') : trans('add_new')) ?></h3>
  <a class="btn btn-outline-secondary btn-rounded" href="<?= e(base_path('admin/committees')) ?>"><i class="fa-solid fa-arrow-left me-2"></i><?= e(trans('back')) ?></a>
</div>

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

      <div class="col-md-6">
        <label class="form-label ec-required"><?= e(trans('conference')) ?></label>
        <select name="conference_id" class="form-select" required>
          <option value="0">--</option>
          <?php foreach ($conferences as $c): ?>
            <option value="<?= e((string)$c['id']) ?>" <?= ((int)($row['conference_id']??0)===(int)$c['id'])?'selected':'' ?>>
              <?= e(current_lang()==='ar' ? (string)($c['title_ar']??'') : (string)($c['title_en']??'')) ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="col-md-6">
        <label class="form-label"><?= e(trans('committee_type')) ?></label>
        <input name="committee_type" class="form-control" value="<?= e((string)($row['committee_type'] ?? 'TPC')) ?>">
      </div>

      <div class="col-md-6">
        <label class="form-label ec-required"><?= e(trans('full_name')) ?></label>
        <input name="full_name" class="form-control" value="<?= e((string)($row['full_name'] ?? '')) ?>" required>
      </div>

      <div class="col-md-6">
        <label class="form-label">Email</label>
        <input name="email" class="form-control" value="<?= e((string)($row['email'] ?? '')) ?>">
      </div>

      <div class="col-md-8">
        <label class="form-label"><?= e(trans('affiliation')) ?></label>
        <input name="affiliation" class="form-control" value="<?= e((string)($row['affiliation'] ?? '')) ?>">
      </div>

      <div class="col-md-4">
        <label class="form-label"><?= e(trans('sort_order')) ?></label>
        <input type="number" name="sort_order" class="form-control" value="<?= e((string)($row['sort_order'] ?? 0)) ?>">
      </div>

      <div class="col-md-6">
        <label class="form-label"><?= e(trans('image')) ?></label>
        <input type="file" name="image" class="form-control" accept="image/*">
        <?php if (!empty($row['image_path'])): ?>
          <div class="small text-muted mt-1">Current: <code><?= e((string)$row['image_path']) ?></code></div>
        <?php endif; ?>
      </div>

      <div class="col-md-6 d-flex align-items-end">
        <div class="form-check">
          <input class="form-check-input" type="checkbox" name="is_active" <?= empty($row) || !empty($row['is_active']) ? 'checked':'' ?>>
          <label class="form-check-label"><?= e(trans('enabled')) ?></label>
        </div>
      </div>

      <div class="col-12 d-flex gap-2">
        <button class="btn btn-success btn-rounded"><i class="fa-solid fa-check me-2"></i><?= e(trans('save')) ?></button>

        <?php if ($isEdit): ?>
          <form method="post" action="<?= e(base_path('admin/committees/delete')) ?>" onsubmit="return confirm('Delete member?');">
            <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
            <input type="hidden" name="id" value="<?= e((string)$row['id']) ?>">
            <button class="btn btn-outline-danger btn-rounded"><i class="fa-solid fa-trash me-2"></i><?= e(trans('delete')) ?></button>
          </form>
        <?php endif; ?>
      </div>
    </form>
  </div>
</div>

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