import { ref } from 'vue'
import { getListCraftApi, allErpCodeListApi } from '@/api/podCnOrder'
import { getUserMarkList, loadWarehouseListApi } from '@/api/common'
import { getAllCountryApi } from '@/api/logistics'
import type { ProductTypeGroup } from '../component/productTypeFilterTypes'
import {
  PRODUCT_TYPE_CUSTOM_PART_MULTIPLE,
  PRODUCT_TYPE_CUSTOM_PART_SINGLE,
  PRODUCT_TYPE_POD_MULTIPLE,
  PRODUCT_TYPE_POD_SINGLE,
} from '../utils/productMarkQuery'
import type { IAllList } from '@/types/api/podUsOrder'
import type { CraftListData } from '@/types/api/podCnOrder'
import type { WarehouseListData } from '@/types'
import { getCustomTagListPodOrderApi } from '@/api/factoryOrderNew'

interface LogisticsCodeItem {
  code: string
  basicsName: string
  apiData: unknown
}

const processTypeMap: Record<string, string> = {
  TH: '烫画',
  ZP: '直喷',
  CX: '刺绣',
  DK: '雕刻',
  BP: '白胚',
  QT: '其他',
}

export function useOrderDictionaries() {
  const userMarkList = ref<string[]>([])
  const receiverCountryList = ref<{ countryCode: string; nameCn: string }[]>([])
  const customTagList = ref<{ id: string; name: string }[]>([])
  const allCodelist = ref<LogisticsCodeItem[]>([])
  const craftList = ref<IAllList[]>([])
  const warehouseList = ref<WarehouseListData[]>([])

  const sourceList = [
    { name: 'erp推送', id: 'jomall-erp' },
    { name: '第三方推送', id: 'third-party' },
  ]
  const sizes = ['FS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', '3XL', '4XL', '5XL']
  const productTypeGroups = ref<ProductTypeGroup[]>([
    {
      label: '普品',
      value: 'normal',
      children: [{ label: '普品', value: 'normal' }],
    },
    {
      label: '胚衣',
      value: 'custom_normal',
      children: [{ label: '胚衣', value: 'custom_normal' }],
    },
    {
      label: 'POD商品',
      value: 'pod',
      children: [
        { label: '单面', value: PRODUCT_TYPE_POD_SINGLE },
        { label: '多面', value: PRODUCT_TYPE_POD_MULTIPLE },
      ],
    },
    {
      label: '一件定制局部印',
      value: 'custom_part',
      children: [
        { label: '单面', value: PRODUCT_TYPE_CUSTOM_PART_SINGLE },
        { label: '多面', value: PRODUCT_TYPE_CUSTOM_PART_MULTIPLE },
      ],
    },
  ])

  const getUserMark = async () => {
    try {
      const res = await getUserMarkList()
      userMarkList.value = res.data
    } catch (_e) {
      /* empty */
    }
  }

  const getReceiverCountryList = async () => {
    try {
      const res = await getAllCountryApi()
      if (res.code !== 200) return
      receiverCountryList.value = res.data
    } catch (_e) {
      /* empty */
    }
  }

  const getCustomTagList = async () => {
    try {
      const res = await getCustomTagListPodOrderApi()
      if (res.code !== 200) return
      customTagList.value = res.data
    } catch (_e) {
      /* empty */
    }
  }

  const getLogisticsCompanyAllCodelist = async () => {
    try {
      const res = await allErpCodeListApi()
      if (res.code !== 200) return
      allCodelist.value = res.data
    } catch (_e) {
      /* empty */
    }
  }

  const loadCraftList = async () => {
    try {
      const res = await getListCraftApi()
      if (res.code !== 200) return
      const data: CraftListData[] = res.data
      craftList.value = data.map((item) => ({
        id: item.craftCode,
        name: item.craftName,
        warehouseName: processTypeMap[item.craftType] ?? '其他',
      }))
    } catch (_e) {
      /* empty */
    }
  }

  const loadWarehouseList = async () => {
    try {
      const res = await loadWarehouseListApi()
      if (res.code !== 200) return
      warehouseList.value = res.data
    } catch (e) {
      console.error(e)
    }
  }

  const loadAllDictionaries = async () => {
    await Promise.all([
      loadCraftList(),
      getUserMark(),
      getCustomTagList(),
      getLogisticsCompanyAllCodelist(),
      getReceiverCountryList(),
      loadWarehouseList(),
    ])
  }

  return {
    userMarkList,
    receiverCountryList,
    customTagList,
    allCodelist,
    craftList,
    warehouseList,
    sourceList,
    sizes,
    productTypeGroups,
    loadCraftList,
    getUserMark,
    getCustomTagList,
    getLogisticsCompanyAllCodelist,
    getReceiverCountryList,
    loadWarehouseList,
    loadAllDictionaries,
  }
}
