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

import MD5 from '@/assets/scripts/md5';
import configs from '@/configs/env';
import apis from '@/configs/http';
import chart from '@/store/modules/chart';

Vue.use(Vuex);

const store = new Vuex.Store({
	strict: !configs.isProduction,
	modules: {
		chart
	},
	state: {
		loginUser: {},
		token: '',
		isLoading: false,
	},
	mutations: {
		setLoginUser(state, loginUser) {
			state.loginUser = loginUser;
		},
		setToken(state, token) {
			state.token = token;
		},
		setLoading(state, flag) {
			state.isLoading = flag;
		},
	},
	actions: {
		login({ commit }, { userName, password }) {
			commit('setLoading', true);
			// console.log(`login actions: `, arguments);
			const encryptPwd = MD5(password);
			const data = { userName, password: encryptPwd };
			return apis.sz.post('/TUser/userLogin', data)
				.then(res => {
					// const { token, userObj } = res.data;
					//用户名缓存
					uni.setStorageSync("name", res.data.name);
					const userObj = res.data;
					uni.setStorageSync("globalUser", res.data);
					uni.setStorageSync("token", res.token);
					uni.setStorageSync("level", res.data.level);
					// uni.setCookie(res.token);
					// uni.setToken(res.token);
					commit('setLoading', false);
					commit('setLoginUser', userObj);
					// commit('setToken', token);
				}, _ => {
					commit('setLoading', false);
					return Promise.reject();
				});
		},
		loginWXCZ({ commit },parm) {
			commit('setLoading', true);
			return apis.sz.post('/TUserWeixin/weiXinZhuce',parm)
				.then(res => {
					//用户名缓存
					uni.setStorageSync("name", res.data.name);
					const userObj = res.data;
					commit('setLoading', false);
					commit('setLoginUser', userObj);
					
					return res;
				}, _ => {
					commit('setLoading', false);
					return Promise.reject();
				});
		},
		loginWX({ commit },parm) {
			commit('setLoading', true);
			return apis.sz.post('/TUserWeixin/weiXinLogin',parm)
				.then(res => {
					//用户名缓存
					uni.setStorageSync("name", res.data.name);
					uni.setStorageSync("token", res.token);
					const userObj = res.data;
					commit('setLoading', false);
					commit('setLoginUser', userObj);
					
					return res;
				}, _ => {
					commit('setLoading', false);
					return Promise.reject();
				});
		},
		updataPassword({ commit }, { userName,password,oldPassword}) {
			commit('setLoading', true);
			const encryptPwd = MD5(password);
			const oldPwd = MD5(oldPassword);
			var oldUserName = uni.getStorageSync("globalUser").userName;
			const data = { userName,oldUserName,password:encryptPwd,oldPassword:oldPwd };
			return apis.sz.post('/TUser/updataPassword', data)
			.then(res => {
					//用户名缓存
					uni.setStorageSync("name", res.data.name);
					const userObj = res.data;
					uni.setStorageSync("globalUser", res.data);
					uni.setStorageSync("token", res.token);
					uni.setStorageSync("level", res.data.level);
					commit('setLoading', false);
					commit('setLoginUser', userObj);
					return res;
				}, _ => {
					commit('setLoading', false);
					// return Promise.reject();
					return null;
				});
		},
		
		add({ commit }, { userName,password,name,level}) {
			commit('setLoading', true);
			const encryptPwd = MD5(password);
			const data = { userName,name,level,password:encryptPwd };
			return apis.sz.post('/TUser/add', data)
				.then(res => {
					return res;
				}, _ => {
					commit('setLoading', false);
					// return Promise.reject();
					return null;
				});
		},
		list({ commit },{ userName,password,name,level}) {
			commit('setLoading', true);
			const encryptPwd = MD5(password);
			const data = { userName,name,level,password:encryptPwd };
			return apis.sz.post('/TUser/list',data)
				.then(res => {
					return res;
				}, _ => {
					commit('setLoading', false);
					return null;
				});
		},
		addCode({ commit },{ name,code,remark,flagCode}) {
			commit('setLoading', true);
			const coding = {name,code,remark,flagCode};
			return apis.sz.post('/TCoding/add',coding)
			.then(res => {
					return res;
				}, _ => {
					commit('setLoading', false);
					return null;
				});
		}
	}
});

export default store;