// 定义组合式API仓库
import { defineStore } from 'pinia'
import { computed } from 'vue'
import { getPrefixName } from '@/config'
import { goToPage } from '@/utils/util'
import type { userInfoInt } from '@/apis/login/type'
import { loginApi } from '@/apis/login/index'
// import { getUserProfile } from '@/apis/systemuser'

interface userInfoStoreInt {
  token: string
  user: any
  appToken: string
  avatar: string
  nick: string
  userId: string
  username: string
  userpassword: string
  huodianlng: any
  huodianlat: any
  mapview: any
  mapadu: any
  isBack: boolean
  wzlocation: any
}

// 创建小仓库
export default defineStore(
  getPrefixName('user'),
  () => {
    const userInfo = ref<userInfoStoreInt>({
      token: '',
      user: {},
      appToken: '',
      avatar: '',
      nick: '',
      userId: '',
      username: '',
      userpassword: '',
      huodianlng: null,
      huodianlat: null,
      mapview: null,
      mapadu: null,
      isBack: false,
      wzlocation: {}
    })
    const token = computed(() => {
      // debugger
      return userInfo.value.appToken || ''
    })
    const appToken = computed(() => {
      // debugger
      return userInfo.value.appToken || ''
    })
    const user = computed(() => {
      return userInfo.value.user
    })
    const nick = computed(() => {
      return userInfo.value.nick
    })

    const avatar = computed(() => {
      return userInfo.value.avatar
    })
    const huodianlng = computed(() => {
      return userInfo.value.huodianlng
    })
    const huodianlat = computed(() => {
      return userInfo.value.huodianlat
    })

    const username = computed(() => {
      return userInfo.value.username
    })
    const userpassword = computed(() => {
      return userInfo.value.userpassword
    })
    const mapview = computed(() => {
      return userInfo.value.mapview
    })
    const mapadu = computed(() => {
      return userInfo.value.mapadu
    })
    const isBack = computed(() => {
      return userInfo.value.isBack
    })
    const wzlocation = computed(() => {
      return userInfo.value.wzlocation
    })
    function setisBack(isBack: any) {
      userInfo.value.isBack = isBack
    }
    function setmapview(newmapview: any) {
      userInfo.value.mapview = newmapview
    }
    function setmapadu(newmapadu: any) {
      userInfo.value.mapview = newmapadu
    }
    function setlocation(locationobj: any) {
      // debugger
      userInfo.value.wzlocation = locationobj
    }
    function sethdlocation(locationobj: any) {
      // debugger
      userInfo.value.huodianlng = locationobj.lng
      userInfo.value.huodianlat = locationobj.lat
    }

    // 登录
    function login(params: userInfoInt) {
      return new Promise((resolve, reject) => {
        loginApi(params)
          .then((res: any) => {
            //  debugger
            userInfo.value = res.data
            userInfo.value.userpassword = params.password ? params.password : ''
            // getUserProfile({}).then((res1: any) => {
            //   debugger
            // })

            resolve(res)
          })
          .catch((err) => {
            reject(err)
          })
      })
    }
    // 退出登录
    function logOut() {
      uni.showToast({
        icon: 'none',
        title: '退出成功',
        mask: true,
        success() {
          setTimeout(() => {
            userInfo.value = {
              token: '',
              user: {},
              appToken: '',
              avatar: '',
              nick: '',
              userId: '',
              username: '',
              userpassword: '',
              huodianlng: null,
              huodianlat: null,
              mapview: null,
              mapadu: null,
              isBack: false,
              wzlocation: {}
            }
            uni.clearStorageSync()
            goToPage({
              url: 'pages/login/login',
              mode: 'reLaunch'
            })
          }, 1000)
        }
      })
    }
    return {
      token,
      appToken,
      user,
      nick,
      avatar,
      userInfo,
      username,
      userpassword,
      logOut,
      login,
      huodianlng,
      huodianlat,
      mapview,
      mapadu,
      setmapview,
      setmapadu,
      isBack,
      setisBack,
      wzlocation,
      setlocation,
      sethdlocation
    }
  },
  {
    // unistorage: {
    //   serializer: {
    //     // 序列化，默认为 JSON.stringify
    //     serialize(v) {
    //       return JSON.stringify(v)
    //     },
    //     // 反序列化，默认为 JSON.parse
    //     deserialize(v) {
    //       return JSON.parse(v)
    //     }
    //   }
    // }
    // 开启后对 state 的数据读写都将持久化
    // unistorage: {
    //     key: 'userInfo', // 缓存的键，默认为该 store 的 id，这里是 main,
    //     paths: ['userInfo.token'], // 需要缓存的路径，这里设置 foo 和 nested 下的 data 会被缓存
    //     // 初始化恢复前触发
    //     beforeRestore(ctx: any) {
    //         console.log(ctx)
    //     },
    //     // 初始化恢复后触发
    //     afterRestore(ctx: any) {
    //         console.log('ctx', ctx)
    //     },
    // },
  }
)
