import { reactive, toRefs } from 'vue'
import { defineStore } from 'pinia'
import { userLogin } from '../api/user'
import router from '../router'
// import { useUserStore } from '@/stores/user';

// import { useRouterStore } from './router'
import { message, notify } from '@/utils/message.js';

// const myMessage = new Message()
export const useUserStore = defineStore(
    'user',
    () => {
        const state = reactive({
            userInfo: {},
            token:'',
            download_type:'', //网页内下载
            idlist:[],
            userInfoList: []
        })
        const loginMethods = async (formInfo) => {
            // const routerStore = useRouterStore()
            const res = await userLogin(formInfo)
            console.log(res)
            if (res.code == 200) {
                console.log('登录')
                // routerStore.increment(routerStore)
                router.replace('/home')
                notify('登录成功！')
                state.download_type ='disk'
                Object.assign(state.userInfo, res.data)
                setUserInfoList([JSON.parse(JSON.stringify(state.userInfo))])
                state.token = res.data.id
            }else{
                notify(res.desc);
            }
        }
        // 账号密码验证方法
        const verifyMethods = async (formInfo) => {
            // const routerStore = useRouterStore()
            const res = await userLogin(formInfo)
            return new Promise((resolve, reject) => {
                console.log(res)
                if (res.code == 200) {
                    console.log('账号密码验证-----')
                    // 是否管理员
                    const isManager = res.data?.grade > 0
                    if (!isManager) {
                        notify('验证人必须是管理员','danger');
                        return
                    } else if (res?.data?.id === state?.userInfo?.id) {
                        notify('验证人不可以是当前登录人', 'danger');
                        return
                    } else {
                        // 存到userInfoList，x-token里面拼接传给后端，生成操作记录用
                        let newUserList = [JSON.parse(JSON.stringify(state.userInfo))]
                        newUserList.push(res.data)
                        setUserInfoList(newUserList)
                        notify('验证成功！');
                        resolve(true)
                    }
                } else {
                   notify(res.desc);
                }
            })
        }
        const faceLoginMethods = async (res) => {
            console.log(res,'11111111')
            router.push('/home')
            // state.download_type ='disk'
            Object.assign(state.userInfo, res.data.data)
            setUserInfoList([JSON.parse(JSON.stringify(state.userInfo))])
            state.token = res.data.id
        }
        // 设置userInfoList
        const setUserInfoList = (userList) => {
            if (!userList?.length) return
            state.userInfoList = []
            state.userInfoList = [...userList]
        }
        return { ...toRefs(state), loginMethods, verifyMethods, faceLoginMethods, setUserInfoList }
    },
    { persist: true }
)
