{{define "app_export"}}
{{template "layout_start" .}}

<div class="page-header">
    <div style="display:flex;align-items:center;gap:1rem">
        <a href="/stacks" class="btn btn-sm btn-outline">&larr; Alkalmazások</a>
        <h2>{{.Stack.Meta.DisplayName}} &mdash; Exportálás</h2>
    </div>
</div>

<div class="card" style="max-width:700px">
    <h3>Mentés helye</h3>
    <select id="destDrive" onchange="loadEstimate()" style="width:100%;padding:.5rem;margin-bottom:1rem">
        <option value="">Válassz tárolót...</option>
        {{range .Drives}}
        <option value="{{.Path}}">{{if .Label}}{{.Label}} ({{.Path}}){{else}}{{.Path}}{{end}}</option>
        {{end}}
    </select>

    <div id="estimateBox" style="display:none;background:var(--bg-2);border-radius:8px;padding:1rem;margin-bottom:1.5rem">
        <div style="display:flex;justify-content:space-between;margin-bottom:.25rem">
            <span>Konfiguráció:</span>
            <span id="estConfig">-</span>
        </div>
        <div style="display:flex;justify-content:space-between;margin-bottom:.25rem">
            <span>Felhasználói adatok:</span>
            <span id="estData">-</span>
        </div>
        <div style="display:flex;justify-content:space-between;font-weight:600;margin-bottom:.25rem;border-top:1px solid var(--line);padding-top:.5rem">
            <span>Összesen:</span>
            <span><span id="estTotal">-</span> <span id="estFree" style="color:var(--text-3)">(szabad: -)</span></span>
        </div>
        <div style="display:flex;justify-content:space-between">
            <span>Becsült idő:</span>
            <span id="estTime">-</span>
        </div>
        <div id="estWarning" style="display:none;color:var(--crit);margin-top:.5rem;font-weight:600"></div>
    </div>

    <div id="classSelect" style="display:none;margin-bottom:1.5rem">
        <div id="mandBox" style="display:none;margin-bottom:1rem">
            <div style="font-weight:600;margin-bottom:.25rem">Mindig része a mentésnek:</div>
            <div id="mandList" style="color:var(--text-3);font-size:.9rem"></div>
        </div>
        <div id="optBox" style="display:none;margin-bottom:1rem">
            <div style="font-weight:600;margin-bottom:.25rem">Választható tartalom:</div>
            <div id="optList"></div>
        </div>
        <div id="exclBox" style="display:none">
            <div id="exclSummary" style="margin-bottom:.25rem"></div>
            <div style="color:var(--text-3);font-size:.85rem;margin-bottom:.5rem">A kihagyott mappák tartalma a Fájlkezelőben bármikor elérhető, és külön is lementhető.</div>
            <button type="button" class="btn btn-xs btn-outline" onclick="toggleExcl()" id="exclToggle">Kihagyott tartalom megjelenítése</button>
            <div id="exclList" style="display:none;margin-top:.5rem"></div>
        </div>
    </div>

    <h3>Jelszó (opcionális)</h3>
    <div style="display:flex;gap:.5rem;margin-bottom:1rem">
        <input type="password" id="exportPassword" placeholder="Titkosítási jelszó" style="flex:1;padding:.5rem">
        <button class="btn btn-sm btn-outline" onclick="togglePw()" type="button" title="Jelszó mutatása">&#128065;</button>
    </div>

    <label style="display:flex;align-items:flex-start;gap:.5rem;margin-bottom:.5rem;cursor:pointer">
        <input type="checkbox" id="stopApp" checked style="margin-top:3px">
        <div>
            <strong>Alkalmazás leállítása mentés előtt (ajánlott)</strong>
            <div style="color:var(--text-3);font-size:.85rem">Az adatok konzisztenciája érdekében javasolt leállítani az alkalmazást mentés közben.</div>
        </div>
    </label>

    <button id="startBtn" class="btn btn-primary" onclick="startExport()" style="margin-top:1rem;width:100%" disabled>
        Exportálás indítása
    </button>
</div>

<div id="progressCard" class="card" style="max-width:700px;display:none">
    <h3>Folyamat</h3>
    <div id="progressSteps"></div>
    <div id="progressError" style="display:none;color:var(--crit);margin-top:1rem;font-weight:600"></div>
</div>

<div id="doneCard" class="card" style="max-width:700px;display:none">
    <h3 style="color:var(--blue)">Kész!</h3>
    <div style="margin-bottom:1rem">
        <span id="doneFile" style="font-weight:600"></span>
        <span id="doneSize" style="color:var(--text-3)"></span>
    </div>
    <a id="doneFBLink" href="#" target="_blank" class="btn btn-outline">Megnyitás FileBrowser-ben &nearr;</a>
