import { defineStore } from 'pinia';
import { post } from '@/api/http';

export const useUserStore = defineStore('user', {
    state: () => ({
        token: localStorage.getItem('member_token') || '',
        // 用户基本信息
        user: null,
        cellphone: localStorage.getItem('cellphone') || '',
        nickname: localStorage.getItem('nickname') || '',
        userimg: localStorage.getItem('userimg') || '',
        // 扩展字段
        isshop: localStorage.getItem('isshop') || '',
        iscenter: localStorage.getItem('iscenter') || '',
        user_id: localStorage.getItem('user_id') || '',
        huiyuankaid: localStorage.getItem('huiyuankaid') || '',
    }),
    getters: {
        isLogin: (state) => !!state.token,
        getToken: (state) => state.token,
        getUser: (state) => state.user,
        getUserInfo: (state) => ({
            cellphone: state.cellphone,
            nickname: state.nickname,
            userimg: state.userimg,
            isshop: state.isshop,
            iscenter: state.iscenter,
            user_id: state.user_id,
            huiyuankaid: state.huiyuankaid,
        }),
    },
    actions: {
        setUser(user) {
            this.user = user;
            if (user) {
                // 同步到 localStorage 保持兼容
                this.cellphone = user.cellphone || '';
                this.nickname = user.nickname || '';
                this.userimg = user.userimg || '';
                this.isshop = user.isshop || '';
                this.iscenter = user.col2 || '';
                this.user_id = user.id || '';
                this.huiyuankaid = user.huiyuankaid || '';

                localStorage.setItem('cellphone', this.cellphone);
                localStorage.setItem('nickname', this.nickname);
                localStorage.setItem('userimg', this.userimg);
                localStorage.setItem('isshop', this.isshop);
                localStorage.setItem('iscenter', this.iscenter);
                localStorage.setItem('user_id', this.user_id);
                localStorage.setItem('huiyuankaid', this.huiyuankaid);
            }
        },
        setToken(token) {
            this.token = token;
            if (token) {
                localStorage.setItem('member_token', token);
            } else {
                localStorage.removeItem('member_token');
            }
        },
        /**
         * 登录后获取用户信息
         */
        async fetchUserInfo() {
            try {
                const res = await post('/v1/client/UserClient/info');
                if (res.status === 200 && res.data) {
                    this.setUser(res.data);
                }
            } catch (err) {
                console.error('fetchUserInfo failed:', err);
            }
        },
        clearUser() {
            this.user = null;
            this.token = '';
            this.cellphone = '';
            this.nickname = '';
            this.userimg = '';
            this.isshop = '';
            this.iscenter = '';
            this.user_id = '';
            this.huiyuankaid = '';

            // 清除 localStorage
            const keys = [
                'member_token', 'cellphone', 'nickname', 'userimg',
                'isshop', 'iscenter', 'user_id', 'huiyuankaid', 'member_username'
            ];
            keys.forEach(key => localStorage.removeItem(key));
        }
    }
});