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

/**
 *@description: 微信登录功能模块
 *@return {*}
 *@param {}-
*/
function weixinFn() {

    const {
        setUserInfo,
    } = useGlobalFunc()
    /**
     * 定义切换标题数据
     */
    interface LoginType {
        title: string;
    }
    const loginTypeList: Ref<LoginType[]> = ref([
        { title: "验证码登录" },
        { title: "密码登录" },
    ]);
    const activeItem: Ref<number> = ref(1);

    onShow(() => {
        store.commit('serveInfo/setIsAgreement', false)
        store.commit('serveInfo/setIsAgreementIf', false)
    })

    /*************************************************
     * 存储用户信息
    */
    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()
    }


    /**
     * 密码登录
     */




    // /**
    //  * 微信登录
    //  */
    // let appWxLoginFnTimer: any = null
    // let appWxLoginFnTimer2: any = null


    /**
     * 用户协议
     */
    function user() {
        uni.navigateTo({ url: '/subPages/agreement/user' })
    }


    /**
     * 隐私协议
     */
    function privacy() {
        uni.navigateTo({ url: '/subPages/agreement/privacy' })
    }

    /*************************************************
     * 是否同意协议
    */
    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
        }
    }

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

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


    return {
        loginTypeList,                  // 切换tab
        activeItem,                     // 切换高亮
        codeLogin,                      // 验证码登录
        // pwdLogin,                       // 密码登录
        user,                           // 用户协议
        privacy,                        // 隐私协议
        change,                         // 切换
        agreement,                      // 是否同意协议
        isAgreement,                    // 是否同意协议
    }
}


export default weixinFn