import { defineStore } from 'pinia'
import { loginInfo,loginByCode,logout } from '@/api/wx.js'
/**
 * 授权仓库
 */
export const useAuthStore = defineStore('useAuthStore', {
  state: () => {
    return {
	/**
	 * 登录用户信息
	 */
      user:undefined,
	  /**
	   * 授权令牌
	   */
	  token:undefined,
	  
	  /**
	   * 授权注册登录窗口是否显示
	   */
	  loginShow:false,
	  /**
	   * 用户角色列表
	   */
	  roles:[],
	  /**
	   * 用户权限列表
	   */
	  permissions:[]
    }
  },
  //计算字段
  getters: {
	isLogin() {
		return this.user != undefined;
	}
  },
  //方法
  actions: {
	//小程序静默登录
	login() {
		return new Promise((reslove,reject)=>{
			let that = this;
			
			// #ifdef MP-WEIXIN
			
			uni.login({
				success(res) {
					loginByCode(res.code).then((r)=>{
						console.debug("静默登录成功",r);
						that.token = r.token;
						return that.loadInfo();
					}).then(()=>{
						reslove();
						uni.$emit("login");
					}).catch((error)=>{
						let message = "静默登录失败";
						console.warn(message,error);
						// that.$reset();
						reject(new Error(message,{cause:error}));
					});
				}
			});
			
			// #endif
			
			// #ifndef MP-WEIXIN
			
			reject(new Error("非微信小程序不支持静默登录"));
			
			// #endif
			
			
		});
	},
	regLogin(){
		let that = this;
		return new Promise((reslove,reject)=>{
			that.login().then(reslove).catch(()=>{
				that.loginShow = true;
			});
		});
		
	},
	async loadInfo(){
		if(!this.token){
			return;
		}
		try{
			let data = await loginInfo();
			this.user=data.user;
			this.permissions = data.permissions;
			this.roles = data.roles;
			uni.$emit("loadInfo");
		}catch(error){
			let message = "加载登录用户信息失败";
			console.warn(message,error);
			this.$reset();
			throw new Error(message,{cause:error});
		}
	},
	async logout() {
		try{
			let data = await logout();
			uni.showToast({
				icon:"success",
				title:"安全退出"
			});
		}catch(error){
			let message = "安全退出失败";
			console.warn(message,error);
			throw new Error(message,{cause:error});
		}finally {
			this.$reset();
			uni.$emit("logout");
		}
	},
	async setToken(token) {
		this.token = token;
		await this.loadInfo();
		uni.$emit("login");
		return Promise.resolve();
	},
	hasPermissionAnd(...permissions){
		if(!this.isLogin){
			return false;
		}
		if(this.permissions.includes("*:*:*")){
			return true;
		}
		for(let p of permissions){
			if(!this.permissions.includes(p)){
				return false;
			}
		}
		return true;
	},
	hasPermission(...permissions){
		if(!this.isLogin){
			return false;
		}
		if(this.permissions.includes("*:*:*")){
			return true;
		}
		for(let p of permissions){
			if(this.permissions.includes(p)){
				return true;
			}
		}
		return false;
	},
	hasRoleAnd(...roles){
		if(!this.isLogin){
			return false;
		}
		for(let p of roles){
			if(!this.roles.includes(p)){
				return false;
			}
		}
		return true;
	},
	hasRole(...roles){
		if(!this.isLogin){
			return false;
		}
		for(let p of roles){
			if(this.roles.includes(p)){
				return true;
			}
		}
		return false;
	}
	
	
	
	
  },
  unistorage: true // 开启后对 state 的数据读写都将持久化

})