import { useWeAppAuthStore } from '@/common'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { showToastBack } from '@/common/libraries/naviHelper'
import useModel from './model'
import { Apis } from '@/gen/Apis'
import { avatarUpload } from '@/common/libraries/upload'

const auth = useWeAppAuthStore()
const handleChangeUserInfo = (data: any) => {
  getApiLoading(Apis.Login.Auth.ChangeAvatar, data).then(res => {
    auth.data.user = {
      ...auth.data.user,
      ...data
    }
    useModel.formData.value = {
      ...auth.data.user,
      ...data
    }
    uni.showToast({
      icon: 'success',
      title: '编辑成功！',
      duration: 2000
    })
    console.log(res)
  })
}

export default {
  init() {
    useModel.formData.value = auth?.data?.user || {}
  },
  handleChooseAvatar: async (e: any) => {
    if (e?.detail?.avatarUrl) {
      let data = await avatarUpload(e?.detail?.avatarUrl)
      handleChangeUserInfo({ avatar: data })
      console.log(data, 'filePath')
    }
    console.log(e, 'eeeee')
  },
  handleConfirm(e: string) {
    handleChangeUserInfo({ name: e })
    console.log(e, 'eeeee')
  },
  handleLogout() {
    getApiLoading(Apis.Login.Auth.Logout, {}).then(res => {
      auth.data = {
        user: undefined
      }
      showToastBack('退出成功！', 1, true)
      console.log(res)
    })
  }
}
