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()
export default {
  init() {
    useModel.formData.value = JSON.parse(JSON.stringify(auth?.data?.user || {}))
  },
  handleChooseAvatar: async (e: any) => {
    console.log(e, 'eeeee')
    if (e?.detail?.avatarUrl) {
      let data = await avatarUpload(e?.detail?.avatarUrl)
      useModel.formData.value.avatar = data
      console.log(data, 'filePath')
    }
  },
  handleConfirm() {
    getApiLoading(Apis.Login.Auth.ChangeName, useModel.formData.value).then(res => {
      uni.showToast({
        icon: 'success',
        title: '编辑成功！',
        duration: 1000,
        success: () => {
          setTimeout(() => {
            auth.data.user = {
              ...auth.data.user,
              ...useModel.formData.value
            }
          }, 1000)
        }
      })
      console.log(res)
    })
  },
  handleLogout() {
    getApiLoading(Apis.Login.Auth.Logout, {}).then(res => {
      showToastBack('退出成功！', 1, true)
      setTimeout(() => {
          auth.data = {
            user: undefined,
            selected_house: undefined,
            config:  undefined
          }
        }, 1000)
      console.log(res)
    })
  }
}
