<?php
function wn_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$rules = $rules ?? [];
$templates = $templates ?? [];
$success = $_SESSION['flash_success'] ?? '';
$error = $_SESSION['flash_error'] ?? '';
unset($_SESSION['flash_success'], $_SESSION['flash_error']);
?>

<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-bell"></i> Workflow Notification Rules</h3>
            <div class="opacity-75">Map submission stages to conference email templates.</div>
        </div>
    </div>

    <?php if ($success): ?>
        <div class="alert alert-success"><?= wn_h($success) ?></div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?= wn_h($error) ?></div>
    <?php endif; ?>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">Add Notification Rule</h5>

            <form method="post" action="/easychair/admin/workflow-notifications/store" class="row g-2 align-items-end">
                <div class="col-md-3">
                    <label class="form-label fw-bold">Stage Key</label>
                    <input name="stage_key" class="form-control" placeholder="e.g. revision_required" required>
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold">Template</label>
                    <select name="template_key" class="form-select" required>
                        <option value="">Select template</option>
                        <?php foreach ($templates as $tpl): ?>
                            <?php $key = ($tpl['template_key'] ?? '') ?: ($tpl['event_key'] ?? ''); ?>
                            <option value="<?= wn_h($key) ?>">
                                <?= wn_h($key . ' — ' . (($tpl['subject_en'] ?? '') ?: ($tpl['subject_ar'] ?? ''))) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-2">
                    <label class="form-label fw-bold">Language</label>
                    <select name="lang" class="form-select">
                        <option value="en">English</option>
                        <option value="ar">Arabic</option>
                    </select>
                </div>

                <div class="col-md-1">
                    <label class="fw-bold">
                        <input type="checkbox" name="is_active" value="1" checked>
                        Active
                    </label>
                </div>

                <div class="col-md-2">
                    <button class="btn btn-success w-100">Add</button>
                </div>
            </form>
        </div>
    </div>

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

        <div class="card border-0 shadow-sm" style="border-radius:22px;">
            <div class="card-body p-4">

                <h5 class="fw-bold mb-3">Existing Rules</h5>

                <div class="table-responsive">
                    <table class="table table-bordered align-middle">
                        <thead class="table-dark">
                            <tr>
                                <th>Stage</th>
                                <th>Template</th>
                                <th>Language</th>
                                <th>Active</th>
                                <th>Action</th>
                            </tr>
                        </thead>

                        <tbody>
                        <?php foreach ($rules as $r): ?>
                            <tr>
                                <td>
                                    <strong><?= wn_h($r['stage_key']) ?></strong>
                                    <div class="text-muted small">Rule #<?= (int)$r['id'] ?></div>
                                </td>

                                <td>
                                    <select name="rules[<?= (int)$r['id'] ?>][template_key]" class="form-select">
                                        <?php foreach ($templates as $tpl): ?>
                                            <?php $key = ($tpl['template_key'] ?? '') ?: ($tpl['event_key'] ?? ''); ?>
                                            <option value="<?= wn_h($key) ?>" <?= (($r['template_key'] ?? '') === $key) ? 'selected' : '' ?>>
                                                <?= wn_h($key . ' — ' . (($tpl['subject_en'] ?? '') ?: ($tpl['subject_ar'] ?? ''))) ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </td>

                                <td>
                                    <select name="rules[<?= (int)$r['id'] ?>][lang]" class="form-select">
                                        <option value="en" <?= (($r['lang'] ?? '') === 'en') ? 'selected' : '' ?>>English</option>
                                        <option value="ar" <?= (($r['lang'] ?? '') === 'ar') ? 'selected' : '' ?>>Arabic</option>
                                    </select>
                                </td>

                                <td class="text-center">
                                    <input type="checkbox"
                                           name="rules[<?= (int)$r['id'] ?>][is_active]"
                                           value="1"
                                           <?= !empty($r['is_active']) ? 'checked' : '' ?>>
                                </td>

                                <td>
                                    <a class="btn btn-sm btn-danger"
                                       onclick="return confirm('Delete this rule?')"
                                       href="/easychair/admin/workflow-notifications/delete?id=<?= (int)$r['id'] ?>">
                                        Delete
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>

                        <?php if (!$rules): ?>
                            <tr>
                                <td colspan="5" class="text-center text-muted py-4">No workflow notification rules found.</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 Rules
                </button>
            </div>
        </div>
    </form>
</div>
