import { defineStore } from 'pinia'
import { getUserInfo } from '../api/user'

const userInformation = defineStore('user', {
  state: () => {
    return {
      phyName: '',
      idcard: '',
      age: '',
      sex: '',
      telepNumber: '',
      departName: ''
    }
  },
  actions: {
    // 将请求过来的个人信息赋值
    initInfo (info: any) {
      this.phyName = info.phyName
      this.idcard = info.idcard
      this.age = info.age
      this.sex = info.sex
      this.telepNumber = info.telepNumber
      this.departName = info.departName
    },
    // 请求个人信息
    fetchUserInfo () {
      getUserInfo({
        token: localStorage.getItem('token')
      }).then((res: any) => {
          console.log(res)
          this.initInfo(res.data.obj[0])
      })
    }
  },
  persist: true
})

export default userInformation
