import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'

// 定义 Store
export const useMemberStore = defineStore(
  'member',
  () => {
    // 会员信息
    const profile = ref<any>()
    const token = ref<any>()
    const userInfo = ref<any>({
      //用户ID
      id: '',
      //渠道ID
      channelId: '',
      //手机号
      phone: '',
      //微信用户
      name: '',
      //昵称
      userName: '',
      //openId
      openId: '',
      //Miami
      //用户类型
      type: '',
      //密码
      password: '',
      //用户状态
      state: '',
      //用户头像
      photoPath: '',
      //是否授权
      isAuth: '',
      //渠道名字
      channelName: '',
      //邮箱
      mail: '',
      //岗位
      job: '',
      //公司名称
      compAddress: '',
      //公司头像
      channelAvatar: '',
      //海报
      posterBackground: '',
    })

    //用户scene唯一标识保存
    const scene = ref<string>('')
    //保存scene唯一标识信息
    const saveScene = (val: string) => {
      scene.value = val
    }
    //保存用户信息
    const saveUserInfo = (val: any) => {
      userInfo.value = val
    }
    //清除用户信息
    const clearUserInfo = () => {
      userInfo.value = {}
    }
    //保存token  token校验时使用
    const saveToken = (val: string) => {
      token.value = val
    }
    //清除token
    const clearToken = () => {
      token.value = ''
    }
    // 保存会员信息，登录时使用
    const setProfile = (val: any) => {
      profile.value = val
    }

    // 清理会员信息，退出时使用
    const clearProfile = () => {
      profile.value = undefined
    }

    // 记得 return
    return {
      profile,
      token,
      userInfo,
      scene,
      saveScene,
      saveToken,
      clearToken,
      setProfile,
      clearProfile,
      saveUserInfo,
      clearUserInfo,
    }
  },
  // TODO: 持久化
  {
    //网页版是这样设置
    // persist: true,
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
