import {defineStore} from 'pinia'
import Cookies from 'js-cookie'


// 第一个参数是应用程序中 store 的唯一 id
export const useAppStore = defineStore('app', {
    // 其它配置项
    state: () => {
        let userData = {
            isLoading: false,
            isShow: true,
            isShowFooter: false,
            user: {
                AID: '',
                name: '',
                classes: '',
                teamGroup: '',
                role: '',
                userName: '',
                password: '',
                opName: '',
                opDescription: '',
                roleType: '',
            },
            routerData: {
                userRouterList: [],
                nowRouterPath: '',
                nowRouterTitle: '',
                selected: false,
            },
        };

        // 从Cookie中读取用户信息
        const userInfoStr = Cookies.get('userInfo')
        const loginKey = Cookies.get('loginKey')

        if (userInfoStr && loginKey) {
            try {
                const userInfo = JSON.parse(userInfoStr)
                userData.user = {
                    ...userData.user,
                    ...userInfo,
                    opDescription: userInfo.opDescription || Cookies.get('lastOpDescription') || userInfo.opName
                }
            } catch (e) {
                console.error('Failed to parse user info from cookie', e)
            }
        }

        return userData;
    },
})