</div>

<script>
var stackName = '{{.Stack.Name}}';
var domain = '{{.Stack.Meta.Subdomain}}' ? '{{.Stack.Meta.Subdomain}}.{{$.Domain}}' : '';
var pollTimer = null;

function csrfH() {
    var el = document.querySelector('meta[name="csrf-token"]');
    return el ? {'X-CSRF-Token': el.content, 'Content-Type': 'application/json'} : {'Content-Type': 'application/json'};
}

function togglePw() {
    var inp = document.getElementById('exportPassword');
    inp.type = inp.type === 'password' ? 'text' : 'password';
}

async function loadEstimate() {
    var drive = document.getElementById('destDrive').value;
    var box = document.getElementById('estimateBox');
    var btn = document.getElementById('startBtn');

    if (!drive) {
        box.style.display = 'none';
        btn.disabled = true;
        return;
    }

    try {
        var resp = await fetch('/api/export/estimate?stack=' + encodeURIComponent(stackName) + '&drive=' + encodeURIComponent(drive));
        var data = await resp.json();
        if (!data.ok) {
            box.style.display = 'none';
            btn.disabled = true;
            return;
        }
        var est = data.data;
        document.getElementById('estConfig').textContent = est.config_size_human;
        document.getElementById('estData').textContent = est.data_size_human;
        document.getElementById('estTotal').textContent = '~' + est.total_size_human;
        document.getElementById('estFree').textContent = '(szabad: ' + est.dest_free_human + ')';
        document.getElementById('estTime').textContent = '~' + est.estimated_minutes + ' perc';

        var warn = document.getElementById('estWarning');
        if (!est.fits_on_dest) {
            warn.textContent = 'Nincs elég szabad hely a kiválasztott tárolón!';
            warn.style.display = 'block';
            btn.disabled = true;
        } else {
            warn.style.display = 'none';
            btn.disabled = false;
        }
        box.style.display = 'block';
        renderClassSelect(est);
    } catch(e) {
        console.error('Estimate error:', e);
    }
}

