import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex); //vue的插件机制

import api from '@/api/api.js'

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: {
		configList: {}, //配置列表
		// 角色 true为老板 false为工人
		role : false,
		userInfo : {}, //用户信息
		banner : [],//轮播图
		jobTypeList : [],//工种
		natureList : [],//工作性质
		addressList : [],//开放地址 
		UserExtensionInfo : {},//用户扩展信息，包含认证信息、统计信息
	},
	getters: {
	},
	mutations: {
		// 初始化配置
		initConfig(state){
			api('getConfig', res => {
				if(res.code == 200){
					res.result.forEach(n => {
						state.configList[n.paramCode] = n.paramValueText || 
						n.paramValue ||
						n.paramValueImage 
						// state.configList[n.keyName + '_keyValue'] = n.keyValue
					})
				}
			})
			
			// let config = ['getPrivacyPolicy', 'getUserAgreement']
			// config.forEach(k => {
			// 	api(k, res => {
			// 		if (res.code == 200) {
			// 			state.configList[k] = res.result
			// 		}
			// 	})
			// })
		},
		login(state){
			uni.showLoading({
				title: '登录中...'
			})
			uni.login({
				success(res) {
					if(res.errMsg != "login:ok"){
						return
					}
					
					api('wxLogin', {
						code : res.code
					}, res => {
						
						uni.hideLoading()
						
						if(res.code != 200){
							return
						}
						
						state.userInfo = res.result.userInfo
						uni.setStorageSync('token', res.result.token)
						
						if(!state.userInfo.nickName || !state.userInfo.headImage
						|| !state.userInfo.phone){
							uni.navigateTo({
								url: '/pages_order/auth/wxUserInfo'
							})
						}else{
							uni.navigateBack(-1)
						}
					})
				}
			})
		},
		getUserInfo(state){
			api('getInfo', res => {
				if(res.code == 200){
					state.userInfo = res.result
					
					if(!state.userInfo.nickName || !state.userInfo.headImage
					|| !state.userInfo.phone){
						uni.navigateTo({
							url: '/pages_order/auth/wxUserInfo'
						})
					}
				}
			})
		},
		// 获取工种列表
		getJobTypeList(state){
			api('commonQueryJobTypeList', {
				pageNo : 1,
				pageSize : 99999,
			}, res => {
				if(res.code != 200){
					return
				}
				state.jobTypeList = res.result.records
			})
		},
		// 获取开放地址
		getAddressList(state){
			api('commonQueryAddressList', {
				pageNo : 1,
				pageSize : 99999,
			}, res => {
				if(res.code != 200){
					return
				}
				state.addressList = res.result.records
			})
		},
		// 获取轮播图
		getBanner(state){
			api('commonQueryBannerList', {
				pageNo : 1,
				pageSize : 99999,
			}, res => {
				if(res.code != 200){
					return
				}
				state.banner = res.result.records
			})
		},
		// 获取工作性质列表
		getNatureList(state){
			api('commonQueryJobNatureList', {
				pageNo : 1,
				pageSize : 99999,
			}, res => {
				if(res.code != 200){
					return
				}
				state.natureList = res.result.records
			})
		},
		logout(state){
			uni.showModal({
				title: '确认退出登录吗',
				success(r) {
					if(r.confirm){
						state.userInfo = {}
						state.role = false
						uni.removeStorageSync('token')
						uni.redirectTo({
							url: '/pages/index/index'
						})
					}
				}
			})
		},
		setRole(state, role){
			state.role = role
		},
		// 在完成实名认证的情况下执行
		isAuthCertification(state, fn){
			this.commit('getUserExtensionInfo')
			if(!state.UserExtensionInfo.personAuthenticationStatus) return
			
			if(['-1', '2'].includes(state.UserExtensionInfo.personAuthenticationStatus)){
				uni.showModal({
					title: `实名认证`,
					content: '实名认证未完成 或 实名认证审核未通过，请先完成！',
					confirmText: '去完成',
					success : e => {
						if(e.confirm){
							uni.navigateTo({
								url: '/pages_order/auth/certification'
							})
						}
					}
				})
				return
			}
			
			if(state.UserExtensionInfo.personAuthenticationStatus == '0'){
				uni.showToast({
					title: '实名认证审核中，请耐心等待！',
					icon: 'none'
				})
				return
			}
			
			
			fn && fn(state)
		},
		// 在完成企业认证的情况下执行
		isAuthCertificationEnterprise(state, fn){
			this.commit('getUserExtensionInfo')
			if(!state.UserExtensionInfo.companyAuthenticationStatus) return
			
			if(['-1', '2'].includes(state.UserExtensionInfo.companyAuthenticationStatus)){
				uni.showModal({
					title: `企业认证`,
					content: '企业认证未完成 或 企业认证审核未通过，请先完成！',
					confirmText: '去完成',
					success : e => {
						if(e.confirm){
							uni.navigateTo({
								url: '/pages_order/auth/certificationEnterprise'
							})
						}
					}
				})
				return
			}
			
			if(state.UserExtensionInfo.companyAuthenticationStatus == '0'){
				uni.showToast({
					title: '企业认证审核中，请耐心等待！',
					icon: 'none'
				})
				return
			}
			
			fn && fn(state)
		},
		//获取用户扩展信息的接口
		getUserExtensionInfo(state){
			api('getUserCenterData', res =>{
				if(res.code == 200){
					state.UserExtensionInfo = res.result
				}
			})
		},
	},
	actions: {},
})

export default store