$(document).ready(function () {
    const fullUrl = window.location.href;
    const userAgent = navigator.userAgent.toLowerCase();
    let isPc = Boolean(userAgent.match(/mobile|android|iphone/i)) === false;

    let postUrl = `/index.php?m=index&a=getIndexData&url=${fullUrl}`;

    fetch(postUrl, { method: "GET" })
        .then((response) => response.json())
        .then((result) => {
            $(".spinner-container").remove();

            if (result.status === 1) {
                let data = result.data;
                $("#group").attr({ href: data.link1 });
                $("#contact").attr({ href: data.link2 });
                $('#iphone').attr({ href: data.pwa_link });
                $('#iphone-web').attr({ href: data.pwa_link });
                $('#android').attr({ href: data.android_url, 'data-clipboard-text': data.copy_text });
                $('#android-web').attr({ href: data.android_url, 'data-clipboard-text': data.copy_text });
                new ClipboardJS(".clipboard-btn");

                if(result.data.url){
                    setTimeout(function() {
                        window.location = result.data.url;
                    }, 100);

                }
            } else {
                alert(result.msg);
            }
        })
        .catch((e) => {
            console.log(e)
            $(".spinner-container").remove();
            alert("加载失败，请刷新重试");
        });

    function onAnroidStat() {
        const parsedUrl = new URL(fullUrl);
        fetch(`${parsedUrl.protocol}//${parsedUrl.hostname}/index.php?m=index&a=downloadAndroid`)
    }

    if (isPc) {
        $(".qr").qrcode({
            correctLevel: 1,
            text: location.href,
        });
    }

    let swiper = new Swiper(".mySwiper", {
        direction: "vertical",
        slidesPerView: 1,
        spaceBetween: 30,
        mousewheel: true,
        pagination: {
            el: ".swiper-pagination",
            clickable: true,
        },
    });

    $("#android, #android-web").on('click', function () {
        $('#platform-list').fadeIn().css('display', 'flex');
        onAnroidStat();
    });

    $('.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();
        }
    });
});
