const url = "https://epp.only4.work/access/wechat/getUnlimitedQRCode"
const page = "pages/index/index" // "pages/scan/entrance"
const envVersion = "develop" // 正式版为 "release"，体验版为 "trial"，开发版为 "develop"
const autoColor = true
const isHyaline = false
const width = 500

// 获取 Url 参数
function getUrlParams() {
    let params = {}
    location.search.substring(1).split("&").map(param => {
        let a = param.indexOf("=")
        if (a < 0)
            params[param] = ''
        else
            params[param.substring(0, a)] = decodeURIComponent(param.substring(a + 1))
    })
    // console.log(params)
    return params
}
let urlParams = getUrlParams()
console.log("urlParams", urlParams)

// 是否在 electron 中
const inElectron = urlParams['inElectron']

// 当前的门禁端
let gateId = urlParams['gateId'] || ""

if (!gateId) {
    // 弹出选择框
}

// 点击全屏
document.getElementById("fullscreen-button").addEventListener("click", function () {
    if (document.fullscreenElement) {
        document.exitFullscreen();
    } else {
        if(inElectron) {
            // electron 中
            const message = JSON.stringify({
                action: 'fullscreen'
            });
            window.parent.postMessage(message, '*');
        } else {
            // 浏览器中
            document.body.requestFullscreen()
        }
    }
})

// 页面上的元素
const image = document.getElementById('qrcode')
const refreshTimeCountDown = document.getElementById('refreshTimeCountDown')

// 定时更新页面上的小程序码
let i = 0, refreshTime = 10 + 1
function updateQRCode() {
    if (i % refreshTime == 0) {
        let scene = encodeURIComponent(`guard&${Date.now()}`);
        image.src = `${url}?page=${page}&scene=${scene}&envVersion=${envVersion}&width=${width}&autoColor=${autoColor}&isHyaline=${isHyaline}`
        console.log(image.src)
        refreshTimeCountDown.innerHTML = `&nbsp;`
    } else {
        refreshTimeCountDown.textContent = `${refreshTime - i}秒后刷新`
    }
    i = i % refreshTime + 1
}

updateQRCode()
setInterval(updateQRCode, 1000)
