import Vue from 'vue'
import Vuex from 'vuex'
import api from '../common/config/caimeiApi.js'
import authorize from '../common/config/authorize.js'

Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
        isWxAuthorize: false,
        hasLogin: false,
        userInfo: {},
        identity: 0,
        wechatUserInfo: {},
        cartNumber: 0,
        isIphoneX: false,
        isActivity: false,
        isLoginType: 0,
        isLoginProductId: 0
    },
    mutations: {
        login(state, provider) { //用户身份 1、协销 2、资质机构 3、供应商 4.个人机构
            state.hasLogin = true
            state.userInfo = provider
            uni.setStorage({ //缓存用户登陆状态
                key: 'userInfo',
                data: provider
            })
        },
        logout(state) {
            state.hasLogin = false
            state.userInfo = {}
            uni.removeStorage({
                key: 'userInfo'
            })
            uni.removeStorage({
                key: 'token'
            })
        },
        wxLogin(state, provider) {
            state.isWxAuthorize = true
            state.wechatUserInfo = provider
            uni.setStorage({ //缓存用户登陆状态 
                key: 'wechatUserInfo',
                data: provider
            })
            uni.setStorageSync('_WX_State', 1)
        },
        updateStatus(state, provider) {
            let TIME = api.formatDate()
            console.log(`${TIME}`, provider)
            state.userInfo = provider
            if (state.userInfo) {
                if (provider.userIdentity) {
                    state.identity = provider.userIdentity
                } else if (provider.userID == 5261 || provider.userID == 10947 || provider.userID == 11579) {
                    state.identity = 1
                } else {
                    state.identity = 0
                }
            }
            console.log('用户类型', state.identity)
            uni.setStorage({ //缓存用户登陆状态
                key: 'userInfo',
                data: provider
            })
        },
        updateAllNum(state, num) {
            if (num >= 100) {
                uni.setTabBarBadge({
                    index: 2,
                    text: '99+'
                })
            } else if (num > 0) {
                uni.setTabBarBadge({
                    index: 2,
                    text: String(num)
                })
            } else {
                uni.removeTabBarBadge({
                    index: 2,
                })
            }
        },
        setActivity(state, variable) { // 记录活动弹窗状态
            state.isActivity = variable
        },
        setChangeVar(state, variable) {
            state.isIphoneX = variable
        },
        setIsIphone(state, variable) { // 记录设备信息是否为IphoneX
            state.isIphone = variable
        },
        setLoginType(state, variable) { // 记录登录跳转类型
            state.isLoginType = variable
            console.log(state.isLoginType)
        },
        setLoginProductId(state, variable) { // 记录跳转商品ID
            state.isLoginProductId = variable
            console.log(state.isLoginProductId)
        },
        setLoginOrderId(state, variable) { // 记录跳转订单ID
            state.isLoginOrderId = variable
            console.log(state.isLoginOrderId)
        }
    },
    actions: {
        setVariableFun: function(context, vData) {
            context.commit('setChangeVar', vData)
        },
        setIsIphoneFun: function(context, vData) {
            context.commit('setIsIphone', vData)
        },
        setActivityFn: function(context, vData) {
            context.commit('setActivity', vData)
        },
        // setLoginTypeFn:function(context,vData){
        // 	context.commit('setLoginType',vData)
        // }
    }
})

export default store
