import { ref } from 'vue'
import * as pubApi from '@/api/pub'

export interface DictItem {
  label: string
  value: ID
}

/**
 * 获取字典值列表
 * @param dicType 字典类型
 * @example 使用示例
 * const { dictList, fetchDict } = useDict('DIC_TYPE')
 * @enum { string } institution_type 机构类型
 */
export const useDict = (dicType: string) => {
  const dictList = ref<DictItem[]>([])

  const fetchDict = async () => {
    console.log('👏👏👏useDict fetchDict', dicType)
    try {
      const res = await pubApi.queryDictList({ dicType })
      const data = res.data || []
      dictList.value = Array.isArray(data)
        ? data.map((item) => ({
            label: item.itemName,
            value: item.itemCode,
          }))
        : []
    } catch (error) {
      console.error('获取字典数据失败:', error)
      dictList.value = []
    }
  }

  return {
    dictList,
    fetchDict,
  }
}
