<?php
function rvh_h($v) { return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }
function rvh_t($key, $fallback) {
    if (function_exists('ec_t')) return ec_t($key, $fallback);
    if (function_exists('t')) return t($key, $fallback);
    return $fallback;
}
function rvh_rec($v) {
    return function_exists('ec_review_recommendation_t') ? ec_review_recommendation_t($v) : (string)$v;
}
$reviews = $reviews ?? [];
?>

<div class="container-fluid">
    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1">
                <i class="fa fa-clock-rotate-left"></i>
                <?= rvh_h(rvh_t('reviewer.history.title','Review History')) ?>
            </h3>
            <div class="text-muted"><?= rvh_h(rvh_t('reviewer.history.completed_reviews','Completed Reviews')) ?></div>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th><?= rvh_h(rvh_t('reviewer.assignment.submission_no','Submission No.')) ?></th>
                        <th><?= rvh_h(rvh_t('reviewer.assignment.paper_title','Paper Title')) ?></th>
                        <th><?= rvh_h(rvh_t('reviewer.assignment.score','Score')) ?></th>
                        <th><?= rvh_h(rvh_t('reviewer.assignment.recommendation','Recommendation')) ?></th>
                        <th><?= rvh_h(rvh_t('reviewer.assignment.submitted_at','Submitted At')) ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($reviews as $r): ?>
                        <tr>
                            <td><?= rvh_h($r['submission_no'] ?? '') ?></td>
                            <td><strong><?= rvh_h(($r['title'] ?? '') ?: ($r['title_ar'] ?? '')) ?></strong></td>
                            <td><?= rvh_h($r['total_score'] ?? '') ?></td>
                            <td><span class="badge bg-success"><?= rvh_h(rvh_rec($r['recommendation'] ?? '')) ?></span></td>
                            <td><?= rvh_h($r['submitted_at'] ?? $r['updated_at'] ?? '') ?></td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$reviews): ?>
                        <tr>
                            <td colspan="5" class="text-center text-muted py-4">
                                <?= rvh_h(rvh_t('reviewer.history.no_reviews','No completed reviews found.')) ?>
                            </td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
