import {
	login,
	loginOut,
	loginPhone,
	getUserProfile,
	getLoginCode,
	loginUserInfo,
	phoneLogin,
	dealerPhoneLogin
} from '@/api/user.js'
import uniGo from '@/utils/unIGO.js';
export default {
	state: () => ({
		token: uni.getStorageSync("token") ? uni.getStorageSync("token") : null,
		userInfo: uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")) : {},
		role: uni.getStorageSync("role") ? uni.getStorageSync("role") : 'consumer', //consumer消费 head团长
		loginUser: uni.getStorageSync("loginUser") ? JSON.parse(uni.getStorageSync("loginUser")) : {}
	}),
	mutations: {
		loginIn(state) {
			let userInfo = uni.getStorageSync("userInfo");
			let token = uni.getStorageSync("token");
			let role = uni.getStorageSync("role");
			if (userInfo && token && role) {
				state.token = token;
				state.userInfo = JSON.parse(userInfo);
				state.role = role;
			}
		},
		login(state, obj) {
			uni.setStorageSync("token", obj.token);
			uni.setStorageSync("userInfo", JSON.stringify(obj.userInfo));
			uni.setStorageSync("role", obj.role);
			state.token = obj.token;
			state.userInfo = obj.userInfo;
			state.role = obj.role;
		},
		loginOut(state) {
			uni.removeStorageSync("token");
			uni.removeStorageSync("userInfo");
			uni.removeStorageSync("role");
			uni.removeStorageSync("loginUser");
			state.token = null;
			state.userInfo = {};
			state.role = 'consumer';
			state.loginUser = {};
		},
		upDateRoleMount(state, val) {
			uni.setStorageSync("role", val);
			state.role = val;
		},
		upDateRoleToken(state, obj) {
			uni.setStorageSync("token", obj.token);
			state.token = obj.token;
		},
		upDateLoginUserMount(state, val) {
			uni.setStorageSync("loginUser", JSON.stringify(val));
			state.loginUser = val;
		},
		upDateUserInfoMount(state, val) {
			loginUserInfo(state.token).then(kh => {
				const {
					data
				} = kh
				uni.setStorageSync("userInfo", JSON.stringify(data));
				state.userInfo = data;
				if (val === 'login') {
					if (data.isUpdatePassword == 0) {
						uniGo.navigateTo({
							url: `/pages/login/changePwd`
						})
					} else {
						if (data.shopList && data.shopList.length > 1) {
							uniGo.navigateTo({
								url: `/personal/selectStore/index`
							});
						} else {
							uniGo.switchTab({
								url: `/pages/index/index`
							});
						}
					}
				}
			})
		}
	},
	actions: {
		
		async phoneLogin({
			commit
		}, userInfo) {
			const {
				data
			} = await phoneLogin(userInfo)
			console.log(data, '======state phoneLogin')
			uni.setStorageSync("token", data.token);
			commit("login", {
				token: data.token,
				userInfo: data,
				role: 'consumer'
			});
		},
		upDateRole({
			commit
		}, data) {
			commit("upDateRoleMount", data);
			commit("upDateUserInfoMount");
		},
		upDateToken({
			commit
		}, data) {
			commit("upDateRoleToken", data);
		},
		postLogin({
			commit
		}, data) {
			if (data) {
				console.log('登录111111111111', data)
				let paramsV = {
					code: data.code,
					encryptedData: data.encryptedData,
					iv: data.iv,
					userCode: data.userCode,
					userInfo: {
						avatarUrl: data.loginUser.avatarUrl,
						city: data.loginUser.city,
						country: data.loginUser.country,
						gender: data.loginUser.gender,
						language: data.loginUser.language,
						nickName: data.loginUser.nickName,
						province: data.loginUser.province,
					}
				}
				console.log(paramsV, '11111111111111111参数')
				commit('upDateLoginUserMount', data.loginUser)
				loginPhone(paramsV).then(back => {
					const {
						data: dataFromV
					} = back
					// headOrNot 1团长 2消费
					const {
						headOrNot
					} = dataFromV
					let roleV;
					headOrNot == 1 ? roleV = 'head' : roleV = 'consumer'
					let objFormV = {
						token: dataFromV.token,
						userInfo: dataFromV,
						role: roleV
					}
					commit("login", objFormV);
				})
			} else {
				console.log('登录2222222222222222222')
				// 展示加载框
				Promise.all([getUserProfile(), getLoginCode()]).then((res) => {
					console.log(res, 'bbbbbbbbbbb')
					let obj = res[0],
						rev = res[1];

					let params = {
						code: rev.code,
						encryptedData: obj.encryptedData,
						iv: obj.iv,
						userInfo: {
							avatarUrl: obj.userInfo.avatarUrl,
							city: obj.userInfo.city,
							country: obj.userInfo.country,
							gender: obj.userInfo.gender,
							language: obj.userInfo.language,
							nickName: obj.userInfo.nickName,
							province: obj.userInfo.province,
						}
					}
					commit('upDateLoginUserMount', obj.userInfo)
					dealerPhoneLogin({
						code: rev.code,
						encryptedData: obj.encryptedData,
						iv: obj.iv,
					}).then(back => {
						console.log(bakc, 'postLogin 2222222')
					})
				})
			}
		},
		wxPhoneLogin({
			commit
		}, data) {
			console.log(data, 'wxPhoneLogin vuex')
			// return
			dealerPhoneLogin({
				code: data.loginCodeData.code,
				encryptedData: data.wxPhoneDetail.encryptedData,
				iv: data.wxPhoneDetail.iv,
			}).then(back => {
				console.log(back, 'wxPhoneLogin dealerPhoneLogin vuex')
				uni.setStorageSync("token", back.data.token);
				commit("upDateRoleToken", {
					token: back.data.token
				});
				commit("upDateUserInfoMount", data.form)
			})
		}
	}
}
