<?php
function dr_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$files = $files ?? [];
$file = $file ?? '';
$suggestions = $suggestions ?? [];
$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 d-flex justify-content-between align-items-center flex-wrap gap-3">
            <div>
                <h3 class="fw-bold mb-1">
                    <i class="fa fa-wand-magic-sparkles"></i>
                    Dictionary Replace Generator
                </h3>
                <div class="opacity-75">
                    Generate safe ec_t() replacements for selected view/controller files.
                </div>
            </div>
            <div class="d-flex gap-2">
                <a class="btn btn-light rounded-pill" href="/easychair/admin/dictionary-scan">
                    <i class="fa fa-magnifying-glass"></i>
                    Scanner
                </a>
                <a class="btn btn-outline-light rounded-pill" href="/easychair/admin/dictionary">
                    <i class="fa fa-language"></i>
                    Dictionary
                </a>
            </div>
        </div>
    </div>

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

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

    <div class="alert alert-warning">
        <strong>Safe mode:</strong>
        This tool only replaces simple visible labels such as headings, table headers, labels, buttons, links, and options.
        It creates a backup before applying.
    </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/dictionary-replace" class="row g-3 align-items-end">
                <div class="col-lg-10">
                    <label class="form-label fw-bold">File</label>
                    <select name="file" class="form-select" required>
                        <option value="">Choose file...</option>
                        <?php foreach ($files as $f): ?>
                            <option value="<?= dr_h($f) ?>" <?= $file === $f ? 'selected' : '' ?>>
                                <?= dr_h($f) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-lg-2">
                    <button class="btn btn-primary w-100 rounded-pill">
                        Analyze
                    </button>
                </div>
            </form>
        </div>
    </div>

    <?php if ($file): ?>
        <form method="post" action="/easychair/admin/dictionary-replace/apply">
            <input type="hidden" name="file" value="<?= dr_h($file) ?>">

            <div class="card border-0 shadow-sm" style="border-radius:22px;">
                <div class="card-body p-4">
                    <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
                        <div>
                            <h5 class="fw-bold mb-1">Suggestions</h5>
                            <div class="text-muted small"><?= dr_h($file) ?></div>
                        </div>

                        <div class="d-flex gap-2">
                            <button type="button" class="btn btn-outline-primary rounded-pill" onclick="document.querySelectorAll('.dr-check').forEach(x=>x.checked=true)">
                                Select All
                            </button>
                            <button type="button" class="btn btn-outline-secondary rounded-pill" onclick="document.querySelectorAll('.dr-check').forEach(x=>x.checked=false)">
                                Clear
                            </button>
                            <button class="btn btn-success rounded-pill px-4"
                                    onclick="return confirm('Apply selected replacements to this file? A backup will be created.')">
                                Apply Selected
                            </button>
                        </div>
                    </div>

                    <div class="table-responsive">
                        <table class="table table-bordered align-middle">
                            <thead class="table-dark">
                            <tr>
                                <th style="width:60px;">Use</th>
                                <th>Detected Text</th>
                                <th>Translation Key</th>
                                <th>Occurrences</th>
                                <th>Replacement Preview</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($suggestions as $i => $s): ?>
                                <tr>
                                    <td class="text-center">
                                        <input class="dr-check" type="checkbox" name="rows[<?= $i ?>][selected]" value="1">
                                    </td>
                                    <td>
                                        <input type="hidden" name="rows[<?= $i ?>][text]" value="<?= dr_h($s['text']) ?>">
                                        <strong><?= dr_h($s['text']) ?></strong>
                                        <div class="text-muted small"><?= dr_h($s['group_key']) ?></div>
                                    </td>
                                    <td>
                                        <input class="form-control form-control-sm"
                                               name="rows[<?= $i ?>][translation_key]"
                                               value="<?= dr_h($s['translation_key']) ?>">
                                    </td>
                                    <td>
                                        <span class="badge bg-primary"><?= (int)$s['count'] ?></span>
                                    </td>
                                    <td>
                                        <code style="white-space:pre-wrap;"><?= dr_h($s['replacement']) ?></code>
                                    </td>
                                </tr>
                            <?php endforeach; ?>

                            <?php if (!$suggestions): ?>
                                <tr>
                                    <td colspan="5" class="text-center text-muted py-4">
                                        No replacement suggestions found for this file.
                                    </td>
                                </tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <?php if ($suggestions): ?>
                        <button class="btn btn-success btn-lg rounded-pill px-5 mt-3"
                                onclick="return confirm('Apply selected replacements to this file?')">
                            Apply Selected Replacements
                        </button>
                    <?php endif; ?>
                </div>
            </div>
        </form>
    <?php endif; ?>
</div>
