<?php
function ds_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
function ds_t($key, $fallback) {
    if (function_exists('ec_t')) return ec_t($key, $fallback);
    if (function_exists('t')) return t($key, $fallback);
    return $fallback;
}
$suggestions = $suggestions ?? [];
$success = $_SESSION['flash_success'] ?? '';
unset($_SESSION['flash_success']);
?>

<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-magnifying-glass"></i>
                    Dictionary Scanner
                </h3>
                <div class="opacity-75">
                    Scan EasyChair views/controllers and suggest missing translation keys.
                </div>
            </div>
            <a class="btn btn-light rounded-pill px-4" href="/easychair/admin/dictionary">
                <i class="fa fa-language"></i>
                Dictionary
            </a>
        </div>
    </div>

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

    <div class="alert alert-info">
        <strong>Tip:</strong>
        Select only useful interface labels. Ignore code words, class names, URLs, or technical strings.
    </div>

    <form method="post" action="/easychair/admin/dictionary-scan/store">
        <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">
                    <h5 class="fw-bold mb-0">
                        Suggestions
                        <span class="badge bg-primary"><?= count($suggestions) ?></span>
                    </h5>

                    <div class="d-flex gap-2">
                        <button type="button" class="btn btn-outline-primary rounded-pill" onclick="document.querySelectorAll('.ds-check').forEach(x=>x.checked=true)">
                            Select All
                        </button>
                        <button type="button" class="btn btn-outline-secondary rounded-pill" onclick="document.querySelectorAll('.ds-check').forEach(x=>x.checked=false)">
                            Clear
                        </button>
                        <button class="btn btn-success rounded-pill px-4">
                            Save Selected
                        </button>
                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table table-bordered align-middle">
                        <thead class="table-dark">
                        <tr>
                            <th style="width:55px;">Use</th>
                            <th>Suggested Key</th>
                            <th>Group</th>
                            <th>Detected Text</th>
                            <th>English Text</th>
                            <th>Arabic Text</th>
                            <th>File</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($suggestions as $i => $s): ?>
                            <?php
                            $text = (string)$s['text'];
                            $hasArabic = preg_match('/\p{Arabic}/u', $text);
                            ?>
                            <tr>
                                <td class="text-center">
                                    <input class="ds-check" type="checkbox" name="rows[<?= $i ?>][selected]" value="1">
                                </td>

                                <td>
                                    <input class="form-control form-control-sm"
                                           name="rows[<?= $i ?>][translation_key]"
                                           value="<?= ds_h($s['suggested_key']) ?>">
                                </td>

                                <td>
                                    <input class="form-control form-control-sm"
                                           name="rows[<?= $i ?>][group_key]"
                                           value="<?= ds_h($s['group_key']) ?>">
                                </td>

                                <td>
                                    <input type="hidden" name="rows[<?= $i ?>][text]" value="<?= ds_h($text) ?>">
                                    <strong><?= ds_h($text) ?></strong>
                                </td>

                                <td>
                                    <input class="form-control form-control-sm"
                                           dir="ltr"
                                           name="rows[<?= $i ?>][text_en]"
                                           value="<?= !$hasArabic ? ds_h($text) : '' ?>">
                                </td>

                                <td>
                                    <input class="form-control form-control-sm"
                                           dir="rtl"
                                           name="rows[<?= $i ?>][text_ar]"
                                           value="<?= $hasArabic ? ds_h($text) : '' ?>">
                                </td>

                                <td class="small text-muted"><?= ds_h($s['file_path']) ?></td>
                            </tr>
                        <?php endforeach; ?>

                        <?php if (!$suggestions): ?>
                            <tr>
                                <td colspan="7" class="text-center text-muted py-4">
                                    No missing labels detected.
                                </td>
                            </tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <button class="btn btn-success btn-lg rounded-pill px-5 mt-3">
                    Save Selected Suggestions
                </button>
            </div>
        </div>
    </form>
</div>
