<?php
$summary = $report['summary'] ?? [];
$results = $report['results'] ?? [];

function wc_e($v): string {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$score = (int)($summary['score'] ?? 0);
$status = (string)($summary['status'] ?? 'unknown');
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Workflow Certification</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body{
            margin:0;
            background:#f4f6fb;
            color:#172033;
            font-family:Arial,Helvetica,sans-serif;
        }
        .wrap{
            max-width:1300px;
            margin:0 auto;
            padding:28px;
        }
        .hero{
            background:linear-gradient(135deg,#111827,#1f2937);
            color:white;
            border-radius:22px;
            padding:28px;
            box-shadow:0 18px 45px rgba(15,23,42,.18);
            margin-bottom:22px;
        }
        .hero h1{
            margin:0 0 8px;
            font-size:30px;
        }
        .hero p{
            margin:0;
            opacity:.82;
        }
        .cards{
            display:grid;
            grid-template-columns:repeat(4,minmax(0,1fr));
            gap:16px;
            margin-bottom:22px;
        }
        .card{
            background:white;
            border-radius:18px;
            padding:20px;
            box-shadow:0 10px 26px rgba(15,23,42,.08);
            border:1px solid #e5e7eb;
        }
        .card .label{
            color:#64748b;
            font-size:13px;
            text-transform:uppercase;
            letter-spacing:.06em;
            margin-bottom:8px;
        }
        .card .value{
            font-size:32px;
            font-weight:800;
        }
        .status-healthy{color:#15803d}
        .status-warning{color:#b45309}
        .status-critical{color:#b91c1c}
        .toolbar{
            background:white;
            border:1px solid #e5e7eb;
            border-radius:18px;
            padding:16px;
            margin-bottom:22px;
            box-shadow:0 10px 26px rgba(15,23,42,.06);
        }
        select,button{
            padding:10px 12px;
            border-radius:10px;
            border:1px solid #cbd5e1;
            font-size:14px;
        }
        button{
            background:#111827;
            color:white;
            cursor:pointer;
        }
        .panel{
            background:white;
            border-radius:18px;
            border:1px solid #e5e7eb;
            box-shadow:0 10px 26px rgba(15,23,42,.06);
            overflow:hidden;
            margin-bottom:22px;
        }
        .panel h2{
            margin:0;
            padding:18px 20px;
            border-bottom:1px solid #e5e7eb;
            font-size:20px;
        }
        table{
            width:100%;
            border-collapse:collapse;
        }
        th,td{
            padding:12px 14px;
            border-bottom:1px solid #eef2f7;
            vertical-align:top;
            font-size:14px;
        }
        th{
            background:#f8fafc;
            text-align:left;
            color:#475569;
            font-size:12px;
            text-transform:uppercase;
            letter-spacing:.04em;
        }
        .badge{
            display:inline-block;
            padding:5px 9px;
            border-radius:999px;
            font-size:12px;
            font-weight:700;
        }
        .badge-pass{background:#dcfce7;color:#166534}
        .badge-attention{background:#fef3c7;color:#92400e}
        .badge-critical{background:#fee2e2;color:#991b1b}
        .muted{color:#64748b}
        .issue{
            margin:3px 0;
            line-height:1.4;
        }
        .issue-critical{color:#991b1b}
        .issue-warning{color:#92400e}
        @media(max-width:900px){
            .cards{grid-template-columns:1fr 1fr}
            .wrap{padding:16px}
        }
        @media(max-width:600px){
            .cards{grid-template-columns:1fr}
            table{font-size:12px}
        }
    </style>
</head>
<body>
<div class="wrap">

    <div class="hero">
        <h1>Enterprise Workflow Certification</h1>
        <p>Read-only certification layer for submission, reviewer, production, issue, and publication workflow states.</p>
    </div>

    <form method="get" class="toolbar">
        <label>
            Conference:
            <select name="conference_id">
                <option value="0">All conferences</option>
                <?php foreach ($conferences as $c): ?>
                    <option value="<?= (int)$c['id'] ?>" <?= ((int)($_GET['conference_id'] ?? 0) === (int)$c['id']) ? 'selected' : '' ?>>
                        #<?= (int)$c['id'] ?> -
                        <?= wc_e($c['acronym'] ?? '') ?>
                        <?= wc_e($c['title'] ?? '') ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </label>
        <button type="submit">Run Certification</button>
    </form>

    <div class="cards">
        <div class="card">
            <div class="label">Certification Score</div>
            <div class="value status-<?= wc_e($status) ?>"><?= $score ?>%</div>
        </div>
        <div class="card">
            <div class="label">Checked Submissions</div>
            <div class="value"><?= (int)($summary['checked'] ?? 0) ?></div>
        </div>
        <div class="card">
            <div class="label">Critical Issues</div>
            <div class="value status-critical"><?= (int)($summary['critical'] ?? 0) ?></div>
        </div>
        <div class="card">
            <div class="label">Warnings</div>
            <div class="value status-warning"><?= (int)($summary['warning'] ?? 0) ?></div>
        </div>
    </div>

    <?php foreach ($results as $result): ?>
        <div class="panel">
            <h2>
                <?= wc_e($result->validator) ?>
                <span class="muted">
                    —
                    <?= (int)$result->checked ?>
                    checked
                </span>
            </h2>

            <table>
                <thead>
                <tr>
                    <th>Submission</th>
                    <th>Title</th>
                    <th>Stage / Status</th>
                    <th>Review</th>
                    <th>Production</th>
                    <th>Issue</th>
                    <th>Certification</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($result->items as $item): ?>
                    <tr>
                        <td>
                            <strong>#<?= (int)$item['submission_id'] ?></strong><br>
                            <span class="muted"><?= wc_e($item['submission_no']) ?></span>
                        </td>
                        <td><?= wc_e($item['title']) ?></td>
                        <td>
                            <strong><?= wc_e($item['stage']) ?></strong><br>
                            <span class="muted"><?= wc_e($item['status']) ?></span>
                        </td>
                        <td>
                            Reviewers: <?= (int)$item['reviewers'] ?><br>
                            Completed: <?= (int)$item['completed_reviews'] ?>
                        </td>
                        <td>
                            Camera Ready: <?= (int)$item['camera_ready'] ?><br>
                            Approved Galleys: <?= (int)$item['approved_galleys'] ?>
                        </td>
                        <td>
                            Issue: <?= wc_e($item['issue_id']) ?><br>
                            Published: <?= wc_e($item['published_at']) ?>
                        </td>
                        <td>
                            <?php if ($item['health'] === 'pass'): ?>
                                <span class="badge badge-pass">PASS</span>
                            <?php else: ?>
                                <span class="badge badge-attention">ATTENTION</span>
                                <?php foreach ($item['issues'] as $issue): ?>
                                    <div class="issue issue-<?= wc_e($issue[0]) ?>">
                                        <?= strtoupper(wc_e($issue[0])) ?>:
                                        <?= wc_e($issue[2]) ?>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endforeach; ?>

</div>
</body>
</html>
