<?php
if (!function_exists('ec_sa_h')) {
    function ec_sa_h($v) {
        return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
    }
}

$submissionRow = $submissionRow ?? ($s ?? ($row ?? []));
$sid = (int)($submissionRow['id'] ?? $submissionRow['submission_id'] ?? 0);
$status = (string)($submissionRow['status'] ?? '');
$stage = (string)($submissionRow['stage'] ?? '');
$productionStatus = (string)($submissionRow['production_status'] ?? '');

$canProduction = in_array($status, ['accepted','camera_ready','production','galley','assigned_to_issue','published'], true)
    || in_array($stage, ['accepted','camera_ready','production','galley','issue','published'], true)
    || in_array($productionStatus, ['camera_ready_uploaded','galley_uploaded','approved','published'], true);

$canIssue = in_array($status, ['accepted','production','assigned_to_issue','published'], true)
    || in_array($stage, ['accepted','production','galley','issue','published'], true);
?>

<?php if ($sid > 0): ?>
    <div class="d-flex flex-wrap gap-1">
        <a class="btn btn-sm btn-primary rounded-pill"
           href="/easychair/admin/submission-workflow?submission_id=<?= $sid ?>">
            Workflow
        </a>

        <?php if ($canProduction): ?>
            <a class="btn btn-sm btn-success rounded-pill"
               href="/easychair/admin/production">
                Production
            </a>
        <?php endif; ?>

        <?php if ($canIssue): ?>
            <a class="btn btn-sm btn-info rounded-pill"
               href="/easychair/admin/issues">
                Issue
            </a>
        <?php endif; ?>

        <?php if (!empty($submissionRow['file_path'])): ?>
            <a class="btn btn-sm btn-outline-secondary rounded-pill"
               target="_blank"
               href="/easychair/storage/<?= ec_sa_h($submissionRow['file_path']) ?>">
                File
            </a>
        <?php endif; ?>
    </div>
<?php endif; ?>
