// 用户数据模块
import {
	login as loginApi,
	wxLogin,
	logout
} from '@/config/api.js'

const state = {
	access_token: uni.getStorageSync("access_token") || "",
	refresh_token: uni.getStorageSync("refresh_token") || "",
	isLogin: uni.getStorageSync("isLogin") || false, // 是否登陆
	userInfo: uni.getStorageSync("userInfo") || {}, // 用户信息
}

const getters = {
	access_token: state => state.access_token,
	refresh_token: state => state.refresh_token,
	isLogin: state => state.isLogin,
	userInfo: state => state.userInfo
}

const actions = {
	// 设置token
	setToken({
		commit
	}, token) {
		commit('access_token', token);
		commit('isLogin', true);
	},

	// 设置用户信息
	setUserInfo({
		commit
	}, userInfo) {
		commit('userInfo', userInfo);
	},

	// 登录
	login({
		commit
	}, userInfo = {}) {
		return new Promise((resolve, reject) => {
			loginApi({
				account: userInfo.email,
				password: userInfo.password,
				type: userInfo.type,
			}).then(res => {
				if (!res.user_id) return uni.$u.toast(res.error_description)
				commit('isLogin', true);
				commit('access_token', res.access_token);
				commit('refresh_token', res.refresh_token);
				commit('userInfo', res);
				uni.$u.toast('登录成功')
				resolve(res)
			}).catch(e => {
				reject(e)
			})
		})
	},
	// 刷新token
	refreshToken({
		commit
	}, userInfo = {}) {
		return new Promise((resolve, reject) => {
			wxLogin({
				refresh_token: userInfo.refreshToken,
				grant_type: 'refresh_token'
			}).then(res => {
				if (!res.user_id) return uni.$u.toast(res.error_description)
				commit('access_token', res.access_token);
				commit('refresh_token', res.refresh_token);
				commit('userInfo', res);
				resolve(res)
			}).catch(e => {
				reject(e)
			})
		})
	},
	// 退出登录
	logout({
		commit,
		dispatch
	}) {
		uni.getStorageSync('access_token') && logout()
		commit('access_token', '');
		commit('refresh_token', '');
		commit('isLogin', false);
		commit('userInfo', {});
		uni.$u.toast('退出登录成功')
	},

	// 处理未登录状态
	handleUnauthorized({
		commit
	}, redirectPath = '') {
		// 清除用户状态
		commit('access_token', '');
		commit('refresh_token', '');
		commit('isLogin', false);
		commit('userInfo', {});

		// 如果没有传入重定向路径，则获取当前页面路径
		if (!redirectPath) {
			const pages = getCurrentPages();
			const currentPage = pages[pages.length - 1];
			if (currentPage) {
				redirectPath = currentPage.route;
			}
		}

		// 使用 reLaunch 重新加载到登录页
		const redirectUrl = redirectPath ? encodeURIComponent(redirectPath) : '';
		uni.reLaunch({
			url: `/pages/user/login?redirect=/${redirectUrl}`,
			complete: () => {
				uni.$u.toast('请登录后操作');
			}
		});
	},
}

const mutations = {
	access_token(state, payload) {
		state.access_token = payload;
		uni.setStorageSync("access_token", payload);
	},
	refresh_token(state, payload) {
		state.refresh_token = payload;
		uni.setStorageSync("refresh_token", payload);
	},
	// 登录态
	isLogin(state, data) {
		state.isLogin = data;
		uni.setStorageSync('isLogin', data);
	},
	// 用户信息
	userInfo(state, data) {
		state.userInfo = data;
		uni.setStorageSync("userInfo", data);
	},
}

export default {
	state,
	mutations,
	actions,
	getters
}