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

function rev_t($key, $fallback) {
    if (function_exists('ec_t')) return ec_t($key, $fallback);
    if (function_exists('t')) return t($key, $fallback);
    return $fallback;
}

function rev_status($v) {
    return function_exists('ec_review_status_t') ? ec_review_status_t($v) : (string)$v;
}

/* 🔥 SAFE DEFAULTS */
$assignments = $assignments ?? [];
$stats = $stats ?? [
    'total' => 0,
    'assigned' => 0,
    'completed' => 0,
    'late_reviews' => 0
];
?>

<div class="container-fluid">

    <!-- HEADER -->
    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#2563eb);color:#fff;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1">
                <i class="fa fa-clipboard-check"></i>
                <?= rev_h(rev_t('reviewer.dashboard.title','Reviewer Dashboard')) ?>
            </h3>
            <div class="opacity-75">
                <?= rev_h(rev_t('reviewer.dashboard.welcome','Welcome to your reviewer dashboard')) ?>
            </div>
        </div>
    </div>

    <!-- STATS -->
    <div class="row g-3 mb-4">
        <?php
        $cards = [
            ['reviewer.dashboard.total_assignments','Total Assignments',$stats['total'] ?? 0,'fa-list-check'],
            ['reviewer.dashboard.pending_assignments','Pending Assignments',$stats['assigned'] ?? 0,'fa-hourglass-half'],
            ['reviewer.dashboard.completed_reviews','Completed Reviews',$stats['completed'] ?? 0,'fa-circle-check'],
            ['reviewer.dashboard.late_reviews','Late Reviews',$stats['late_reviews'] ?? 0,'fa-triangle-exclamation'],
        ];
        foreach ($cards as $c):
        ?>
        <div class="col-lg-3 col-md-6">
            <div class="card border-0 shadow-sm h-100" style="border-radius:20px;">
                <div class="card-body p-4 d-flex justify-content-between align-items-center">
                    <div>
                        <div class="text-muted small fw-bold">
                            <?= rev_h(rev_t($c[0],$c[1])) ?>
                        </div>
                        <div class="fs-2 fw-bold">
                            <?= (int)$c[2] ?>
                        </div>
                    </div>
                    <div class="rounded-circle d-flex align-items-center justify-content-center"
                         style="width:52px;height:52px;background:#eef2ff;color:#1d4ed8;">
                        <i class="fa <?= rev_h($c[3]) ?>"></i>
                    </div>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>

    <!-- ASSIGNMENTS TABLE -->
    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">

            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="fw-bold mb-0">
                    <?= rev_h(rev_t('reviewer.assignments.title','Review Assignments')) ?>
                </h5>
            </div>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>#</th>
                            <th><?= rev_h(rev_t('reviewer.assignment.paper_title','Paper Title')) ?></th>
                            <th><?= rev_h(rev_t('reviewer.assignment.review_status','Status')) ?></th>
                            <th><?= rev_h(rev_t('reviewer.assignment.deadline','Deadline')) ?></th>
                            <th><?= rev_h(rev_t('submission.actions','Actions')) ?></th>
                        </tr>
                    </thead>

                    <tbody>
                    <?php foreach ($assignments as $a): ?>
                        <tr>

                            <!-- Submission -->
                            <td>#<?= (int)($a['submission_id'] ?? 0) ?></td>

                            <!-- Title -->
                            <td>
                                <strong>
                                    <?= rev_h(($a['title'] ?? '') ?: ($a['title_ar'] ?? 'Untitled')) ?>
                                </strong>
                            </td>

                            <!-- Status -->
                            <td>
                                <span class="badge bg-info text-dark">
                                    <?= rev_h(rev_status($a['status'] ?? 'assigned')) ?>
                                </span>
                            </td>

                            <!-- Deadline -->
                            <td>
                                <?= rev_h($a['due_date'] ?? '-') ?>
                            </td>

                            <!-- Actions -->
                            <td>

                                <?php if (($a['status'] ?? '') !== 'completed'): ?>

                                    <a href="/easychair/reviewer/review?assignment_id=<?= (int)$a['id'] ?>"
                                       class="btn btn-primary btn-sm">
                                        Review
                                    </a>

                                <?php else: ?>

                                    <span class="badge bg-success">Completed</span>

                                    <span class="badge bg-secondary">
                                        Round <?= (int)($a['current_round_no'] ?? 1) ?>
                                    </span>

                                <?php endif; ?>

                            </td>

                        </tr>
                    <?php endforeach; ?>

                    <?php if (empty($assignments)): ?>
                        <tr>
                            <td colspan="5" class="text-center text-muted py-4">
                                <?= rev_h(rev_t('reviewer.assignments.no_assignments','No review assignments found.')) ?>
                            </td>
                        </tr>
                    <?php endif; ?>

                    </tbody>
                </table>
            </div>

        </div>
    </div>
</div>
