<?php
$pageTitle = t('backup_review_title');
$counts    = $meta['counts'] ?? [];
$createdTs = (int)($meta['source_created_at'] ?? 0);
$createdFmt = $createdTs > 0 ? Helper::formatDateTime(date('Y-m-d H:i:s', $createdTs)) : '—';
?>
<div class="page-header">
    <div class="page-header-left">
        <a href="/admin/settings#backupCard" class="btn btn-ghost btn-sm">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
            <?= t('system_settings') ?>
        </a>
        <h1 class="page-title"><?= t('backup_review_title') ?></h1>
    </div>
</div>

<div class="card">
    <div class="card-header"><h2 class="card-title"><?= t('backup_review_summary') ?></h2></div>
    <div class="card-body">
        <p class="form-hint" style="margin:0 0 1rem"><?= t('backup_review_intro') ?></p>

        <table class="data-table" style="margin-bottom:1.5rem">
            <tbody>
                <tr>
                    <th style="text-align:left;width:40%"><?= t('backup_review_source_app_name') ?></th>
                    <td><?= Helper::e($meta['source_app_name'] ?? '') ?: '—' ?></td>
                </tr>
                <tr>
                    <th style="text-align:left"><?= t('backup_review_source_version') ?></th>
                    <td><code><?= Helper::e($meta['source_version'] ?? '') ?></code></td>
                </tr>
                <tr>
                    <th style="text-align:left"><?= t('backup_review_created') ?></th>
                    <td><?= Helper::e($createdFmt) ?></td>
                </tr>
                <tr>
                    <th style="text-align:left"><?= t('backup_review_encrypted') ?></th>
                    <td><?= !empty($meta['was_encrypted']) ? t('backup_review_yes') : t('backup_review_no') ?></td>
                </tr>
                <?php if (!empty($counts)): ?>
                <tr>
                    <th style="text-align:left"><?= t('backup_review_counts') ?></th>
                    <td>
                        <?php
                        $labels = [
                            'users'             => t('users'),
                            'projects'          => t('projects'),
                            'tasks'             => t('tasks'),
                            'attachments'       => t('attachments'),
                            'project_externals' => t('externals'),
                        ];
                        $parts = [];
                        foreach ($counts as $tbl => $n) {
                            $label = $labels[$tbl] ?? $tbl;
                            $parts[] = Helper::e($label) . ': <strong>' . (int)$n . '</strong>';
                        }
                        echo implode(' &middot; ', $parts);
                        ?>
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>

        <form method="POST" action="/admin/backup/restore?step=apply" data-confirm="<?= Helper::e(t('backup_review_apply_confirm')) ?>">
            <?= Helper::csrf() ?>
            <input type="hidden" name="token" value="<?= Helper::e($token) ?>">

            <h3 style="margin:0 0 6px;font-size:.95rem;border-top:1px solid var(--border-color);padding-top:1rem"><?= t('backup_review_target_url') ?></h3>
            <p class="form-hint" style="margin:0 0 12px"><?= t('backup_review_target_url_intro') ?></p>

            <div class="form-group" style="max-width:520px">
                <label class="form-label"><?= t('backup_review_app_url_label') ?></label>
                <input type="text" name="new_app_url" class="form-input"
                       value="<?= Helper::e($meta['source_app_url'] ?? '') ?>"
                       placeholder="https://example.com">
                <span class="form-hint"><?= t('backup_review_app_url_hint') ?></span>
            </div>

            <div class="alert alert-error" style="margin:1rem 0">
                <?= t('backup_review_url_change_warn') ?>
            </div>

            <div style="display:flex;gap:.5rem;flex-wrap:wrap;margin-top:1rem">
                <button type="submit" class="btn btn-danger btn-sm"><?= t('backup_review_apply_btn') ?></button>
            </div>
        </form>

        <form method="POST" action="/admin/backup/restore?step=cancel" style="margin-top:.75rem">
            <?= Helper::csrf() ?>
            <input type="hidden" name="token" value="<?= Helper::e($token) ?>">
            <button type="submit" class="btn btn-ghost btn-sm"><?= t('backup_review_cancel_btn') ?></button>
        </form>
    </div>
</div>
