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

.ecStatus{

display:inline-flex;
align-items:center;
justify-content:center;

min-width:150px;

padding:8px 18px;

border-radius:999px;

font-size:12px;

font-weight:700;

letter-spacing:.4px;

text-transform:capitalize;

color:#fff;

box-shadow:0 3px 10px rgba(0,0,0,.15);

transition:.2s ease;

}

.ecStatus:hover{

transform:translateY(-1px);

box-shadow:0 6px 16px rgba(0,0,0,.20);

}

/* ----------------------------
   Business Status Colors
   ---------------------------- */

.ecStatus.submitted{

background:#6c757d;

}

.ecStatus.screening{

background:#0d6efd;

}

.ecStatus.under_review{

background:#f39c12;
color:#222;

}

.ecStatus.revision_required{

background:#dc3545;

}

.ecStatus.revision_submitted{

background:#20c997;

}

.ecStatus.accepted{

background:#198754;

}

.ecStatus.rejected{

background:#b02a37;

}

.ecStatus.production{

background:#0f766e;

}

.ecStatus.assigned_to_issue{

background:#0ea5e9;

}

.ecStatus.published{

background:#212529;

}

.ecStatus.camera_ready{

background:#6f42c1;

}

/* fallback */

.ecStatus:not([class*="submitted"])
:not([class*="screening"])
:not([class*="under_review"])
:not([class*="revision_required"])
:not([class*="revision_submitted"])
:not([class*="accepted"])
:not([class*="rejected"])
:not([class*="production"])
:not([class*="assigned_to_issue"])
:not([class*="published"])
:not([class*="camera_ready"]){

background:#6c757d;

}
    
</style>
<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 width="170">Workflow Stage</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 class="text-center">

<?php

$status = strtolower((string)($r['status'] ?? 'submitted'));

?>

<span class="ecStatus <?= ss_h($status) ?>">

<?= ss_h(ucwords(str_replace('_',' ',$status))) ?>

</span>

</td>
                            <td class="text-center"><?= (int)$r['files_count'] ?></td>
                            <td class="text-center fw-bold"><?= (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>
