import Vue from 'vue'
import Vuex from 'vuex'
import api from '../common/config/caimeiApi.js'
import authorize from '../common/config/authorize.js'
import * as caimeiApi from '@/common/config/caimeiApi.js'
import ajaxService from '@/services/ajax.service.js'
import UserService from '@/services/user.service.js'
const getUserService = new UserService(ajaxService)
Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
        isWxAuthorize: false,
        hasLogin: false,
        userInfo: {},
        identity: 0,
        noticeNum:0,
        clubType: 0, // 1: 医美机构  2：生美机构 （医美、生美机构类型）
        wechatUserInfo: {},
        cartNumber: 0,
        isIphoneX: false,
        isActivity: false,
        isLoginType: 0,
        isLoginProductId: 0,
        isManage: false,// 是否是管理员或者小组长
        isRossShow:false // 是否显示ross广告图
    },
    mutations: {
        login(state, provider) { //用户身份 1、协销 2、资质机构 3、供应商 4.个人机构
            state.hasLogin = true
            state.userInfo = provider
            uni.setStorage({ //缓存用户登陆状态
                key: 'userInfo',
                data: provider
            })
        },
        logout(state,provider) {
            state.hasLogin = false
            state.userInfo = provider
            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
            uni.setStorage({ //缓存用户登陆状态
                key: 'userInfo',
                data: provider
            })
        },
        async updateNoticeNum(state) { // 更新通知消息数量
            const userInfo = await caimeiApi.getStorage()
            const commonId = userInfo.clubId ? userInfo.clubId : 0
            getUserService.getAuthClubCount({ commonId: commonId })
                .then(response => {
                    state.noticeNum = response.data.count
                    if (state.noticeNum >= 100) {
                        uni.setTabBarBadge({
                            index: 2,
                            text: '99+'
                        })
                    } else if (state.noticeNum > 0) {
                        uni.setTabBarBadge({
                            index: 2,
                            text: String(state.noticeNum)
                        })
                    } else {
                        uni.removeTabBarBadge({
                            index: 2,
                        })
                    }
                })
                .catch(error => {
                    uni.removeTabBarBadge({
                        index: 2,
                    })
                })
        },
        updateAllNum(state, num) {
            if (num >= 100) {
                uni.setTabBarBadge({
                    index: 1,
                    text: '99+'
                })
            } else if (num > 0) {
                uni.setTabBarBadge({
                    index: 1,
                    text: String(num)
                })
            } else {
                uni.removeTabBarBadge({
                    index: 1,
                })
            }
        },
        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
