<?php ob_start(); ?>

<?php admin_content_header(__('admin.backup.restore_title') . ' – ' . __('admin.backup.confirm_title'), 'hard-drive', [
    ['url' => '/admin/backup#restore', 'label' => __('admin.common.cancel')],
]); ?>

<style>
.confirm-box { background:#fffbeb; border:2px solid #f59e0b; border-radius:10px; padding:1.5rem; margin-bottom:1.5rem; }
.confirm-box h3 { margin:0 0 0.75rem; color:#92400e; font-size:1.1rem; }
.confirm-box p  { margin:0 0 0.5rem; color:#78350f; font-size:0.9rem; line-height:1.6; }
.confirm-box ul { margin:0.5rem 0 0; padding-left:1.25rem; color:#78350f; font-size:0.9rem; }
.confirm-hint { background:#fef2f2; border:1px solid #fca5a5; border-radius:8px; padding:0.875rem 1.1rem; margin-bottom:1.25rem; color:#7f1d1d; font-size:0.875rem; line-height:1.6; }
.confirm-hint strong { display:block; margin-bottom:0.2rem; }
.manifest-table { width:100%; border-collapse:collapse; font-size:0.9rem; }
.manifest-table th, .manifest-table td { padding:0.5rem 0.75rem; border-bottom:1px solid var(--border-color,#e5e7eb); text-align:left; }
.manifest-table th { width:160px; color:var(--gray-500,#6b7280); font-weight:500; }
.version-mismatch { color:#b45309; font-weight:600; }
</style>

<div class="confirm-box">
    <h3><?= __('admin.backup.confirm_warning_heading') ?></h3>
    <p><?= __('admin.backup.confirm_warning_text') ?></p>
    <ul>
        <li><?= __('admin.backup.confirm_warning_db') ?></li>
        <li><?= __('admin.backup.confirm_warning_files') ?></li>
    </ul>
    <p style="margin-top:0.75rem;"><strong><?= __('admin.backup.confirm_irreversible') ?></strong>
    <?= __('admin.backup.confirm_backup_first') ?></p>
</div>

<?php
$backupCmsVersion  = $manifest['cms_version'] ?? '?';
$currentCmsVersion = $currentVersion ?? '?';
$versionMismatch   = ($backupCmsVersion !== '?' && $currentCmsVersion !== '?'
                      && $backupCmsVersion !== $currentCmsVersion);
?>

<?php if ($versionMismatch): ?>
<div class="confirm-hint">
    <strong><?= __('admin.backup.confirm_version_heading') ?></strong>
    <?= __('admin.backup.confirm_version_text', [':backup_version' => e($backupCmsVersion), ':current_version' => e($currentCmsVersion)]) ?>
</div>
<?php endif; ?>

<div class="form-card">
    <h4><?= __('admin.backup.confirm_info_heading') ?></h4>
    <table class="manifest-table">
        <tr>
            <th><?= __('admin.backup.confirm_created_at') ?></th>
            <td><?= e($manifest['created_at'] ?? '–') ?></td>
        </tr>
        <tr>
            <th><?= __('admin.backup.confirm_website') ?></th>
            <td><?= e($manifest['site_name'] ?? '–') ?></td>
        </tr>
        <tr>
            <th><?= __('admin.backup.confirm_cms_version') ?></th>
            <td class="<?= $versionMismatch ? 'version-mismatch' : '' ?>"><?= e($backupCmsVersion) ?>
                <?php if ($versionMismatch): ?>
                    <small>(<?= __('admin.backup.confirm_current_version', [':version' => e($currentCmsVersion)]) ?>)</small>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <th><?= __('admin.backup.confirm_format') ?></th>
            <td>iw_backup v<?= e($manifest['backup_format'] ?? '?') ?></td>
        </tr>
        <tr>
            <th><?= __('admin.backup.confirm_php_version') ?></th>
            <td><?= e($manifest['php_version'] ?? '–') ?></td>
        </tr>
    </table>
</div>

<div class="form-card" style="background:#f0fdf4;border-color:#86efac;">
    <h4 style="color:#166534;"><?= __('admin.backup.confirm_protected_heading') ?></h4>
    <ul style="margin:0.25rem 0;padding-left:1.25rem;color:#15803d;font-size:0.9rem;">
        <li><code>app/Config/database.php</code> – <?= __('admin.backup.confirm_protected_db') ?></li>
        <li><code>app/Config/app.php</code> – <?= __('admin.backup.confirm_protected_app') ?></li>
        <li><?= __('admin.backup.confirm_protected_core') ?></li>
    </ul>
    <p style="margin:0.5rem 0 0;font-size:0.85rem;color:#166534;">
        <?= __('admin.backup.confirm_uploads_note') ?>
    </p>
</div>

<div class="confirm-hint" style="background:#eff6ff;border-color:#93c5fd;color:#1e3a5f;">
    <strong><?= __('admin.backup.confirm_theme_heading') ?></strong>
    <?= __('admin.backup.confirm_theme_text') ?>
</div>

<form method="POST" action="/admin/backup/restore/execute" id="restoreExecuteForm">
    <?= $csrf->field() ?>

    <div style="display:flex;gap:1rem;align-items:center;flex-wrap:wrap;margin-top:1.5rem;">
        <button type="submit" id="restoreExecuteBtn" class="btn btn-danger"
                onclick="return confirm('<?= _e('admin.backup.confirm_js_confirm') ?>')">
            <?= __('admin.backup.confirm_execute_btn') ?>
        </button>
        <a href="/admin/backup#restore" class="btn btn-secondary"><?= __('admin.common.cancel') ?></a>
    </div>

    <!-- LRA-Status: wird eingeblendet sobald das Formular abgesendet wird -->
    <div class="lra-status" id="lraRestoreStatus" role="status" aria-live="polite">
        <div class="lra-spinner" aria-hidden="true"></div>
        <div class="lra-status-body">
            <div class="lra-status-heading"><?= __('admin.backup.lra_restore_heading') ?></div>
            <ul class="lra-steps">
                <li class="lra-step-current"><?= __('admin.backup.lra_restore_step1') ?></li>
                <li><?= __('admin.backup.lra_restore_step2') ?></li>
                <li><?= __('admin.backup.lra_restore_step3') ?></li>
                <li><?= __('admin.backup.lra_restore_step4') ?></li>
            </ul>
        </div>
    </div>
</form>

<script>
(function () {
    var form = document.getElementById('restoreExecuteForm');
    var btn  = document.getElementById('restoreExecuteBtn');
    var stat = document.getElementById('lraRestoreStatus');
    if (!form || !btn) return;

    form.addEventListener('submit', function () {
        // Verzögertes Deaktivieren: onclick-confirm() läuft zuerst,
        // dann submitted der Browser – erst danach deaktivieren wir.
        setTimeout(function () {
            btn.disabled = true;
            btn.innerHTML = '<span class="lra-btn-spinner" aria-hidden="true"></span><?= __('admin.common.processing') ?>';
            if (stat) stat.classList.add('lra-active');
        }, 0);
    });
}());
</script>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
