<?php
$s = get_settings();
$delay = (int)($s['popup_delay_seconds'] ?? 10);
$build_version = defined('NADIPLAY_BUILD_VERSION') ? NADIPLAY_BUILD_VERSION : '1.2.3';
$saved_version = trim((string)($s['app_version'] ?? '1.0.0'));
?>
<header class="page-head">
    <h1>Update Popup</h1>
    <p class="page-sub">
        Controls the in-app update dialog. The <em>delay timer</em> below is what you asked for —
        the popup will wait <strong>this many seconds</strong> after the app loads before
        appearing, giving you time to log in and edit settings even when a forced update is set.
    </p>
</header>

<?php if ($saved_version !== $build_version): ?>
<section class="card" style="background:linear-gradient(180deg,rgba(40,30,15,.55),rgba(20,15,8,.55));border-color:rgba(226,177,80,.45);">
    <h2 style="color:var(--warn)">⚠️ This server now ships build v<?= h($build_version) ?></h2>
    <p class="hint" style="margin-top:6px;line-height:1.6">
        Your saved <strong>Latest version</strong> below is still
        <code><?= h($saved_version) ?></code>, so <code>/api/public/app-info</code>
        keeps returning that value. Set it to <code><?= h($build_version) ?></code> below
        and click <em>Save</em> to match the deployed build.
    </p>
</section>
<?php endif; ?>

<section class="card" style="background:linear-gradient(180deg,rgba(20,40,30,.55),rgba(10,20,16,.55));border-color:rgba(86,192,141,.35);">
    <h2 style="color:var(--good)">How versioning works</h2>
    <p class="hint" style="margin-top:6px">
        <strong>Each new APK release must bump the version number.</strong> If the current
        APK is <code>1.0.0</code>, your next build must be <code>1.0.1</code> (or higher).
        After the user installs the new APK, the popup will <strong>not appear again</strong>
        because the app reads its <em>real</em> installed version from
        <code>android/app/build.gradle → versionName</code> at runtime.
    </p>
    <ol class="hint" style="margin-top:6px;padding-left:18px;line-height:1.7">
        <li>In <code>android/app/build.gradle</code>: change <code>versionName "1.0.0"</code> → <code>"1.0.1"</code> and bump <code>versionCode</code>.</li>
        <li>Rebuild the APK and upload it to your server.</li>
        <li>Below, set <strong>Latest version</strong> = <code>1.0.1</code> and the new APK URL → Save.</li>
        <li>Users on the OLD APK see the popup once. After they install the new APK, popup is gone for them.</li>
    </ol>
    <p class="hint" style="margin-top:6px">
        Tip: the helper script <code>scripts/bump-version.sh 1.0.1</code> in the repo
        does step 1 for you in one command.
    </p>
</section>

<form method="post" action="?page=update" class="card">
    <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
    <input type="hidden" name="action" value="save_update">

    <h2>Visibility</h2>
    <label class="check">
        <input type="checkbox" name="update_enabled" value="1" <?= !empty($s['update_enabled']) ? 'checked' : '' ?>>
        <span>Enable update popup (when off, no popup is shown to anyone)</span>
    </label>

    <h2 style="margin-top:24px">Versions</h2>
    <div class="grid-2">
        <label class="field">
            <span>Latest version <small>(shown to users)</small></span>
            <input type="text" name="app_version" placeholder="<?= h($build_version) ?>" value="<?= h($s['app_version'] ?? $build_version) ?>" required>
        </label>
        <label class="field">
            <span>Minimum supported version <small>(force-update below this)</small></span>
            <input type="text" name="min_supported_version" placeholder="1.0.0" value="<?= h($s['min_supported_version'] ?? '1.0.0') ?>" required>
        </label>
    </div>

    <h2 style="margin-top:24px">Delay timer</h2>
    <p class="hint">
        Number of seconds the popup will wait <strong>after the app loads</strong> before showing.
        Increase this to <strong>10–14 seconds</strong> (or higher) if a forced update is making it
        hard to log in. Range: 0–600 seconds.
    </p>
    <div class="grid-2 align-end">
        <label class="field">
            <span>Popup delay (seconds)</span>
            <input
                type="number"
                name="popup_delay_seconds"
                min="0" max="600" step="1"
                value="<?= h((string)$delay) ?>"
                id="delay-input"
                oninput="document.getElementById('delay-range').value = this.value; document.getElementById('delay-out').textContent = this.value + 's';"
            >
        </label>
        <label class="field">
            <span>Quick slider (0–60s)</span>
            <input
                type="range"
                id="delay-range"
                min="0" max="60" step="1"
                value="<?= h((string)min(60, $delay)) ?>"
                oninput="document.getElementById('delay-input').value = this.value; document.getElementById('delay-out').textContent = this.value + 's';"
            >
            <output id="delay-out"><?= h((string)$delay) ?>s</output>
        </label>
    </div>

    <h2 style="margin-top:24px">Download links</h2>
    <div class="grid-2">
        <label class="field">
            <span>Direct APK URL</span>
            <input type="url" name="update_apk_url" placeholder="https://4kmovies.vip/nadiplayer-<?= h($build_version) ?>.apk" value="<?= h($s['update_apk_url'] ?? '') ?>">
        </label>
        <label class="field">
            <span>Play Store URL</span>
            <input type="url" name="update_play_url" placeholder="https://play.google.com/store/apps/details?id=..." value="<?= h($s['update_play_url'] ?? '') ?>">
        </label>
    </div>

    <h2 style="margin-top:24px">Changelog ("What's new")</h2>
    <label class="field">
        <textarea name="update_changelog" rows="6" placeholder="• Fixed crash on Live TV&#10;• Added Korean language&#10;• Performance improvements"><?= h($s['update_changelog'] ?? '') ?></textarea>
    </label>

    <div class="actions">
        <button type="submit" class="btn-primary">Save update settings</button>
    </div>
</form>
