<?php require __DIR__ . '/../../layouts/header.php'; $page_title = trans('reviewers_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-user-check me-2"></i><?= e($isEdit ? trans('edit') : trans('add_new')) ?></h3>
  <a class="btn btn-outline-secondary btn-rounded" href="<?= e(base_path('admin/reviewers')) ?>"><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/reviewers/save')) ?>" 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-4">
        <label class="form-label ec-required"><?= e(trans('first_name')) ?></label>
        <input name="first_name" class="form-control" value="<?= e((string)($row['first_name'] ?? '')) ?>" required>
      </div>
      <div class="col-md-4">
        <label class="form-label ec-required"><?= e(trans('last_name')) ?></label>
        <input name="last_name" class="form-control" value="<?= e((string)($row['last_name'] ?? '')) ?>" required>
      </div>
      <div class="col-md-4">
        <label class="form-label ec-required">Email</label>
        <input name="email" class="form-control" value="<?= e((string)($row['email'] ?? '')) ?>" required>
      </div>

      <div class="col-md-3">
        <label class="form-label"><?= e(trans('preferred_lang')) ?></label>
        <select name="preferred_lang" class="form-select">
          <option value="en" <?= ((string)($row['preferred_lang'] ?? 'en')==='en')?'selected':'' ?>>EN</option>
          <option value="ar" <?= ((string)($row['preferred_lang'] ?? 'en')==='ar')?'selected':'' ?>>AR</option>
        </select>
      </div>

      <div class="col-md-3">
        <label class="form-label"><?= e(trans('admin_rating')) ?></label>
        <input type="number" name="admin_rating" class="form-control" min="0" max="5" value="<?= e((string)($profile['admin_rating'] ?? 0)) ?>">
      </div>

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

      <div class="col-md-12">
        <label class="form-label"><?= e(trans('expertise_keywords')) ?></label>
        <input name="expertise_keywords" class="form-control" value="<?= e((string)($profile['expertise_keywords'] ?? '')) ?>" placeholder="AI, NLP, Networks...">
      </div>

      <div class="col-md-12">
        <label class="form-label"><?= e(trans('notes')) ?></label>
        <textarea name="notes" class="form-control" rows="3"><?= e((string)($profile['notes'] ?? '')) ?></textarea>
      </div>

      <div class="col-md-6">
        <label class="form-label"><?= e(trans('password')) ?> (<?= e(current_lang()==='ar'?'اختياري':'optional') ?>)</label>
        <input name="password" class="form-control" type="password" autocomplete="new-password">
        <div class="small text-muted mt-1">If empty on edit, password unchanged.</div>
      </div>

      <div class="col-md-6 d-flex align-items-end gap-2">
        <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/reviewers/delete')) ?>" onsubmit="return confirm('Delete reviewer?');">
            <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'; ?>