// Task 4: the class-scoped selection UI (classified apps only; legacy apps see the plain estimate).
var lastEst = null;
function humanBytes(b) {
    if (b >= 1073741824) return (b/1073741824).toFixed(1) + ' GB';
    if (b >= 1048576) return (b/1048576).toFixed(1) + ' MB';
    if (b >= 1024) return (b/1024).toFixed(1) + ' KB';
    return b + ' B';
}
function renderClassSelect(est) {
    lastEst = est;
    var wrap = document.getElementById('classSelect');
    if (!est.has_classification) { wrap.style.display = 'none'; return; }
    wrap.style.display = 'block';

    var mand = est.mandatory_items || [], opt = est.optional_items || [], excl = est.excluded_items || [];
    // Mandatory (locked).
    var mb = document.getElementById('mandBox');
    if (mand.length) {
        mb.style.display = 'block';
        document.getElementById('mandList').innerHTML = mand.map(function(i){
            return '<div>' + esc(i.rel_path) + ' <span style="opacity:.7">(' + i.human + ')</span></div>';
        }).join('');
    } else { mb.style.display = 'none'; }
    // Optional (pre-selected checkboxes).
    var ob = document.getElementById('optBox');
    if (opt.length) {
        ob.style.display = 'block';
        document.getElementById('optList').innerHTML = opt.map(function(i){
            return '<label style="display:flex;gap:.5rem;align-items:center;cursor:pointer;padding:.15rem 0">' +
                '<input type="checkbox" class="fab-opt" checked data-key="' + esc(i.key) + '" data-bytes="' + i.bytes + '" onchange="recomputeTotal()">' +
                '<span>' + esc(i.rel_path) + ' <span style="opacity:.7">(' + i.human + ')</span></span></label>';
        }).join('');
    } else { ob.style.display = 'none'; }
    // Excluded (opt-in, collapsed, behind the two-number warning).
    var eb = document.getElementById('exclBox');
    if (excl.length) {
        eb.style.display = 'block';
        document.getElementById('exclList').innerHTML = excl.map(function(i){
            return '<label style="display:flex;gap:.5rem;align-items:center;cursor:pointer;padding:.15rem 0">' +
                '<input type="checkbox" class="fab-excl" data-key="' + esc(i.key) + '" data-bytes="' + i.bytes + '" onchange="recomputeTotal()">' +
                '<span>' + esc(i.rel_path) + ' <span style="opacity:.7">(' + i.human + ')</span></span></label>';
        }).join('');
    } else { eb.style.display = 'none'; }
    recomputeTotal();
}
function recomputeTotal() {
    if (!lastEst) return;
    var base = lastEst.base_bytes || 0;
    var optSum = 0, exclSum = 0, exclAll = 0;
    document.querySelectorAll('.fab-opt').forEach(function(cb){ if (cb.checked) optSum += parseInt(cb.dataset.bytes,10)||0; });
    document.querySelectorAll('.fab-excl').forEach(function(cb){ var b = parseInt(cb.dataset.bytes,10)||0; exclAll += b; if (cb.checked) exclSum += b; });
    var alap = base + optSum;
    var full = alap + exclAll;
    var s = document.getElementById('exclSummary');
    if ((lastEst.excluded_items||[]).length) {
        s.innerHTML = 'Alap mentés: ~' + humanBytes(alap) + '. A kihagyott, nagy méretű tartalommal együtt: ~' + humanBytes(full) + '.';
    }
}
function toggleExcl() {
    var l = document.getElementById('exclList');
    var shown = l.style.display !== 'none';
    l.style.display = shown ? 'none' : 'block';
    document.getElementById('exclToggle').textContent = shown ? 'Kihagyott tartalom megjelenítése' : 'Kihagyott tartalom elrejtése';
}
function gatherSelections() {
    var deselect = [], optIn = [];
    document.querySelectorAll('.fab-opt').forEach(function(cb){ if (!cb.checked) deselect.push(cb.dataset.key); });
    document.querySelectorAll('.fab-excl').forEach(function(cb){ if (cb.checked) optIn.push(cb.dataset.key); });
    return { deselect_optional: deselect, opt_in_excluded: optIn };
}
function esc(s) { return String(s).replace(/[&<>"]/g, function(c){ return {'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'}[c]; }); }

async function startExport() {
    var drive = document.getElementById('destDrive').value;
    var password = document.getElementById('exportPassword').value;
    var stopApp = document.getElementById('stopApp').checked;

    document.getElementById('startBtn').disabled = true;
    document.getElementById('progressCard').style.display = 'block';
    document.getElementById('doneCard').style.display = 'none';

    try {
        var sel = gatherSelections();
        var resp = await fetch('/api/export/start', {
            method: 'POST',
            headers: csrfH(),
            body: JSON.stringify({
                stack_name: stackName,
                dest_drive: drive,
                password: password,
                stop_app: stopApp,
                deselect_optional: sel.deselect_optional,
                opt_in_excluded: sel.opt_in_excluded
            })
        });
        var data = await resp.json();
        if (!data.ok) {
            showError(data.error || 'Hiba történt');
            return;
        }
        pollStatus();
    } catch(e) {
        showError(e.message);
    }
}

function pollStatus() {
    if (pollTimer) clearInterval(pollTimer);
    pollTimer = setInterval(async function() {
        try {
            var resp = await fetch('/api/export/status');
            var data = await resp.json();
            renderSteps(data.steps || []);

            if (data.error) {
                showError(data.error);
                clearInterval(pollTimer);
                return;
            }

            if (data.done && !data.error) {
                clearInterval(pollTimer);
                showDone(data);
            }
        } catch(e) {
            console.error('Poll error:', e);
        }
    }, 1000);
}

function renderSteps(steps) {
    var html = '';
    for (var i = 0; i < steps.length; i++) {
        var s = steps[i];
        var icon = '&#9675;'; // pending
        if (s.status === 'running') icon = '&#10227;';
        if (s.status === 'done') icon = '&#10003;';
        if (s.status === 'failed') icon = '&#10007;';
        var cls = s.status === 'failed' ? 'color:var(--crit)' : s.status === 'done' ? 'color:var(--blue)' : s.status === 'running' ? 'color:var(--blue)' : '';
        html += '<div style="padding:.25rem 0;' + cls + '">' + icon + ' ' + s.label;
        if (s.error) html += ' <span style="font-size:.85rem">(' + s.error + ')</span>';
        html += '</div>';
    }
    document.getElementById('progressSteps').innerHTML = html;
}

function showError(msg) {
    var el = document.getElementById('progressError');
    el.textContent = msg;
    el.style.display = 'block';
    document.getElementById('startBtn').disabled = false;
}

function showDone(data) {
    document.getElementById('progressCard').style.display = 'none';
    document.getElementById('doneCard').style.display = 'block';
    var fileName = (data.output_path || '').split('/').pop();
    document.getElementById('doneFile').textContent = fileName;
    document.getElementById('doneSize').textContent = data.output_size ? '(' + data.output_size + ')' : '';

    // Build FileBrowser link to the exports directory
    var drive = document.getElementById('destDrive').value;
    var fbPath = drive + '/felhom-data/exports/';
    document.getElementById('doneFBLink').href = 'https://files.' + location.hostname.split('.').slice(-2).join('.') + '/files' + fbPath;
}
</script>

{{template "layout_end" .}}
{{end}}
