import Vue from 'vue'
import Vuex from 'vuex'
import request from '@/util/index'
import util from '@/common/utils'
import CreatePersistedState from "vuex-persistedstate"
Vue.use(Vuex)

const state = {
	visitChannels: '',
	userData: {}, // 用户基础信息
	toDoMsg: {},// 用户首页待办事项
	isConnect: true ,// 网络是否链接
	isLoading: false, // 是否在请求数据
	isChangeMsgCount: false ,// 是否更新待办事项
	shouldReLoadingList: false // 是否需要更新当前列表
};

const getters = {

	getUser(state) {
		return state.userData
	},
	getMenu(state) {
		return state.userData.AuthorityInfo
	},
	getConnect(state){
		return state.isConnect
	},
	getLoading (state) {
		return state.isLoading
	},
	isChangeMsgCount (state) {
		return state.isChangeMsgCount
	},
	shouldReLoadingList (state) {
		return state.shouldReLoadingList
	},

};

const mutations = {

	setUser (state, data) {
		state.userData = data
	},
	setVisitChannels (state, data) {
		state.visitChannels = data
	},

	shouldReLoadingList (state, data){
		state.shouldReLoadingList = data
	},
	isConnect(state,value) {

		state.isConnect  = value
	},
	setUserTodoList(state,value){
		state.toDoMsg  = value
	},
};
const actions = {
	 getLoginCode({ dispatch, state} ){
		uni.login({ // 登录
			 success (res) {
				dispatch('memberLogin',res.code) // 获取用户数据
			}
		})
	},
	getTodoList({ dispatch, state,commit}){ // 获取用户待办事项
		let _user = state.userData
		request.$get("ToDoList", {
			userId: _user.UserId,
			userName: _user.UserName || ''
		}).then(res => {
			commit('setUserTodoList', res.Data)
		})
	},
	memberLogin({ dispatch, state,commit},_code) {
		let _user = state.userData
		request.$get('WeChat_Login',{
		// request.$webGet('WebAPI_Push/WeChat/Login',{
			WeChat_Code: _code || '',

		}).then(data=>{
			if (data.Result_Code === 100) {
				  let user = data.Result_Data
				  if (user.Membership_Id) {
					dispatch('updateUser',data.Result_Data)
					// _this.addUserBehavior(1002) // 记录用户行为
				  }else{
					  commit('setUser', user)
					  uni.showModal({
					  	title:'温馨提示',
						content: '请您授权登录后再操作。',
						success(res) {
							if(res.confirm){

								util.toNextRoute('redirectTo', '/pages/register/register')

							}
						}
					  })
				  }
			} else if (data.Result_Code === 200) { // session过期
			   dispatch('getLoginCode')
			} else {
				console.log(data)
			}


		})
	},
	async updateUser ({dispatch, commit, state},user){

		let _user = user || state.userData
		let _id = _user.Membership_Id
		if(!_id) return
		// console.log(_user)
		await request.$get('WeChat_GetBusinessMemberInfo',{Membership_Id:_id}).then(res=>{
		// request.$webGet('WebAPI_Push/Member/GetMemberInfo',{Membership_Id:_id,memberShipId:_id}).then(res=>{
			if(res.Result_Code==100) {
				let data = res.Result_Data
				data.WeChat_MiniProToken = _user.WeChat_MiniProToken
				data.WeChat_UserId = _user.WeChat_UserId
				data.WeChat_UserName = _user.WeChat_UserName
				commit('setUser', data)
				dispatch('getTodoList')
			}else{
				uni.redirectTo({
					url:`/pages/commercialBI/noData?type=noAuthor`
				})
			}
		})
	}
}
export default new Vuex.Store({

	state,
	mutations,
	getters,
	actions,
	plugins: [
		CreatePersistedState({
			storage: {
				getItem: key => uni.getStorageSync(key),
				setItem: (key,value) => uni.setStorageSync(key,value),
				removeItem: key => uni.removeStorageSync(key)
			}
		})
	]
});
