import login from "@/api/https/login"
import useGlobalFunc from "@/composables/useGlobalFunc";
import { store } from "@/store";
import { getStorage, setStorage } from "@/utils/storage";
import { onShow } from "@dcloudio/uni-app";
import { onBeforeUnmount, ref, Ref } from "vue";
import toast from "@/utils/toast";


/**
 *@description: 微信登录功能模块
 *@return {*}
 *@param {}-
*/
// https://image.jxc4.com/image/5cd356df441a32295798f78a39491464.png
function weixinFn() {
    const log = ref<string>('https://image.jxc4.com/image/9b9436561e7ff7d8d764787b1aa5182e.jpg')
    const navTitle = ref<string>('京西到家商家')
    onShow(() => {
        console.log('系统类型', getStorage('terrace'));
        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 { setUserInfo } = useGlobalFunc()
    /**
     * 定义切换标题数据
     */
    interface LoginType {
        title: string;
    }
    const loginTypeList: Ref<LoginType[]> = ref([
        { title: "验证码登录" },
        { title: "密码登录" },
    ])
    const activeItem: Ref<number> = ref(0);


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


    /**
     * 验证码登录
     */
    function codeLogin(data: AnyObject) {
        setUserInfo(data)
        saveUserType()
    }


    /**
     * 密码登录
     */
    function pwdLogin(data: AnyObject) {
        setUserInfo(data)
        saveUserType()
    }


    /**
     * 用户协议
     */
    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}` })
    }


    /*************************************************
     * 切换登录信息
    */
    function change(res: AnyObject) {
        activeItem.value = res.detail.current
    }

    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('取消切换系统')
            },
        })
    }

    /*************************************************
     * 是否同意协议
    */
    const isAgreement = ref<boolean>(false)
    function agreement(e: AnyObject) {
        if (e.detail.value.length) {
            // 同意
            isAgreement.value = true
            store.commit('serveInfo/setIsAgreement', true)
            store.commit('serveInfo/setIsAgreementIf', false)
        } else {
            // 不同意
            store.commit('serveInfo/setIsAgreement', false)
            isAgreement.value = false
        }
    }


    /*************************************************
     * 收尾工作
    */
    onBeforeUnmount(() => {
        store.commit('serveInfo/setIsAgreement', false)
    })


    return {
        loginTypeList,                  // 切换tab
        activeItem,                     // 切换高亮
        codeLogin,                      // 验证码登录
        pwdLogin,                       // 密码登录
        user,                           // 用户协议
        privacy,                        // 隐私协议
        change,                         // 切换
        log,                            // log
        switchTerrace,                  // 切换平台
        navTitle,                       // 导航栏标题
        agreement,                      // 是否同意协议
        isAgreement,                    // 是否同意协议
    }
}


export default weixinFn