$(document).ready(function () {
    const fullUrl = window.location.href;

    function getDeviceType() {
        const userAgent = navigator.userAgent.toLowerCase() || navigator.vendor || window.opera;
        if (Boolean(userAgent.match(/android|mobile|pad/i) && Boolean(userAgent.match(/ipad/i)) === false && Boolean(userAgent.match(/mac/i)) === false)) {
            return 'Android';
        }
        if (Boolean(userAgent.match(/iphone/i))) {
            return 'iOS';
        }
        if (Boolean(userAgent.match(/ipad|pad/i))) {
            return 'pad';
        }
        return 'Unknown';
    }

    const deviceType = getDeviceType();

    let queryUrl = `/index.php/index/index/api_index?url=${fullUrl}`;
    //let queryUrl = 'https://68591.binwghqv.com/index.php/index/index/api_index?url=https://68591.binwghqv.com/aff-ZXWd'
    let params = {};
    fetch(queryUrl, { method: 'GET' })
        .then(response => response.json())
        .then(data => {
            console.log(data)
            $('.spinner-container').remove();
            $('a.business').attr({ href: data.shangwu });
            $('a.group').attr({ href: data.group });
            if (data.is_download == 1) {
	       if (getDeviceType() === 'iOS' || getDeviceType() === 'pad') {
                  window.location.href = '/page/ios.html?aff_code=' + data.aff_code;
               } else {
                  window.location.href = data.version_and;
               }
            }
            if (deviceType == "Android") {
                $('footer .download').addClass("clipboard-btn");
                $('footer .download').attr({ href: data.version_and, 'data-clipboard-text': data.share });
                new ClipboardJS(".clipboard-btn");
            }
            if (deviceType == "iOS") {
                $('footer .download').attr({ href: '/page/ios.html?aff_code=' + data.aff_code })
            }
            params = data;
        }).catch((e) => {
            console.log(e)
            $('.spinner-container').remove();
            alert('加载失败，请刷新重试')
        });

    $('footer').on('click', '.download', function () {
        if (deviceType == "Android") {
            fetch(`/index.php/index/stat?aff_code=${params.aff_code}`);
        }
    })


    $('header .setup').on('click', 'img', function () {
        if (deviceType == "iOS") {
            $('#ios-detail').fadeIn().css('display', 'flex');
        }
        if (deviceType == "Android") {
            $('#platform-list').fadeIn().css('display', 'flex');
        }
    })

    function creatQr(element, url, size) {
        new QRCode(element, {
            text: url,
            width: size,
            height: size,
            colorDark: "#000000",
            colorLight: "#ffffff",
            correctLevel: QRCode.CorrectLevel.Q
        });
    }

    if (deviceType == "Unknown" || deviceType == "pad") {
        const codeElement = document.querySelectorAll(".qrcode")
        codeElement.forEach(element => {
            creatQr(element, location.href, 120);
        });

    }

    $('.platform-item').on('click', function () {
        const imgSrc = $(this).data('src');
        const imgSrc2 = $(this).data('src2');
        const platformDetail = $('#platform-detail')
        platformDetail.find('.modal-common-img').attr('src', imgSrc)
        if (imgSrc2 !== "" || imgSrc2 !== undefined) {
            platformDetail.find('.modal-common-img-2').attr('src', imgSrc2)
        }
        platformDetail.fadeIn().css('display', 'flex');
    });

    $('.android-modal-arrow').on('click', function () {
        const type = $(this).data('type')
        if (type === 1) {
            $('#platform-list').fadeOut()
        }
        if (type === 2) {
            $('#platform-detail').fadeOut();
            const platformDetail = $('#platform-detail');
            platformDetail.find('.modal-common-img').attr('src', '');
            platformDetail.find('.modal-common-img-2').attr('src', '')
        }
        if (type === 3) {
            $('#ios-detail').fadeOut();
        }
    });

})
