function ToggleSidebar() {
    document.querySelector('.custom-sidebar').classList.toggle("d-none");
}

function scrollToTop(id) {
    let element = document.getElementById(id);
    element.scrollTop = 0;
}

function collapseSub(subId) {
    var collapse = bootstrap.Collapse.getInstance(document.getElementById(subId));
    if (collapse != null) {
        collapse.hide();
    }
}

function downloadFile(filename, content) {
    var link = document.createElement('a');
    link.download = filename;
    link.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content)
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

function saveAsFile(filename, base64) {
    var link = document.createElement('a');
    link.download = filename;
    link.href = "data:application/octet-stream;base64," + base64;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

function reloadPageWithDelay() {
    window.setTimeout(function () {
        window.location.reload();
    }, 15000);
}

function generateQr(text) {
    new QRCode(document.getElementById("qrCode"),
        {
            text: text,
            width: 150,
            height: 150
        });
}

function setFocus(elementId) {
    document.getElementById(elementId)?.focus();
}

function showModalDialog(dialogId) {
    let modal = new bootstrap.Modal(document.getElementById(dialogId), { backdrop: 'static', keyboard: false });
    if (modal) {
        modal.show();
    }
}

function hideModalDialog(dialogId) {
    var modal = bootstrap.Modal.getInstance(document.getElementById(dialogId));
    if (modal != null) {
        modal.hide();
    }
}

async function postAsync(url, data) {
    let response = await fetch(url, {
        method: 'POST',
        body: data,
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }
    });
    return await response.text();
}

async function getAsync(url) {
    let response = await fetch(url, {
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }
    });
    return await response.text();
}

function getOS() {
    var userAgent = window.navigator.userAgent,
        platform = window.navigator.platform,
        macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
        windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
        iosPlatforms = ['iPhone', 'iPad', 'iPod'],
        os = null;

    if (macosPlatforms.indexOf(platform) !== -1) {
        os = 'Mac OS';
    } else if (iosPlatforms.indexOf(platform) !== -1) {
        os = 'iOS';
    } else if (windowsPlatforms.indexOf(platform) !== -1) {
        os = 'Windows';
    } else if (/Android/.test(userAgent)) {
        os = 'Android';
    } else if (!os && /Linux/.test(platform)) {
        os = 'Linux';
    }

    return os;
}