import { ref, reactive } from 'vue'
import { v4 } from 'uuid'
import { ClientType } from '@/constants/client'
import { useLoginStore } from '@/stores'
import { initBaseData } from '@/business/common'
import eventBus from '@/services/bus'

export function useSign() {
    const { userLogin } = useLoginStore();
    const loading = ref(false);
    const user = reactive<Proto.LoginReq>({
        LoginID: '110000000001',
        LoginPWD: '123456',
        GUID: v4(),
        LoginType: 0,
        ClientType: ClientType.Web,
        Version: '2.0.0.0',
        DeviceID: ''
    })

    // 用户注册
    const signUp = () => {
        console.log('用户注册')
    }

    // 用户登录
    const signIn = () => {
        loading.value = true
        return new Promise((resolve, reject) => {
            userLogin(user).then((res) => {
                initBaseData(() => {
                    resolve(res)
                }).catch((err) => {
                    loading.value = false
                    reject(err)
                })
            }).catch((err) => {
                loading.value = false
                reject(err)
            })
        })
    }

    // 用户登出
    const signOut = () => {
        eventBus.$emit('LogoutNotify')
    }

    return {
        loading,
        user,
        signUp,
        signIn,
        signOut,
    }
}