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: [], //配置列表
		userInfo : {}, //用户信息
		permissions : [],//权限列表
		statusList : [],//工单状态列表
		// workDetail : {},//工单详情
	},
	getters: {
	},
	mutations: {
		// 初始化配置
		initConfig(state){
			// api('getConfig', res => {
			// 	if(res.code == 200){
			// 		state.configList = res.result
			// 	}
			// })
			
			let config = ['getPrivacyPolicy', 'getUserAgreement']
			config.forEach(k => {
				api(k, res => {
					if (res.code == 200) {
						state.configList[k] = res.result
					}
				})
			})
		},
		// 账号密码登录
		accountLogin(state, form){
			api('login', form, res => {
				if(res.code == 200){
					state.userInfo = res.result
					state.permissions = res.result.permissionRoleList.map(n => n.permissionName)
					uni.setStorageSync('token', res.result.id)
					uni.navigateBack(-1)
				}
			})
		},
		// 退出登录
		logout(state){
			uni.showModal({
				title: '确认退出登录吗',
				success(r) {
					if(r.confirm){
						state.userInfo = {}
						state.role = false
						uni.removeStorageSync('token')
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}
				}
			})
		},
		// 获取个人信息
		getUserInfo(state){
			api('queryUserById', {
				userId : uni.getStorageSync('token')
			}, res => {
				if(res.code == 200){
					state.userInfo = res.result
					state.permissions = res.result.permissionRoleList.map(n => n.permissionName)
				}
			})
		},
		// 获取工单状态列表
		templateStatusInfo(state){
			api('templateStatusInfo', res => {
				if(res.code == 200){
					state.statusList = [
						{
							name : '全部'
						},
						...res.result.records,
					]
				}
			})
		},
		// 根据id获取工单详情
		// queryTemplateById(templateId){
		// 	api('queryTemplateById', {
		// 		templateId
		// 	}, res =>{
		// 		if(res.code == 200){
		// 			this.workDetail = res.result
		// 		}
		// 	})
		// },
	},
	actions: {},
})

export default store