<?php
function rr_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$assignment = $assignment ?? [];
$criteria = $criteria ?? [];
$existingReview = $existingReview ?? null;
?>

<div class="container-fluid py-3">
    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body">
            <h3 class="fw-bold mb-1">نموذج التحكيم</h3>
            <div class="text-muted">
                <?= rr_h(($assignment['title'] ?? '') ?: ($assignment['title_en'] ?? '') ?: ($assignment['title_ar'] ?? '')) ?>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body">
            <h5 class="fw-bold">بيانات البحث</h5>
            <p><?= nl2br(rr_h(($assignment['abstract'] ?? '') ?: ($assignment['abstract_en'] ?? '') ?: ($assignment['abstract_ar'] ?? ''))) ?></p>

            <?php if (!empty($assignment['file_path'])): ?>
                <a class="btn btn-outline-primary" href="/easychair/storage/<?= rr_h($assignment['file_path']) ?>" target="_blank">
                    تحميل ملف البحث
                </a>
            <?php endif; ?>
        </div>
    </div>

<form method="post"
      action="/easychair/reviewer/submit-review"
      enctype="multipart/form-data">

    <input type="hidden"
       name="assignment_id"
       value="<?= (int)($assignment_id ?? 0) ?>">

        <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
            <div class="card-body">
                <h5 class="fw-bold mb-3">درجات المعايير</h5>

<?php foreach ($criteria as $c): ?>

<?php

$label =
    $c['label_en']
    ?? $c['title_en']
    ?? $c['label_ar']
    ?? $c['title_ar']
    ?? ('Criterion #' . $c['id']);

$maxScore =
    (float)($c['max_score'] ?? 10);

?>

<div class="mb-3">

<label class="form-label fw-bold">

<?= htmlspecialchars($label) ?>

<span class="text-muted">
(max <?= $maxScore ?>)
</span>

</label>

<input
    type="number"
    name="criteria[<?= (int)$c['id'] ?>]"
    class="form-control"
    min="0"
    max="<?= $maxScore ?>"
    step="0.1"
    required
>

</div>

<?php endforeach; ?>

                <?php if (!$criteria): ?>
                    <div class="alert alert-warning">لا توجد معايير معرفة. سيتم استخدام الدرجة العامة فقط.</div>
                <?php endif; ?>

                <div class="mb-3">
                    <label class="form-label fw-bold">الدرجة العامة</label>
                    <input class="form-control" type="number" step="0.01" name="overall_score" min="0" max="10" required>
                </div>

                <div class="mb-3">
                    <label class="form-label fw-bold">التوصية</label>
                    <select name="recommendation" class="form-select" required>
                        <option value="">اختر</option>
                        <option value="accept">قبول</option>
                        <option value="minor_revision">تعديلات بسيطة</option>
                        <option value="major_revision">تعديلات جوهرية</option>
                        <option value="reject">رفض</option>
                    </select>
                </div>

                <div class="mb-3">
                    <label class="form-label fw-bold">ملاحظات للمؤلف</label>
                    <textarea name="comments_to_author" class="form-control" rows="5"></textarea>
                </div>

                <div class="mb-3">
                    <label class="form-label fw-bold">ملاحظات للمحرر / الرئيس</label>
                    <textarea name="comments_to_editor" class="form-control" rows="5"></textarea>
                </div>

                <div class="mb-3">
                    <label class="form-label fw-bold">رفع ملف مراجعة اختياري</label>
                    <input type="file" name="review_file" class="form-control"
                           accept=".pdf,.doc,.docx,.zip,.rar,.png,.jpg,.jpeg">
                </div>

<?php if (!empty($criteria)): ?>

<hr>

<h4>
Review Form
</h4>

<?php foreach ($criteria as $c): ?>

<?php

$label =
    $c['label_en']
    ?? $c['label_ar']
    ?? ('Criterion #' . $c['id']);

$maxScore =
    (float)($c['max_score'] ?? 10);

?>

<div class="mb-3">

<label class="form-label fw-bold">

<?= htmlspecialchars($label) ?>

<span class="text-muted">
(max <?= $maxScore ?>)
</span>

</label>

<input
    type="number"
    name="criteria[<?= (int)$c['id'] ?>]"
    class="form-control"
    min="0"
    max="<?= $maxScore ?>"
    step="0.1"
    required
>

</div>

<?php endforeach; ?>

<?php endif; ?>
               
 <button class="btn btn-success btn-lg">
                    إرسال التحكيم
                </button>
            </div>
        </div>



    </form>
</div>
