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

<?php
$lang = function_exists('current_lang')
    ? current_lang()
    : ($_SESSION['lang'] ?? 'en');

$isRtl = $lang === 'ar';
?>

<div class="alert alert-info rounded-4">

<strong>
<?= $isRtl
    ? 'جولة التحكيم'
    : 'Review Round' ?>
:</strong>

<?= (int)($assignment['round_no'] ?? 1) ?>

</div>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="page-title"><?= e(trans('review_assignments')) ?></h1>

        <div class="text-muted">
            <?= e((string) ($assignment['submission_no'] ?? '')) ?>
            —
            <?= e((string) ($assignment['title'] ?? '')) ?>
        </div>
    </div>

    <a href="<?= e(base_path('reviewer/assignments')) ?>"
       class="btn btn-outline-secondary rounded-3">
        <?= e(trans('back')) ?>
    </a>
</div>

<div class="card border-0 shadow-sm rounded-4"
     dir="<?= $isRtl ? 'rtl' : 'ltr' ?>">
    <div class="card-body p-4">

        <div class="section-title">
            <span class="dot"></span>
            <h2 class="h5 mb-0">
                <?= e(trans('review_guidelines')) ?>
            </h2>
        </div>

        <div>
            <?= current_lang() === 'ar'
                ? (string) ($assignment['review_instructions_ar'] ?? '')
                : (string) ($assignment['review_instructions_en'] ?? '') ?>
        </div>

    </div>
</div>

<div class="card border-0 shadow-sm rounded-4">
    <div class="card-body p-4">

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

            <input type="hidden"
                   name="_csrf"
                   value="<?= e(csrf_token()) ?>">

            <input type="hidden"
                   name="assignment_id"
                   value="<?= e((string) $assignment['id']) ?>">

            <!-- REVIEW CRITERIA -->

<div class="mb-4">

    <div class="d-flex justify-content-between align-items-center mb-4">

        <h5 class="fw-bold mb-0">
            <i class="fa fa-star text-warning me-2"></i>
            Review Criteria
        </h5>

        <div class="badge bg-primary rounded-pill px-3 py-2">

            <?= count($criteria ?? []) ?>
            Criteria

        </div>

    </div>

    <div class="row g-3">

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

            <?php

            $criterionTitle =
                $lang === 'ar'
                ? ($c['title_ar'] ?? $c['title_en'] ?? 'معيار')
                : ($c['title_en'] ?? $c['title_ar'] ?? 'Criterion');

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

            $weight =
                (float)($c['weight'] ?? 1);

            ?>

            <div class="col-lg-6">

                <div class="card border-0 shadow-sm rounded-4 h-100 reviewer-score-card">

                    <div class="card-body p-3">

                        <div class="d-flex justify-content-between align-items-start mb-3">

                            <div>

                                <div class="fw-bold mb-1">

                                    <?= htmlspecialchars($criterionTitle) ?>

                                </div>

                                <div class="small text-muted">

                                    Weight:
                                    <?= $weight ?>

                                </div>

                            </div>

                            <div class="badge bg-light text-dark rounded-pill px-3 py-2">

                                Max:
                                <?= $maxScore ?>

                            </div>

                        </div>

                        <input
                            type="range"
                            name="criteria[<?= (int)$c['id'] ?>]"
                            class="form-range reviewer-slider"
                            min="0"
                            max="<?= $maxScore ?>"
                            step="0.1"
                            value="<?= $maxScore / 2 ?>"
                            oninput="
                                document.getElementById(
                                    'score_<?= (int)$c['id'] ?>'
                                ).innerText = this.value
                            "
                        >

                        <div class="d-flex justify-content-between align-items-center mt-2">

                            <div class="small text-muted">

                                <?= $isRtl ? 'التقييم الحالي' : 'Current Score' ?>

                            </div>

                            <div
                                id="score_<?= (int)$c['id'] ?>"
                                class="badge bg-success rounded-pill px-3 py-2 fs-6">

                                <?= $maxScore / 2 ?>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        <?php endforeach; ?>

    </div>

</div>
            </div>

            <!-- META -->

            <div class="row g-3">

                <div class="col-md-6">

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

                    <select name="recommendation"
                            class="form-select rounded-3"
                            required>

                        <option value="accept">
    <?= $isRtl ? 'قبول' : 'Accept' ?>
</option>

<option value="minor_revision">
    <?= $isRtl ? 'تعديلات طفيفة' : 'Minor Revision' ?>
</option>

<option value="major_revision">
    <?= $isRtl ? 'تعديلات جوهرية' : 'Major Revision' ?>
</option>

<option value="reject">
    <?= $isRtl ? 'رفض' : 'Reject' ?>
</option>

                    </select>

                </div>

                <div class="col-md-6">

                    <label class="form-label fw-bold">
                        <?= e(trans('upload_review_file')) ?>
                    </label>

                    <input type="file"
                           name="review_file"
                           class="form-control rounded-3">

                </div>

                <div class="col-md-6">

                    <label class="form-label fw-bold">
                        Comments to Author
                    </label>

                    <textarea name="comments"
                              class="form-control rounded-3"
                              rows="6"></textarea>

                </div>

                <div class="col-md-6">

                    <label class="form-label fw-bold">
                        Comments to Editor
                    </label>

                    <textarea name="comments_to_editor"
                              class="form-control rounded-3"
                              rows="6"></textarea>

                </div>

            </div>

            <!-- SUBMIT -->

            <div class="mt-4">

                <button type="submit"
                        class="btn btn-success rounded-3">

                    <?= e(trans('save')) ?>

                </button>

            </div>

        </form>

    </div>
</div>

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

<div class="card border-0 shadow-sm rounded-4 mt-4">

    <div class="card-body p-4">

        <div class="section-title">
            <span class="dot"></span>

            <h2 class="h5 mb-0">
                <?= e(trans('review_files')) ?>
            </h2>
        </div>

        <div class="table-responsive">

            <table class="table align-middle mb-0">

                <thead>
                    <tr>
                        <th><?= e(trans('review_file')) ?></th>
                        <th>Date</th>
                    </tr>
                </thead>

                <tbody>

                    <?php foreach ($files as $f): ?>

                        <tr>

                            <td>
                                <?= e((string) ($f['original_filename'] ?? '')) ?>
                            </td>

                            <td>
                                <?= e((string) ($f['created_at'] ?? '')) ?>
                            </td>

                        </tr>

                    <?php endforeach; ?>

                </tbody>

            </table>

        </div>

    </div>

</div>

<?php endif; ?>
<style>

.reviewer-score-card{
    transition:all .25s ease;
    border:1px solid rgba(0,0,0,.05);
}

.reviewer-score-card:hover{
    transform:translateY(-2px);
    box-shadow:0 12px 30px rgba(0,0,0,.08)!important;
}

.reviewer-slider{
    height:8px;
}

.reviewer-slider::-webkit-slider-thumb{
    width:20px;
    height:20px;
    border-radius:50%;
    cursor:pointer;
}

.section-title{
    display:flex;
    align-items:center;
    gap:10px;
    margin-bottom:20px;
}

.section-title .dot{
    width:12px;
    height:12px;
    border-radius:50%;
    background:#0d6efd;
}

.page-title{
    font-weight:800;
}

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