import { login, logout, getInfo, getPublicKey, loginFirst } from '@/api/login';
import { getToken, setToken, removeToken } from '@/utils/auth';
import defAva from '@/assets/images/profile.jpg';
import { defineStore } from 'pinia';
import { encrypt } from '@/utils/jsencrypt';
import Cookies from 'js-cookie';

const useUserStore = defineStore('user', {
    state: (): {
        token?: string;
        name: string;
        avatar: string;
        roles: any[];
        permissions: string[];
        userInfo: any;
    } => ({
        token: getToken(),
        name: '',
        avatar: '',
        roles: [],
        permissions: [],
        userInfo: {},
    }),
    actions: {
        getPublicKey() {
            return new Promise((resolve, reject) => {
                getPublicKey()
                    .then(res => {
                        resolve(res);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        // 登录
        login(userInfo: { username: string; password: string; code: string; uuid: string }) {
            return new Promise((resolve, reject) => {
                login(userInfo.username, userInfo.password, userInfo.code, userInfo.uuid)
                    .then((res: any) => {
                        setToken(res.token);
                        this.token = res.token;
                        resolve(res);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        // 第一次登陆
        phoneLogin(userInfo: { username: string; password: string; code: string; uuid: string }) {
            return new Promise((resolve, reject) => {
                loginFirst(userInfo.username, userInfo.password, userInfo.code, userInfo.uuid)
                    .then((res: any) => {
                        setToken(res.token);
                        this.token = res.token;
                        resolve(res);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        // 获取用户信息
        getInfo() {
            return new Promise((resolve, reject) => {
                getInfo()
                    .then((res: any) => {
                        console.log(res);
                        Cookies.set('roles', res.roles[0], { expires: 30 });
                        Cookies.set('superAdministrator', res.user.userName, { expires: 30 });
                        const user = res.user;
                        const avatar =
                            user.avatar === '' || user.avatar == null
                                ? defAva
                                : import.meta.env.VITE_APP_BASE_API + user.avatar;

                        if (res.roles && res.roles.length > 0) {
                            // 验证返回的roles是否是一个非空数组
                            this.roles = res.roles;
                            this.permissions = res.permissions;
                        } else {
                            this.roles = ['ROLE_DEFAULT'];
                        }
                        this.userInfo = user;
                        this.name = user.userName;
                        this.avatar = avatar;
                        resolve(res);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        // 退出系统
        logOut() {
            return new Promise((resolve, reject) => {
                logout()
                    .then(() => {
                        this.token = '';
                        this.roles = [];
                        this.permissions = [];
                        removeToken();
                        resolve(1);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
    },
});

export default useUserStore;
