<?php
$page_title  = 'Downloads';
$page_active = 'downloads';
require __DIR__ . '/includes/page_header.php';
?>

<div class="text-center mb-6">
    <h2 class="text-2xl font-bold text-white flex items-center justify-center gap-3">
        <i class="ph ph-download-simple text-primary-400"></i> DOWNLOADS
    </h2>
</div>

<div class="grid grid-cols-1 lg:grid-cols-2 gap-5">
    <div class="card">
        <div class="card-head"><h3><i class="ph ph-android-logo text-primary-400"></i> APK Build</h3></div>
        <div class="p-5 space-y-3 text-sm">
            <div class="flex items-center justify-between">
                <span class="text-dark-400">Version</span>
                <span class="text-white font-semibold" id="dl-app-version">—</span>
            </div>
            <div class="flex items-center justify-between">
                <span class="text-dark-400">Direct link</span>
                <a href="#" id="dl-apk-link" class="text-primary-400 hover:text-primary-300 mono text-xs truncate max-w-[260px]">—</a>
            </div>
            <p class="text-xs text-dark-500 mt-2">
                Configured under <a href="app_config.php" class="text-primary-400">Configuration → App Settings</a>.
            </p>
        </div>
    </div>

    <div class="card">
        <div class="card-head"><h3><i class="ph ph-cloud-arrow-down text-primary-400"></i> Panel Backup</h3></div>
        <div class="p-5 space-y-3 text-sm">
            <p class="text-dark-300">Save a complete copy of your panel as a ZIP file. You can use Restore later to bring everything back.</p>
            <div class="flex gap-2">
                <a href="api/backup.php?action=download" class="btn-primary text-xs">
                    <i class="ph ph-download-simple"></i> Download backup.zip
                </a>
                <button onclick="document.getElementById('restore-file').click()" class="btn-outline text-xs">
                    <i class="ph ph-upload-simple"></i> Restore from ZIP
                </button>
                <input type="file" id="restore-file" accept=".zip" class="hidden">
            </div>
        </div>
    </div>
</div>

<script>
(async function () {
    try {
        const { config } = await api.get('api/config.php');
        document.getElementById('dl-app-version').textContent = config.app_version || '—';
        const link = config.apk_link || '';
        const a = document.getElementById('dl-apk-link');
        if (link) { a.href = link; a.textContent = link; a.target = '_blank'; }
    } catch (e) { /* config endpoint shouldn't fail; if it does, the placeholders stay */ }
})();

document.getElementById('restore-file').addEventListener('change', async (e) => {
    const f = e.target.files[0];
    if (!f) return;
    if (!(await confirmDanger('This will replace all panel data with the contents of the backup. A safety copy of the current data is saved first. Continue?'))) return;
    const fd = new FormData();
    fd.append('file', f);
    fd.append('confirm', 'YES');
    fd.append('action', 'restore');
    try {
        const r = await fetch('api/restore.php', {
            method: 'POST', credentials: 'same-origin',
            headers: { 'X-CSRF-Token': document.querySelector('meta[name=csrf-token]').content,
                       'X-Requested-With': 'XMLHttpRequest' },
            body: fd,
        });
        const out = await r.json();
        if (out.ok) {
            toast('Backup restored — please reload the page.', 'success');
        } else {
            toast('Restore failed: ' + (out.error || r.status), 'error');
        }
    } catch (err) { toast(err.message, 'error'); }
});
</script>

<?php require __DIR__ . '/includes/page_footer.php'; ?>
