import toast from "@/utils/toast";
import { ref } from "vue";
import useGlobalFunc from "@/composables/useGlobalFunc";
import { getStorage, setStorage } from "@/utils/storage";
import { store } from "@/store";
import { onShow } from "@dcloudio/uni-app";
import login from "@/api/https/login"
const { setUserInfo, appWxLogin } = useGlobalFunc()
export default {
    setup() {
        // 跳转手机号登录
        function phoneLogin() {
            uni.navigateTo({
                url: '/subPages/login/index',
            })
        }

        const log = ref<string>('https://image.jxc4.com/image/9b9436561e7ff7d8d764787b1aa5182e.jpg')
        const navTitle = ref<string>('京西到家商家')
        onShow(() => {
            isDownWx()
            if (getStorage('terrace') == 'jxcs') {
                log.value = 'https://image.jxc4.com/image/9b9436561e7ff7d8d764787b1aa5182e.jpg'
                navTitle.value = '京西到家商家'
            } else if(getStorage('terrace') == 'jxgy'){
                log.value = 'https://image.jxc4.com/image/9e26e6d4e8646d340c21dfe595ac4d08.jpg'
                navTitle.value = '京西果园商家'
            }else{
                log.value = 'https://image.jxc4.com/image/5cd356df441a32295798f78a39491464.png'
                navTitle.value = '京西超市商家'
            }
            // 获取cid
            plus.push.getClientInfoAsync((info) => {
                let cid = info["clientid"];
                setStorage('cid', cid)
            }, () => { });
        })

        // ******************** 是否下载微信 *****************************
        const isWx = ref<boolean>(false)
        function isDownWx() {
            if (plus.runtime.isApplicationExist({ pname: 'com.tencent.mm', action: 'weixin://' })) {
                isWx.value = true
            } else {
                isWx.value = false
            }
        }

        /**
         * 用户协议
         */
        function user() {
            let url = 'https://www.jxc4.com/managerApp/jxdjService.html'
            uni.navigateTo({ url: `/subPages/agreement/jxcs?url=${url}` })
        }


        /**
         * 隐私协议
         */
        function privacy() {
            let url = 'https://www.jxc4.com/managerApp/jxdjPrivacy.html'
            uni.navigateTo({ url: `/subPages/agreement/jxcs?url=${url}` })
        }

        // 微信登录弹窗实例
        const wxPopup = ref<any>(null)

        // ******************** wx登录类 *****************************
        //#region 
        // 微信登录
        function appWxLoginFn() {
            appWxLogin({
                bangding: (authToken: string) => {
                    // 显示自定义加载图(vuex)
                    store.commit('storeInfo/jxLoadingFn', true)
                    uni.navigateTo({ url: `/subPages/login/addAuth/addAuth?authToken=${authToken}` })
                },
                success: (data: AnyObject) => {
                    setUserInfo(data)
                    saveUserType()
                }
            })
            wxPopup.value.close()
        }

        /*************************************************
         * 存储用户信息
        */
        async function saveUserType() {
            let res = await login.get_self_info()
            setStorage('userType', res.code == 0 ? res.data.type : 0)
        }
        //#endregion 


        // ******************** 切换平台 *****************************
        //#region 
        let timer: any = null
        function switchTerrace() {
            uni.showActionSheet({
                title: '请选择使用平台',
                itemColor: '#5ab83e',
                itemList: ['京西到家商家', '京西果园商家','京西超市商家'],
                popover: {
                    width: 5000,
                },
                success: function (res) {
                    uni.showLoading({
                        title: '正在切换系统',
                    })
                    timer = setTimeout(() => {
                        // 京西到家商家
                        if (res.tapIndex == 0) {
                            setStorage('terrace', 'jxcs')
                            log.value = 'https://image.jxc4.com/image/9b9436561e7ff7d8d764787b1aa5182e.jpg'
                            navTitle.value = '京西到家商家'
                        }
                        // 京西果园商家
                        if (res.tapIndex == 1) {
                            setStorage('terrace', 'jxgy')
                            log.value = 'https://image.jxc4.com/image/9e26e6d4e8646d340c21dfe595ac4d08.jpg'
                            navTitle.value = '京西果园商家'
                        }
                        // 京西到家商家
                        if (res.tapIndex == 2) {
                            setStorage('terrace', 'gblm')
                            log.value = 'https://image.jxc4.com/image/5cd356df441a32295798f78a39491464.png'
                            navTitle.value = '京西超市商家'
                        }

                        uni.hideLoading()
                        toast('切换成功', 1)
                        clearTimeout(timer)
                    }, 500)
                },
                fail: () => {
                    console.log('ZSW-取消')
                },
            })
        }
        //#endregion 

        return {
            phoneLogin,
            user,
            privacy,
            wxPopup,
            appWxLoginFn,
            switchTerrace,
            log,
            navTitle,
            isWx,
            getStorage,
        }
    }
}