<?php $pageTitle = t('update_title'); ?>
<div class="page-header">
    <div class="page-header-left">
        <a href="/dashboard" 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('dashboard') ?>
        </a>
        <h1 class="page-title"><?= t('update_title') ?></h1>
    </div>
</div>

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

<div class="settings-grid">
    <div class="card">
        <div class="card-header"><h2 class="card-title"><?= t('update_status') ?></h2></div>
        <div class="card-body">

            <div class="form-group">
                <label class="form-label"><?= t('update_installed_version') ?></label>
                <div class="form-input" style="background:var(--color-surface-alt,#f9fafb);cursor:default">
                    <?= Helper::e(APP_VERSION) ?>
                </div>
            </div>

            <div class="form-group">
                <label class="form-label"><?= t('update_available_version') ?></label>
                <div class="form-input" style="background:var(--color-surface-alt,#f9fafb);cursor:default">
                    <?php if ($info): ?>
                        <?= Helper::e($info['version']) ?>
                        <?php if (version_compare($info['version'], APP_VERSION, '>')): ?>
                            <span style="margin-left:.5rem;color:var(--color-success,#22c55e);font-weight:600">▲ <?= t('update_new') ?></span>
                        <?php elseif (version_compare($info['version'], APP_VERSION, '<')): ?>
                            <span style="margin-left:.5rem;color:var(--color-muted,#6b7280);"><?= t('update_preview') ?></span>
                        <?php else: ?>
                            <span style="margin-left:.5rem;color:var(--color-success,#22c55e);">✓ <?= t('update_latest') ?></span>
                        <?php endif; ?>
                    <?php else: ?>
                        <span style="color:var(--color-muted,#6b7280)"><?= t('update_check_failed') ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <?php if ($info && !empty($info['changelog'])): ?>
            <div class="form-group">
                <label class="form-label"><?= t('update_changelog') ?></label>
                <div class="form-input" style="background:var(--color-surface-alt,#f9fafb);cursor:default;white-space:pre-wrap"><?= Helper::e($info['changelog']) ?></div>
            </div>
            <?php endif; ?>

            <div style="display:flex;gap:.75rem;flex-wrap:wrap;margin-top:1rem">
                <a href="/admin/update?force=1" class="btn btn-ghost btn-sm">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0114.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0020.49 15"/></svg>
                    <?= t('update_force_check') ?>
                </a>

                <?php if ($info && version_compare($info['version'], APP_VERSION, '>')): ?>
                <form method="POST" data-confirm="<?= Helper::e(t('update_confirm')) ?>">
                    <?= Helper::csrf() ?>
                    <input type="hidden" name="action" value="perform">
                    <button type="submit" class="btn btn-primary">
                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14"><polyline points="8 17 12 21 16 17"/><line x1="12" y1="3" x2="12" y2="21"/></svg>
                        <?= t('update_now', ['version' => Helper::e($info['version'])]) ?>
                    </button>
                </form>
                <?php endif; ?>
            </div>

        </div>
    </div>

    <div class="card">
        <div class="card-header"><h2 class="card-title"><?= t('update_hints_title') ?></h2></div>
        <div class="card-body" style="font-size:.9rem;line-height:1.7;color:var(--color-muted,#6b7280)">
            <p><?= t('update_hint_backup') ?></p>
            <p><?= t('update_hint_data') ?></p>
            <p><?= t('update_hint_migrations') ?></p>
        </div>
    </div>
</div>
