import { defineStore } from 'pinia'
import { store } from '@/store'
import { getBackList, getBusinessList, getIzShowList, getIztaskstatusList, getPictureTypeList, getRegionList, getRejectList, getRelationTypeList, getTFList, getizstatusList } 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
  izprojecttypeList: any
  izvisitcityList: any
  rejectList: any
  backList: any
  relationTypeList: any
  izstatusList: 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,
    izprojecttypeList: null,
    izvisitcityList: null,
    rejectList: null,
    backList: null,
    relationTypeList: null,
    izstatusList: 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
    },
    getizprojecttype: (state) => {
      return state.izprojecttypeList
    },
    getizstatus: (state) => {
      return state.izstatusList
    },
  },
  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 getBusinessList('person')
      this.personList = list
      return this.personList
    },
    // 获取计划列表
    async fetchPlanList() {
      const list = await getBusinessList('plan')
      this.planList = list
      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 getBusinessList('project')
      this.izProjectList = list
      return this.izProjectList
    },
    // 任务来源
    async fetchIztaskrromList() {
      const list = await getBusinessList('iztaskrrom')
      this.iztaskrromList = list
      return this.iztaskrromList
    },
    // 任务状态
    async fetchIztaskstatusList() {
      const list = await getBusinessList('iztaskstatus')
      this.iztaskstatusList = list
      return this.iztaskstatusList
    },
    // 地区(省信息)
    async fetchIzvisitproList() {
      const list = await getBusinessList('izvisitpro')
      this.izvisitproList = list
      return this.izvisitproList
    },
    // 厂商
    async fetchIzfirmList() {
      const list = await getBusinessList('izfirm')
      this.izfirmList = list
      return this.izfirmList
    },
    // 产品名称
    async fetchIzproductnameList() {
      const list = await getBusinessList('izproductname')
      this.izproductnameList = list
      return this.izproductnameList
    },
    // 客户名称
    async fetchizcustomnameList() {
      const list = await getBusinessList('izcustomname')
      this.izcustomnameList = list
      return this.izcustomnameList
    },
    // 客户类型
    async fetchIzCustomtypeList() {
      const list = await getBusinessList('izcustomtype')
      this.izCustomtypeList = list
      return this.izCustomtypeList
    },
    // 客户级别
    async fetchIzcustomlevelList() {
      const list = await getBusinessList('izcustomlevel')
      this.izcustomlevelList = list
      return this.izcustomlevelList
    },
    // 项目类别
    async fetchizizprojecttypeList() {
      const list = await getBusinessList('izprojecttype')
      this.izprojecttypeList = list
      return this.izprojecttypeList
    },
    // 发布地区
    async fetchizvisitcityList() {
      const list = await getBusinessList('izvisitcity')
      this.izvisitcityList = list
      return this.izvisitcityList
    },
    // 拒绝原因
    async fetchRejectList() {
      const list = await getRejectList()
      this.rejectList = generateOptions(list)
      return this.rejectList
    },
    // 退回方式
    async fetchBackList() {
      const list = await getBackList()
      this.backList = generateOptions(list)
      return this.backList
    },
    // 退回方式
    async fetchRelationTypeList() {
      const list = await getRelationTypeList()
      this.relationTypeList = generateOptions2(list, 'content', 'name')
      return this.relationTypeList
    },
    // 审核状态
    async fetchizstatusListt() {
      const list = await getizstatusList()
      this.izstatusList = generateOptions2(list, 'codeno', 'name')
      return this.backList
    },
  },
})

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

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

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

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

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