import { ref } from 'vue'
import { postData, setUserToken } from '../api/axios.js'
export function useUserInfo() {
    const userInfo = ref({
        username: '',
        displayname: '',
        identity: '',
    });
    const register = async (username, password) => {
        //登录
        let registerResult = {};
        try {
            const response = await postData('/register/', {
                username: username,
                password: password,
            })
            console.log(response)
            if (response.status === 'success') {
                //保存登录信息
                Object.keys(response.data).forEach(
                    key => {
                        if (key in userInfo.value) {
                            userInfo.value[key] = response.data[key];
                        }
                    }
                )
                console.log(userInfo.value)
                // 将userInfo存入sessionStorage
                sessionStorage.setItem('userInfo', userInfo.value);
                registerResult.status = 'success';
                registerResult.message = '注册成功';
                registerResult.data = userInfo;
                // 保存用户token
                setUserToken(response.data.token);
                // 保存用户信息
                sessionStorage.setItem('user-info', JSON.stringify(userInfo.value))
            }
        } catch (error) {
            console.log(error)
            let message = "注册失败";
            if (error.response) {
                // 处理后端返回的错误信息
                
                const backendError = error.response.data?.errors
                if (backendError) {
                    message = Object.values(backendError).join('; ')
                } else {
                    message = error.response.data?.message || message
                }
            }
            registerResult.status = 'failed';
            registerResult.message = message;
            registerResult.data = null;
        } finally {
            return registerResult;
        }
    }

    const login = async (username, password) => {
        let loginResult = {};
        try {
            const response = await postData('/login/', {
                username: username,
                password: password,
            })
            console.log(response)
            if (response.status === 'success') {
                //保存登录信息
                Object.keys(response.data).forEach(
                    key => {
                        if (key in userInfo.value) {
                            userInfo.value[key] = response.data[key];
                        }
                    }
                )
                console.log(userInfo.value)
                // 将userInfo存入sessionStorage
                sessionStorage.setItem('userInfo', userInfo.value);
                loginResult.status = 'success';
                loginResult.message = '登录成功';
                loginResult.data = userInfo;

                // 保存用户token
                setUserToken(response.data.token);
                // 保存用户信息
                sessionStorage.setItem('user-info', JSON.stringify(userInfo.value))
            }
        } catch (error) {
            console.log(error)
            let message = "登录失败";
            if (error.response) {
                // 处理后端返回的错误信息
                
                const backendError = error.response.data?.errors
                if (backendError) {
                    message = Object.values(backendError).join('; ')
                } else {
                    message = error.response.data?.message || message
                }
            }
            loginResult.status = 'failed';
            loginResult.message = message;
            loginResult.data = null;
        } finally {
            return loginResult;
        }
    }

    return {
        userInfo,
        register,
        login,
    }
}