<?php
function ss_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$rows = $rows ?? [];
$q = $_GET['q'] ?? '';
$status = $_GET['status'] ?? '';
$stage = $_GET['stage'] ?? '';
?>

<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-magnifying-glass"></i> Submission Search & Workflow Access</h3>
            <div class="opacity-75">Safe search page for opening workflow, production, and issue operations.</div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <form method="get" action="/easychair/admin/submission-search" class="row g-2 align-items-end">
                <div class="col-lg-4">
                    <label class="form-label fw-bold">Search</label>
                    <input name="q" class="form-control" value="<?= ss_h($q) ?>" placeholder="Submission no, title, author email...">
                </div>

                <div class="col-lg-3">
                    <label class="form-label fw-bold">Status</label>
                    <select name="status" class="form-select">
                        <option value="">All</option>
                        <?php foreach (['submitted','under_review','revision_required','revision_submitted','accepted','rejected','camera_ready','production','assigned_to_issue','published'] as $x): ?>
                            <option value="<?= ss_h($x) ?>" <?= $status === $x ? 'selected' : '' ?>><?= ss_h($x) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-lg-3">
                    <label class="form-label fw-bold">Stage</label>
                    <select name="stage" class="form-select">
                        <option value="">All</option>
                        <?php foreach (['submitted','screening','reviewing','rebuttal','revision_required','revision_submitted','accepted','rejected','camera_ready','production','galley','assigned_to_issue','issue','published'] as $x): ?>
                            <option value="<?= ss_h($x) ?>" <?= $stage === $x ? 'selected' : '' ?>><?= ss_h($x) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-lg-2">
                    <button class="btn btn-primary w-100">Filter</button>
                </div>
            </form>
        </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>Submission</th>
                        <th>Author</th>
                        <th>Conference / Org</th>
                        <th>Status</th>
                        <th>Files</th>
                        <th>Reviews</th>
                        <th>Workflow</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($rows as $r): ?>
                        <tr>
                            <td>
                                <strong><?= ss_h($r['submission_no'] ?: ('#'.$r['id'])) ?></strong>
                                <div class="text-muted small"><?= ss_h($r['display_title']) ?></div>
                            </td>
                            <td>
                                <?= ss_h(trim($r['author_name']) ?: $r['author_email']) ?>
                                <div class="text-muted small"><?= ss_h($r['author_email']) ?></div>
                            </td>
                            <td>
                                <strong><?= ss_h($r['conference_short_name']) ?></strong>
                                <div class="text-muted small"><?= ss_h($r['org_name']) ?></div>
                            </td>
                            <td>
                                <span class="badge bg-primary"><?= ss_h($r['status']) ?></span>
                                <span class="badge bg-warning text-dark"><?= ss_h($r['stage']) ?></span>
                                <span class="badge bg-info text-dark"><?= ss_h($r['production_status'] ?? 'pending') ?></span>
                            </td>
                            <td><?= (int)$r['files_count'] ?></td>
                            <td><?= (int)$r['assignments_count'] ?></td>
                            <td>
                                <?php $submissionRow = $r; require BASE_PATH . '/app/views/admin/partials/submission_actions.php'; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$rows): ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">No submissions found.</td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
