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

<div class="container-fluid py-3">

    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1"><i class="fa fa-clipboard-check"></i> Review Monitor</h3>
            <div class="opacity-75">Track assignments, completed reviews, scores, overdue reviewers, and submission status.</div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold">Submissions Review Status</h5>
            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>Submission</th>
                            <th>Status</th>
                            <th>Assigned</th>
                            <th>Completed</th>
                            <th>Average Score</th>
                            <th>Nearest Due</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($submissions as $s): ?>
                        <?php
                            $assigned = (int)$s['assigned_count'];
                            $completed = (int)$s['completed_count'];
                            $pct = $assigned > 0 ? round(($completed / $assigned) * 100) : 0;
                        ?>
                        <tr>
                            <td>
                                <strong><?= rm_h($s['submission_no']) ?></strong>
                                <div class="text-muted small"><?= rm_h($s['submission_title']) ?></div>
                            </td>
                            <td>
                                <span class="badge bg-primary"><?= rm_h($s['status']) ?></span>
                                <span class="badge bg-secondary"><?= rm_h($s['stage']) ?></span>
                            </td>
                            <td><?= $assigned ?></td>
                            <td>
                                <?= $completed ?>
                                <div class="progress mt-1" style="height:8px;">
                                    <div class="progress-bar bg-success" style="width:<?= (int)$pct ?>%"></div>
                                </div>
                            </td>
                            <td><?= $s['avg_score'] !== null ? number_format((float)$s['avg_score'],2) : '-' ?></td>
                            <td><?= rm_h($s['nearest_due'] ?? '-') ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (!$submissions): ?>
                        <tr><td colspan="6" class="text-center text-muted py-4">No submissions.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold">Reviewer Load</h5>
            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>Reviewer</th>
                            <th>Assigned</th>
                            <th>Completed</th>
                            <th>Overdue</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($reviewers as $r): ?>
                        <tr>
                            <td>
                                <strong><?= rm_h(trim($r['reviewer_name']) ?: $r['email']) ?></strong>
                                <div class="text-muted small"><?= rm_h($r['email']) ?></div>
                            </td>
                            <td><?= (int)$r['assigned_count'] ?></td>
                            <td><?= (int)$r['completed_count'] ?></td>
                            <td>
                                <?php if ((int)$r['overdue_count'] > 0): ?>
                                    <span class="badge bg-danger"><?= (int)$r['overdue_count'] ?></span>
                                <?php else: ?>
                                    <span class="badge bg-success">0</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (!$reviewers): ?>
                        <tr><td colspan="4" class="text-center text-muted py-4">No reviewers.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</div>
