import { defineStore } from 'pinia'
import { store } from '@/store'
import { getIzCustomtypeList, getIzProjectList, getIzShowList, getIzcustomlevel, getIzfirmList, getIzproductnameList, getIzprojecttypeList, getIztaskrromList, getIztaskstatusList, getIzvisitcityList, getIzvisitproList, getPersonList, getPictureTypeList, getPlanList, getRegionList, getTFList, getizcustomnameList } from '@/api/dictionary'

export interface DictionaryState {
  regionList: any
  pictureTypeList: any
  personList: any
  planList: any
  tfList: any
  izShowList: any
  izProjectList: any
  iztaskrromList: any
  iztaskstatusList: any
  izvisitproList: any
  izfirmList: any
  izproductnameList: any
  izCustomtypeList: any
  izcustomnameList: any
  izcustomlevelList: any
  izizprojecttypeList: any
  izvisitcityList: any
}

export const useDictionaryStore = defineStore({
  id: 'app-dictionary',
  state: (): DictionaryState => ({
    tfList: null,
    regionList: null,
    pictureTypeList: null,
    personList: null,
    planList: null,
    izShowList: null,
    izProjectList: null,
    iztaskrromList: null,
    iztaskstatusList: null,
    izvisitproList: null,
    izfirmList: null,
    izproductnameList: null,
    izcustomnameList: null,
    izCustomtypeList: null,
    izcustomlevelList: null,
    izizprojecttypeList: null,
    izvisitcityList: null,
  }),
  getters: {
    getRegionList: (state) => {
      return state.regionList
    },
    getizpicturetype: (state) => {
      return state.pictureTypeList
    },
    getizplan: (state) => {
      return state.planList
    },
    getizupuser: (state) => {
      return state.personList
    },
    getizshow: (state) => {
      return state.izShowList
    },
    getizproject: (state) => {
      return state.izProjectList
    },
    getiztaskrrom: (state) => {
      return state.iztaskrromList
    },
    getiztaskstatus: (state) => {
      return state.iztaskstatusList
    },
    getizvisitpro: (state) => {
      return state.izvisitproList
    },
    getizfirm: (state) => {
      return state.izfirmList
    },
    getizproductname: (state) => {
      return state.izproductnameList
    },
    getizcustomname: (state) => {
      return state.izcustomnameList
    },
    getizcustomtype: (state) => {
      return state.izCustomtypeList
    },
    getizcustomlevel: (state) => {
      return state.izcustomlevelList
    },
    getizizprojecttype: (state) => {
      return state.izizprojecttypeList
    },
  },
  actions: {
    settfList(config) {
      this.tfList = config
    },
    // 获取地区列表
    async fetchRegionList() {
      const list = await getRegionList()
      this.regionList = generateOptions(list, 'id', 'cityname')
      return this.regionList
    },
    // 获取图片类型列表
    async fetchPictureTypeList() {
      const list = await getPictureTypeList()
      this.pictureTypeList = generateOptions2(list, 'id', 'typename')
      return this.pictureTypeList
    },
    // 获取提报人列表
    async fetchPersionList() {
      const list = await getPersonList()
      this.personList = generateOptions(list, 'id', 'upname')
      return this.personList
    },
    // 获取计划列表
    async fetchPlanList() {
      const list = await getPlanList()
      this.planList = generateOptions(list, 'id', 'planname')
      return this.planList
    },
    // 获取图片真假原因
    async fetchTFList() {
      const list = await getTFList()
      this.tfList = generateOptions2(list)
      return this.tfList
    },
    // 分类列表
    async fetchIzShowList() {
      const list = await getIzShowList()
      this.izShowList = generateOptions2(list, 'id', 'classname')
      return this.izShowList
    },
    async fetchIzProjectList() {
      const list = await getIzProjectList()
      this.izProjectList = generateOptions(list, 'id', 'categoryname')
      return this.izProjectList
    },
    async fetchIztaskrromList() {
      const list = await getIztaskrromList('1')
      this.izCustomtypeList = generateOptions(list)
      return this.izCustomtypeList
    },
    async fetchIztaskstatusList() {
      const list = await getIztaskstatusList('1')
      this.iztaskstatusList = generateOptions(list)
      return this.iztaskstatusList
    },
    async fetchIzvisitproList() {
      const list = await getIzvisitproList()
      this.izvisitproList = generateOptions(list, 'id', 'proname')
      return this.izvisitproList
    },
    async fetchIzfirmList() {
      const list = await getIzfirmList('1')
      this.izfirmList = generateOptions(list)
      return this.izfirmList
    },
    async fetchIzproductnameList() {
      const list = await getIzproductnameList('1')
      this.izproductnameList = generateOptions(list)
      return this.izproductnameList
    },
    async fetchizcustomnameList() {
      const list = await getizcustomnameList('1')
      this.izcustomnameList = generateOptions(list)
      return this.izcustomnameList
    },
    async fetchIzCustomtypeList() {
      const list = await getIzCustomtypeList()
      this.izCustomtypeList = generateOptions(list)
      return this.izCustomtypeList
    },
    async fetchIzcustomlevelList() {
      const list = await getIzcustomlevel('1')
      this.izcustomlevelList = generateOptions(list)
      return this.izcustomlevelList
    },
    async fetchizizprojecttypeList() {
      const list = await getIzprojecttypeList('1')
      this.izizprojecttypeList = generateOptions(list)
      return this.izizprojecttypeList
    },
    async fetchizvisitcityList() {
      const list = await getIzvisitcityList()
      this.izvisitcityList = generateOptions(list)
      return this.izvisitcityList
    },
  },
})

// Need to be used outside the setup
export function useDictionary() {
  return useDictionaryStore(store)
}

// label和value都是name,不用id
function generateOptions(list: any[], idkey: string = 'id', namekey: string = 'name') {
  const items = list || []

  return items.map((item) => {
    return {
      label: item[namekey] || 'todo',
      value: item[namekey] || 'todo',
    }
  })
}

// label是name,value是id
function generateOptions2(list: any[], idkey: string = 'id', namekey: string = 'name') {
  const items = list || []

  return items.map((item) => {
    return {
      label: item[namekey] || 'todo',
      value: item[idkey] || 'todo',
    }
  })
}
