<?php
function wr_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$rules = $rules ?? [];
?>

<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-sliders"></i> Workflow Stage Rules</h3>
            <div class="opacity-75">
                Control what authors can do at every submission stage.
            </div>
        </div>
    </div>

    <?php if (!empty($_GET['saved'])): ?>
        <div class="alert alert-success">Workflow rules saved successfully.</div>
    <?php endif; ?>

    <form method="post" action="/easychair/admin/workflow-rules/save">

        <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>Stage</th>
                                <th>Author Update</th>
                                <th>Revision Upload</th>
                                <th>Rebuttal</th>
                                <th>Camera-ready</th>
                                <th>Visible to Author</th>
                                <th>Reviews Visible</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($rules as $r): ?>
                            <tr>
                                <td>
                                    <strong><?= wr_h($r['stage_key']) ?></strong>
                                    <div class="text-muted small">Rule #<?= (int)$r['id'] ?></div>
                                </td>

                                <?php foreach ([
                                    'allow_author_update',
                                    'allow_author_file_upload',
                                    'allow_rebuttal',
                                    'allow_camera_ready',
                                    'visible_to_author',
                                    'visible_reviews_to_author'
                                ] as $field): ?>
                                    <td class="text-center">
                                        <input type="checkbox"
                                               name="rules[<?= (int)$r['id'] ?>][<?= wr_h($field) ?>]"
                                               value="1"
                                               <?= !empty($r[$field]) ? 'checked' : '' ?>>
                                    </td>
                                <?php endforeach; ?>
                            </tr>
                        <?php endforeach; ?>

                        <?php if (!$rules): ?>
                            <tr>
                                <td colspan="7" class="text-center text-muted py-4">
                                    No workflow rules found. Run Pack 472 SQL seed.
                                </td>
                            </tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <button class="btn btn-success btn-lg rounded-pill px-5">
                    <i class="fa fa-save"></i>
                    Save Workflow Rules
                </button>

            </div>
        </div>

    </form>
</div>
