<?php

$missingProceedings =
    $missingProceedings ?? [];

$missingGalleys =
    $missingGalleys ?? [];

$nonApprovedGalleys =
    $nonApprovedGalleys ?? [];

$readyForIssue =
    $readyForIssue ?? [];

?>

<div class="container-fluid">

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

        <div>

            <h2 class="fw-bold mb-1">
                Publication Integrity Monitor
            </h2>

            <div class="text-muted">
                Validate proceedings, production galleys, publication workflow integrity and repair inconsistencies.
            </div>

        </div>

        <div>

            <span class="badge bg-primary fs-6">
                Total Issues:
                <?= count($missingProceedings)
                + count($missingGalleys)
                + count($nonApprovedGalleys); ?>
            </span>

        </div>

    </div>

    <!-- SUMMARY -->

    <div class="row mb-4">

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

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

                <div class="card-body text-center">

                    <div class="display-6 fw-bold text-danger">
                        <?= count($missingProceedings) ?>
                    </div>

                    <div class="small text-muted">
                        Missing Proceedings Rows
                    </div>

                </div>

            </div>

        </div>

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

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

                <div class="card-body text-center">

                    <div class="display-6 fw-bold text-warning">
                        <?= count($missingGalleys) ?>
                    </div>

                    <div class="small text-muted">
                        Missing Galleys
                    </div>

                </div>

            </div>

        </div>

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

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

                <div class="card-body text-center">

                    <div class="display-6 fw-bold text-danger">
                        <?= count($nonApprovedGalleys) ?>
                    </div>

                    <div class="small text-muted">
                        Non Approved Galleys
                    </div>

                </div>

            </div>

        </div>

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

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

                <div class="card-body text-center">

                    <div class="display-6 fw-bold text-success">
                        <?= count($readyForIssue) ?>
                    </div>

                    <div class="small text-muted">
                        Ready For Issue Assignment
                    </div>

                </div>

            </div>

        </div>

    </div>

    <!-- MISSING PROCEEDINGS -->

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

        <div class="card-header bg-danger text-white">

            Missing Proceedings Rows

        </div>

        <div class="card-body">

            <?php if (empty($missingProceedings)): ?>

                <div class="alert alert-success mb-0">
                    No missing proceedings records found.
                </div>

            <?php else: ?>

                <div class="table-responsive">

                    <table class="table table-bordered table-hover">

                        <thead>

                        <tr>

                            <th>ID</th>
                            <th>Submission</th>
                            <th>Title</th>
                            <th>Issue</th>
                            <th width="180">Action</th>

                        </tr>

                        </thead>

                        <tbody>

                        <?php foreach ($missingProceedings as $r): ?>

                            <tr>

                                <td>
                                    <?= (int)$r['id'] ?>
                                </td>

                                <td>
                                    <?= htmlspecialchars($r['submission_no']) ?>
                                </td>

                                <td>
                                    <?= htmlspecialchars($r['title'] ?? '') ?>
                                </td>

                                <td>
                                    <?= (int)($r['issue_id'] ?? 0) ?>
                                </td>

                                <td>

                                    <a
                                        class="btn btn-sm btn-warning"
                                        href="/easychair/admin/conference-home/publication-integrity-repair?action=repair_proceedings&submission_id=<?= (int)$r['id'] ?>"
                                    >
                                        Repair
                                    </a>

                                </td>

                            </tr>

                        <?php endforeach; ?>

                        </tbody>

                    </table>

                </div>

            <?php endif; ?>

        </div>

    </div>

    <!-- MISSING GALLEYS -->

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

        <div class="card-header bg-warning">

            Missing Galleys

        </div>

        <div class="card-body">

            <?php if (empty($missingGalleys)): ?>

                <div class="alert alert-success mb-0">
                    No missing galley references found.
                </div>

            <?php else: ?>

                <div class="table-responsive">

                    <table class="table table-bordered table-hover">

                        <thead>

                        <tr>

                            <th>ID</th>
                            <th>Submission</th>
                            <th>Issue</th>
                            <th>Galley ID</th>
                            <th width="200">Action</th>

                        </tr>

                        </thead>

                        <tbody>

                        <?php foreach ($missingGalleys as $r): ?>

                            <tr>

                                <td><?= (int)$r['id'] ?></td>

                                <td><?= htmlspecialchars($r['submission_no']) ?></td>

                                <td><?= (int)$r['issue_id'] ?></td>

                                <td><?= (int)$r['galley_id'] ?></td>

                                <td>

                                    <a
                                        class="btn btn-sm btn-danger"
                                        href="/easychair/admin/conference-home/publication-integrity-repair?action=repair_galley&submission_id=<?= (int)$r['id'] ?>"
                                    >
                                        Repair Galley
                                    </a>

                                </td>

                            </tr>

                        <?php endforeach; ?>

                        </tbody>

                    </table>

                </div>

            <?php endif; ?>

        </div>

    </div>

    <!-- NON APPROVED -->

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

        <div class="card-header bg-secondary text-white">

            Non Approved Galleys

        </div>

        <div class="card-body">

            <?php if (empty($nonApprovedGalleys)): ?>

                <div class="alert alert-success mb-0">
                    No non-approved galleys detected.
                </div>

            <?php else: ?>

                <div class="table-responsive">

                    <table class="table table-bordered table-hover">

                        <thead>

                        <tr>

                            <th>ID</th>
                            <th>Submission</th>
                            <th>Issue</th>
                            <th>Status</th>

                        </tr>

                        </thead>

                        <tbody>

                        <?php foreach ($nonApprovedGalleys as $r): ?>

                            <tr>

                                <td><?= (int)$r['id'] ?></td>

                                <td><?= htmlspecialchars($r['submission_no']) ?></td>

                                <td><?= (int)$r['issue_id'] ?></td>

                                <td>
                                    <span class="badge bg-danger">
                                        <?= htmlspecialchars($r['status']) ?>
                                    </span>
                                </td>

                            </tr>

                        <?php endforeach; ?>

                        </tbody>

                    </table>

                </div>

            <?php endif; ?>

        </div>

    </div>

    <!-- READY FOR ISSUE -->

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

        <div class="card-header bg-success text-white">

            Approved Galleys Ready For Issue

        </div>

        <div class="card-body">

            <?php if (empty($readyForIssue)): ?>

                <div class="alert alert-success mb-0">
                    No approved galleys waiting for issue assignment.
                </div>

            <?php else: ?>

                <div class="table-responsive">

                    <table class="table table-bordered table-hover">

                        <thead>

                        <tr>

                            <th>ID</th>
                            <th>Submission</th>
                            <th>Galley ID</th>

                        </tr>

                        </thead>

                        <tbody>

                        <?php foreach ($readyForIssue as $r): ?>

                            <tr>

                                <td><?= (int)$r['id'] ?></td>

                                <td><?= htmlspecialchars($r['submission_no']) ?></td>

                                <td><?= (int)$r['galley_id'] ?></td>

                            </tr>

                        <?php endforeach; ?>

                        </tbody>

                    </table>

                </div>

            <?php endif; ?>

        </div>

    </div>

</div>
