import { defineStore } from 'pinia'
import { apiDict, apiCategory } from 'API/common'
import type { AxiosResponse } from 'axios'
import router from 'Plugins/router'

// type DictData =
type CommonStoreState = {
  dict: { [key: string]: Array<{ value: string; text: string }> }
  // category: { [key: string]: Array<{ value: string; text: string }> }
  category: Array<{key: string, title:string}>
}
// 第一个参数是该 store 的唯一 id
const commonStore = defineStore('common', {
  state: (): CommonStoreState => {
    return {
      dict: {},
      category: []
    }
  },
  actions: {
    async getDict(code: string) {
      let target = this.dict[code]
      if (target) {
        // console.log('已有分类', target)
        return target
      } else {
        const res = await apiDict({ code })
        this.dict[code] = res.data.result
      }
    },
    async getCategory(code: string = 'C04') {
      if(!this.category || this.getCategory.length === 0){
        console.log('---获取全局字典：分类---')
        const res = await apiCategory({ all: true, code })

        if(res.data.success){
          this.category = res.data.result || []
        }
      }
    }
  },
  // other options...
})

export default commonStore

