import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { login,logout,getInfo,profile,getDeptWarehouse } from '@/api/login'
import { menu,power } from "@/api/system/user";
import { getToken,setToken,removeToken } from '@/utils/auth'
import { getPracticalUrl,setPracticalUrl } from '@/utils/practicalUrl'
import { generatePower,generateRoute } from '@/utils/permission'
let timeout = 10000
const practicalUrl = getPracticalUrl()
const baseUrl = practicalUrl ? practicalUrl : config.baseUrl

const user = {
	state: {
		token: getToken(),
		name: storage.get(constant.name),
		avatar: storage.get(constant.avatar),
		roles: storage.get(constant.roles),
		permissions: storage.get(constant.permissions),
		userRoutes: storage.get(constant.userRoutes),
		warehouse: storage.get(constant.warehouse),
		tokenKey: storage.get(constant.tokenKey)
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_NAME: (state, name) => {
			state.name = name
			storage.set(constant.name, name)
		},
		SET_WH: (state, warehouse) => {
			state.warehouse = warehouse
			storage.set(constant.warehouse, warehouse)
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
			storage.set(constant.avatar, avatar)
		},
		SET_ROLES: (state, roles) => {
			state.roles = roles
			storage.set(constant.roles, roles)
		},
		SET_PERMISSIONS: (state, permissions) => {
			state.permissions = permissions
			storage.set(constant.permissions, permissions)
		},
		SET_USER_ROUTE: (state, userRoutes) => {
			state.userRoutes = userRoutes
			storage.set(constant.userRoutes, userRoutes)
		},
		SET_token: (state, token) => {
			state.token = token
			storage.set(constant.token, token)
		},
		SET_tokenKey: (state, tokenKey) => {
			state.tokenKey = tokenKey
			storage.set(constant.tokenKey, tokenKey)
		}
	},

	actions: {
		// 登录
		Login({commit}, userInfo) {
			const username = userInfo.username.trim()
			const password = userInfo.password
			const code = userInfo.code
			const uuid = userInfo.uuid
			return new Promise((resolve, reject) => {
				login(username, password, code, uuid).then(res => {
					
					setToken(res.token)
					commit('SET_TOKEN', res.token)
					commit('SET_token', res.token)
					commit('SET_tokenKey', res.tokenKey)
					//获取权限、路由相关内容
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 获取用户信息
		GetInfo({commit,state}) {
			return new Promise((resolve, reject) => {
			 
				profile({}).then((response) => {
					const user = response.data;
					const avatar = (user == null || user.avatar == "" || user.avatar == null) ?
						require("@/static/images/profile.jpg") :  user.avatar
					//const avatar = require("@/static/images/profile.jpg");
					const username = (user == null || user.username == "" || user.username ==
						null) ? "" : user.username
					// if (res.roles && res.roles.length > 0) {
					// 	commit('SET_ROLES', res.roles)
					// 	commit('SET_PERMISSIONS', res.permissions)
					// } else {
					// 	commit('SET_ROLES', ['ROLE_DEFAULT'])
					// }
					//查询用户部门信息
					if(user.deptId){
						getDeptWarehouse({deptCode:user.deptId}).then((detpData) => {
							var dictionary =[];
							dictionary =  detpData.data.map((item) => ({
								warehouseCode: item.houseCode,
								warehouseName: item.houseName
							}));
							commit('SET_WH', dictionary)
						}).catch(error1 => {
							reject(error1)
						});
					}
					commit('SET_NAME', username)
					commit('SET_AVATAR', avatar)
					resolve(response)
				}).catch(error => {
					reject(error)
				});
				
			})
		},
		//添加路由信息
		async addRoute({commit}) {
			return new Promise((resolve, reject) => {
				menu().then((response) => {
					console.log("addRoute", response)
					const routes = response.data;
					const menuRoute = generateRoute(routes);
					console.log("addRoute", menuRoute)
					commit('SET_USER_ROUTE', menuRoute)
					resolve(response)
				}).catch(error => {
					reject(error)
				});
			})

		},
		//添加权限信息
		async addPower({commit}) {
			return new Promise((resolve, reject) => {
				power().then((response) => {
					console.log("addPower", response)
					const powers = response.data;
					const menuPower = generatePower(powers);
					console.log("addPower", menuPower)
					commit('SET_PERMISSIONS', menuPower)
					resolve(response)
				}).catch(error => {
					reject(error)
				});
			})
		},
		// 退出系统
		LogOut({commit,state}) {
			return new Promise((resolve, reject) => {
				logout(state.token).then(() => {
					commit('SET_TOKEN', '')
					commit('SET_ROLES', [])
					commit('SET_PERMISSIONS', [])
					commit('SET_USER_ROUTE', [])
					commit('SET_token', '')
					commit('SET_tokenKey', '')
					removeToken()
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},
	}
}

export default user