import { defineStore } from 'pinia'
import { userProfile } from '@/api/index'

export const user = defineStore('user', {
  // 推荐使用 完整类型推断的箭头函数
  // persist: true, // 持久化储存
  state: () => {
    return {
      // 所有这些属性都将自动推断其类型
      isQingQue: import.meta.env.VITE_LOGO === 'QingQue',
      projectName: import.meta.env.VITE_PROJECT_NAME,
      keywords: '',
      profile: {} as any,
      menuPath: [
        { text: '首页', value: 1, url: '/home' },
        { text: '分类', value: 2, url: '/cate' },
        { text: '我的游戏', value: 3, url: '/my_game' }
      ],
      isLogin: false,
      loginFormVisible: false
    }
  },
  getters: {

  },
  actions: {
    setProfile(info:any) {
      this.profile = info
    },
    async getUserProfile() {
      // from 2 手机登录   1密码登录
      await userProfile({ from: parseInt(localStorage.getItem('from') as string) }).then((res) => {
        console.log(res)
        this.setProfile(res.data.data)
        this.menuPath = [
          { text: '首页', value: 1, url: '/home' },
          { text: '分类', value: 2, url: '/cate' },
          { text: '订单', value: 3, url: '/order' },
          { text: this.profile.ecoinName + ':' + (this.profile.ecoin / 100).toFixed(2), value: 5, url: '/coin_type2' },
          { text: this.profile.ncoinName + ':' + (this.profile.ncoin / 100).toFixed(2), value: 6, url: '/coin_type1' },
          { text: '我的游戏', value: 4, url: '/my_game' }
        ]
      }).catch((error) => {
        console.log(error)
      })
    }
  }
})
