import { CommonData, CommonValue } from '~/common'
import { Query } from '@/api/webApi'
import { ElMessage } from 'element-plus'

const goodsInfoItemsMap = {
  internal: [
    {
      label: '货物编码',
      key: 'C0',
    },
    {
      label: '运单',
      key: 'C1',
    },
  ],
  international: [
    {
      label: '货物编码',
      key: 'C0',
    },
    {
      label: '运单',
      key: 'C1',
    },
    {
      label: '运单类型',
      key: 'C2',
    },
  ],
}

const simulateGoodsInfoMap = {
  internal: {
    C0: '56888829',
    C1: 'FA67858990',
  },
  international: {
    C0: '56888829',
    C1: 'FA67858990',
    C2: '国际普货',
  },
}

export function useGoodsInfo(name: string, dataContent: CommonValue[]) {
  const goodsInfoItems = ref<{ label: string; key: string }[]>([])
  const getGoodsInfoItems = () => {
    goodsInfoItems.value =
      goodsInfoItemsMap[
        name.includes('International') ? 'international' : 'internal'
      ]
  }
  getGoodsInfoItems()

  const goodsInfo: CommonData = reactive({})
  const getGoodsInfo = async () => {
    try {
      const {
        code,
        returnData: { listValues },
        message,
      } = await Query<CommonData>({
        id: DATACONTENT_ID.goodsInfo,
        dataContent,
      })
      if (Number(code) !== 0) {
        throw new Error(message || '失败')
      }
      if (!listValues.length) {
        ElMessage.error('未查询到货物信息')
        return
      }
      Object.assign(goodsInfo, listValues[0])
    } catch (error) {
      console.error(error)
    }
  }
  const getSimulateGoodsInfo = () => {
    const simulateGoodsInfo =
      simulateGoodsInfoMap[
        name.includes('International') ? 'international' : 'internal'
      ]
    Object.assign(goodsInfo, simulateGoodsInfo)
  }
  onMounted(() => {
    // getGoodsInfo()
    getSimulateGoodsInfo()
  })
  return {
    goodsInfoItems,
    goodsInfo,
  }
}
