import { defineStore } from 'pinia'
import { getAgentInfo } from '@/api/agent'

export const useAgentStore = defineStore('agent', {
  state: () => ({
    isLoaded: false,
    level: 0,
    levelName: '',
    isAgent: false,
    agentID: null,
    agentCode: 0,
    mobile: '',
    region: '',
    wechatId: '',
    teamLeaderId: null,
    isRealName: false,
  }),
  getters: {
    levelDisplayName: (state) => {
      const levelMap = { 1: '普通代理', 2: '黄金代理', 3: '钻石代理' }
      return levelMap[state.level] || '普通代理'
    },
  },
  actions: {
    async fetchAgentStatus() {
      const token = typeof uni !== 'undefined' && uni.getStorageSync ? uni.getStorageSync('token') : ''
      if (!token) {
        this.isLoaded = true
        return
      }
      const { data, error } = await getAgentInfo()
      if (data.value && !error.value) {
        if (data.value.code === 200) {
          const agentData = data.value.data
          if (!agentData.agent_id || agentData.agent_id === '') {
            this.resetAgent()
          } else {
            this.level = agentData.level || 0
            this.levelName = agentData.level_name || ''
            this.isAgent = true
            this.agentID = agentData.agent_id
            this.agentCode = agentData.agent_code || 0
            this.mobile = agentData.mobile || ''
            this.region = agentData.region || ''
            this.wechatId = agentData.wechat_id || ''
            this.teamLeaderId = agentData.team_leader_id || null
            this.isRealName = agentData.is_real_name || false
            uni.setStorageSync('agentInfo', JSON.stringify({
              isAgent: this.isAgent,
              level: this.level,
              levelName: this.levelName,
              agentID: this.agentID,
              agentCode: this.agentCode,
              mobile: this.mobile,
              region: this.region,
              wechatId: this.wechatId,
              teamLeaderId: this.teamLeaderId,
              isRealName: this.isRealName,
            }))
          }
        } else if (data.value.code !== 100010) {
          this.resetAgent()
        }
      } else {
        this.resetAgent()
      }
      this.isLoaded = true
    },
    updateAgentInfo(agentInfo) {
      if (agentInfo) {
        this.isAgent = agentInfo.isAgent || false
        this.level = agentInfo.level || 0
        this.levelName = agentInfo.levelName || ''
        this.agentID = agentInfo.agentID || null
        this.mobile = agentInfo.mobile || ''
        this.region = agentInfo.region || ''
        this.wechatId = agentInfo.wechatId || ''
        this.teamLeaderId = agentInfo.teamLeaderId || null
        this.isRealName = agentInfo.isRealName || false
        this.isLoaded = true
      }
    },
    resetAgent() {
      this.isLoaded = false
      this.level = 0
      this.levelName = ''
      this.isAgent = false
      this.agentID = null
      this.agentCode = 0
      this.mobile = ''
      this.region = ''
      this.wechatId = ''
      this.teamLeaderId = null
      this.isRealName = false
    },
  },
})
