<?php ob_start(); ?>

<div class="content-header">
    <h2><?= __('admin.backup.update_title') ?> – <?= __('admin.backup.update_confirm_title') ?></h2>
    <a href="/admin/backup#update" class="btn btn-secondary btn-sm">← <?= __('admin.common.cancel') ?></a>
</div>

<style>
.update-info-box { background:#eff6ff; border:2px solid #93c5fd; border-radius:10px; padding:1.5rem; margin-bottom:1.5rem; }
.update-info-box h3 { margin:0 0 0.75rem; color:#1e40af; font-size:1.1rem; }
.update-info-box p  { margin:0 0 0.5rem; color:#1e3a8a; font-size:0.9rem; line-height:1.6; }
.update-warning { background:#fffbeb; border:1px solid #fcd34d; border-radius:8px; padding:1rem 1.25rem; margin-bottom:1.25rem; color:#92400e; font-size:0.9rem; line-height:1.6; }
.update-warning strong { display:block; margin-bottom:0.2rem; }
.preflight-warnings { background:#fff7ed; border:1px solid #fed7aa; border-radius:8px; padding:1rem 1.25rem; margin-bottom:1.25rem; color:#7c2d12; font-size:0.9rem; }
.preflight-warnings strong { display:block; margin-bottom:0.4rem; }
.preflight-warnings li { margin-bottom:0.25rem; }
.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:180px; color:var(--gray-500,#6b7280); font-weight:500; }
.version-arrow { font-size:1.1rem; margin:0 0.5rem; color:var(--admin-primary,#2563eb); }
.version-badge { display:inline-block; border-radius:4px; padding:0.15rem 0.5rem; font-weight:600; font-size:0.85rem; }
.version-badge.current { background:#e5e7eb; color:#374151; }
.version-badge.new { background:#dbeafe; color:#1e40af; }
</style>

<div class="update-info-box">
    <h3><?= __('admin.backup.update_checked_heading') ?></h3>
    <p>
        <?= __('admin.backup.update_checked_text') ?>
    </p>
</div>

<div class="update-warning">
    <strong><?= __('admin.backup.update_recommend') ?></strong>
    <?= __('admin.backup.update_warning_text') ?>
</div>

<?php if (!empty($preflightWarnings)): ?>
<div class="preflight-warnings">
    <strong><?= __('admin.backup.update_preflight_heading') ?></strong>
    <ul style="margin:0;padding-left:1.25rem;">
        <?php foreach ($preflightWarnings as $warning): ?>
        <li><?= e($warning) ?></li>
        <?php endforeach; ?>
    </ul>
</div>
<?php endif; ?>

<div class="form-card">
    <h4><?= __('admin.backup.update_info_heading') ?></h4>
    <table class="manifest-table">
        <tr>
            <th><?= __('admin.backup.update_info_version') ?></th>
            <td>
                <span class="version-badge current"><?= e($currentVersion) ?></span>
                <span class="version-arrow">→</span>
                <span class="version-badge new"><?= e($manifest['target_version'] ?? '?') ?></span>
            </td>
        </tr>
        <?php if (!empty($manifest['description'])): ?>
        <tr>
            <th><?= __('admin.backup.update_info_desc') ?></th>
            <td><?= e($manifest['description']) ?></td>
        </tr>
        <?php endif; ?>
        <?php if (!empty($manifest['released_at'])): ?>
        <tr>
            <th><?= __('admin.backup.update_info_released') ?></th>
            <td><?= e($manifest['released_at']) ?></td>
        </tr>
        <?php endif; ?>
        <?php if (!empty($manifest['min_version'])): ?>
        <tr>
            <th><?= __('admin.backup.update_info_min_version') ?></th>
            <td><?= e($manifest['min_version']) ?></td>
        </tr>
        <?php endif; ?>
        <tr>
            <th><?= __('admin.backup.update_info_format') ?></th>
            <td>iw_update v<?= e($manifest['format_version'] ?? '1') ?></td>
        </tr>
    </table>
</div>

<div class="form-card" style="background:#f0fdf4;border-color:#86efac;">
    <h4 style="color:#166534;"><?= __('admin.backup.update_protected_heading') ?></h4>
    <ul style="margin:0.25rem 0;padding-left:1.25rem;color:#15803d;font-size:0.9rem;">
        <li><code>public/uploads/</code> – <?= __('admin.backup.update_protected_uploads') ?></li>
        <li><code>storage/</code> – <?= __('admin.backup.update_protected_storage') ?></li>
        <li><code>app/Config/database.php</code> – <?= __('admin.backup.update_protected_db') ?></li>
        <li><code>app/Config/app.php</code> – <?= __('admin.backup.update_protected_config') ?></li>
    </ul>
    <p style="margin:0.5rem 0 0;font-size:0.85rem;color:#166534;">
        <?= __('admin.backup.update_migrations_note') ?>
    </p>
</div>

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

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

    <!-- LRA-Status: wird eingeblendet sobald das Formular abgesendet wird -->
    <div class="lra-status" id="lraUpdateStatus" 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_update_heading') ?></div>
            <ul class="lra-steps">
                <li class="lra-step-current"><?= __('admin.backup.lra_update_step1') ?></li>
                <li><?= __('admin.backup.lra_update_step2') ?></li>
                <li><?= __('admin.backup.lra_update_step3') ?></li>
                <li><?= __('admin.backup.lra_update_step4') ?></li>
            </ul>
        </div>
    </div>
</form>

<script>
(function () {
    var form = document.getElementById('updateExecuteForm');
    var btn  = document.getElementById('updateExecuteBtn');
    var stat = document.getElementById('lraUpdateStatus');
    if (!form || !btn) return;

    form.addEventListener('submit', function () {
        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'; ?>
