import { Query } from '@/api/webApi'
import { ellipsisCell } from '@/components/SimpleTable/customRender'
import { CommonData, CommonTableColumn } from '~/common'

interface SimpleColumn {
  columnName: string
  columnLabel: string
  [x: string]: any
}

const columnGroupsMap: {
  [x: string]: {
    groupName: string
    children: SimpleColumn[]
  }[]
} = {
  DepartureAirport: [
    {
      groupName: '航班相关',
      children: [
        {
          columnName: 'IATACode',
          columnLabel: '航司',
          needFilters: 1,
          fixed: true,
        },
        {
          columnName: 'flightNO',
          columnLabel: '航班号',
          needFilters: 1,
          fixed: true,
        },
        {
          columnName: 'flightDate',
          columnLabel: '执飞日期',
          width: 73,
          fixed: true,
        },
        {
          columnName: 'planDepartureTime',
          columnLabel: '计划起飞\n时间',
          width: 73,
          fixed: true,
        },
        {
          columnName: 'acDepartureTime',
          columnLabel: '实际起飞\n时间',
          width: 73,
          fixed: true,
        },
        {
          columnName: 'target',
          columnLabel: '航程',
          width: 100,
          needFilters: 1,
          fixed: true,
        },
        {
          columnName: 'planeType',
          columnLabel: '机型',
          needFilters: 1,
          fixed: true,
        },
        {
          columnName: 'flightType',
          columnLabel: '航班类型',
          needFilters: 1,
          fixed: true,
        },
        {
          columnName: 'takeOffStand',
          columnLabel: '停机位',
          fixed: true,
        },
        // {
        //   columnName: 'lastflightNO',
        //   columnLabel: '前序航班',
        //   fixed: true,
        //   defaultHidden: true,
        // },
        // {
        //   columnName: 'acLandingTime',
        //   columnLabel: '实际降落\n时间',
        //   width: 73,
        // },
        {
          columnName: 'jiahuo',
          columnLabel: '一个码标记',
        },
      ],
    },
    {
      groupName: '运单相关',
      children: [
        {
          columnName: 'speCargoInfo',
          columnLabel: '特货信息',
          customRender: ellipsisCell,
        },
        {
          columnName: 'preLoad',
          columnLabel: '运单申报\n(运单/件)',
        },
        // {
        //   columnName: 'transIn',
        //   columnLabel: '中转进\n(运单/件)',
        // },
      ],
    },
    {
      groupName: '货站相关',
      children: [
        {
          columnName: 'receiveSure',
          columnLabel: '收运核单\n(运单/件/重量)',
        },
        {
          columnName: 'receiveSureTime',
          columnLabel: '收运核单\n时间',
        },
        // {
        //   columnName: 'receiveSure_No',
        //   columnLabel: '核单不通过\n(运单/件/重量)',
        // },
        // {
        //   columnName: 'receiveSureTime_No',
        //   columnLabel: '核单不通过\n时间',
        // },
        {
          columnName: 'checks',
          columnLabel: '收运核查\n(拒运/查验)',
          defaultHidden: true,
        },
        {
          columnName: 'checkTime',
          columnLabel: '收运核查\n时间',
          defaultHidden: true,
        },
        {
          columnName: 'securityYes',
          columnLabel: '安检放行\n(运单/件)',
        },
        {
          columnName: 'securityTime',
          columnLabel: '安检放行\n时间',
        },
        {
          columnName: 'addCargo',
          columnLabel: '加货\n(运单/板卡/件/重量)',
        },
        {
          columnName: 'addCargoTime',
          columnLabel: '加货时间',
        },
        // {
        //   columnName: 'wait',
        //   columnLabel: '待运区\n(板卡/件)',
        // },
        // {
        //   columnName: 'waitTime',
        //   columnLabel: '待运区时间',
        // },
        {
          columnName: 'stowage',
          columnLabel: '预配载\n(板卡/件)',
        },
        {
          columnName: 'stowageTime',
          columnLabel: '预配载时间',
        },
      ],
    },
    {
      groupName: '地服相关',
      children: [
        // {
        //   columnName: 'planJoinTime',
        //   columnLabel: '计划交接\n时间',
        // },
        // {
        //   columnName: 'requestDepotJoin',
        //   columnLabel: '发起货站交接\n(板卡/件)',
        // },
        // {
        //   columnName: 'requestDepotJoinTime',
        //   columnLabel: '发起货站交接\n时间',
        // },
        // {
        //   columnName: 'depotJoin',
        //   columnLabel: '货站交接\n(板卡/件)',
        // },
        // {
        //   columnName: 'depotJoinTime',
        //   columnLabel: '货站交接\n时间',
        // },
        {
          columnName: 'resure',
          columnLabel: '交接复核\n(板卡/件)',
        },
        {
          columnName: 'resureTime',
          columnLabel: '交接复核\n时间',
        },
        // {
        //   columnName: 'planeDown',
        //   columnLabel: '机下交接\n(板卡/件)',
        //   columnDescribe: '根据交接复核计算所得',
        // },
        {
          columnName: 'planeDownTime',
          columnLabel: '机下交接\n时间',
        },
        // {
        //   columnName: 'loadPlaneSure',
        //   columnLabel: '装机\n(板卡/件)',
        // },
        {
          columnName: 'loadPlaneSureTime',
          columnLabel: '装机完成\n时间',
          defaultHidden: true,
        },
        // {
        //   columnName: 'loadPlane',
        //   columnLabel: '关闭舱门\n(板卡/件)',
        // },
        {
          columnName: 'loadPlaneTime',
          columnLabel: '关闭舱门\n时间',
        },
        {
          columnName: 'register',
          columnLabel: '拉下登记\n(板卡/件)',
        },
        {
          columnName: 'pullRegisterTime',
          columnLabel: '拉下登记\n时间',
        },
        {
          columnName: 'pullSure',
          columnLabel: '拉回确认\n(板卡/件)',
        },
        {
          columnName: 'pullSureTime',
          columnLabel: '拉回确认\n时间',
        },
        {
          columnName: 'returns',
          columnLabel: '退运\n(板卡/件)',
        },
        {
          columnName: 'returnTime',
          columnLabel: '退运时间',
        },
      ],
    },
  ],
  ArrivalAirport: [
    {
      groupName: '航班相关',
      children: [
        {
          columnName: 'IATACode',
          columnLabel: '航司',
          needFilters: 1,
        },
        {
          columnName: 'flightNO',
          columnLabel: '航班号',
          needFilters: 1,
        },
        {
          columnName: 'flightDate',
          columnLabel: '执飞日期',
        },
        {
          columnName: 'planLandingTime',
          columnLabel: '计划降落\n时间',
        },
        {
          columnName: 'acLandingTime',
          columnLabel: '实际降落\n时间',
        },
        {
          columnName: 'target',
          columnLabel: '航程',
          width: 100,
          needFilters: 1,
        },
        {
          columnName: 'planeType',
          columnLabel: '机型',
          needFilters: 1,
        },
        {
          columnName: 'flightType',
          columnLabel: '航班类型',
          needFilters: 1,
        },
        {
          columnName: 'landingStand',
          columnLabel: '停机位',
        },
        {
          columnName: 'speCargoInfo',
          columnLabel: '特货信息',
          customRender: ellipsisCell,
        },
        // {
        //   columnName: 'preUnLoad',
        //   columnLabel: '预计卸载数\n(运单/件)',
        // },
        // {
        //   columnName: 'transOut',
        //   columnLabel: '中转出\n(运单/件)',
        // },
        // {
        //   columnName: 'receiveDiff',
        //   columnLabel: '收货差异\n(板卡/件)',
        // },
      ],
    },
    {
      groupName: '地服相关',
      children: [
        // {
        //   columnName: 'unloadBill',
        //   columnLabel: '卸机单\n(板/箱/卡)',
        // },
        // {
        //   columnName: 'unloadBillTime',
        //   columnLabel: '卸机单确认\n时间',
        // },
        {
          columnName: 'unLoad',
          columnLabel: '卸机\n(板/箱/卡)',
        },
        {
          columnName: 'unLoadTime',
          columnLabel: '卸机时间',
        },
        {
          columnName: 'planeDown',
          columnLabel: '库区到达\n(板/箱/卡)',
        },
        {
          columnName: 'planeDownTime_IN',
          columnLabel: '库区到达\n时间',
        },
      ],
    },
    {
      groupName: '货站相关',
      children: [
        {
          columnName: 'depotJoin',
          columnLabel: '货站交接\n(板/箱/卡)',
        },
        {
          columnName: 'depotJoinTime_IN',
          columnLabel: '货站交接\n时间',
        },
        {
          columnName: 'tally',
          columnLabel: '理货\n(运单/件/重量)',
        },
        {
          columnName: 'tallyTime_in',
          columnLabel: '理货时间',
        },
        {
          columnName: 'outWarehouse',
          columnLabel: '出库\n(运单/件/重量)',
        },
        {
          columnName: 'outWarehouseTime',
          columnLabel: '出库时间',
        },
      ],
    },
  ],
  InternationalDepartureAirport: [
    {
      groupName: '航班相关',
      children: [
        {
          columnName: 'IATACode',
          columnLabel: '航司',
          needFilters: 1,
          fixed: true,
        },
        {
          columnName: 'flightNO',
          columnLabel: '航班号',
          needFilters: 1,
          fixed: true,
        },
        {
          columnName: 'flightDate',
          columnLabel: '执飞日期',
          width: 73,
          fixed: true,
        },
        {
          columnName: 'planDepartureTime',
          columnLabel: '计划起飞\n时间',
          width: 73,
          fixed: true,
        },
        {
          columnName: 'acDepartureTime',
          columnLabel: '实际起飞\n时间',
          width: 73,
          fixed: true,
        },
        {
          columnName: 'target',
          columnLabel: '航程',
          width: 100,
          needFilters: 1,
          fixed: true,
        },
        {
          columnName: 'planeType',
          columnLabel: '机型',
          needFilters: 1,
          fixed: true,
        },
        {
          columnName: 'flightType',
          columnLabel: '航班类型',
          needFilters: 1,
          fixed: true,
        },
        {
          columnName: 'takeOffStand',
          columnLabel: '停机位',
          fixed: true,
        },
        // {
        //   columnName: 'lastflightNO',
        //   columnLabel: '前序航班',
        //   fixed: true,
        // },
        // {
        //   columnName: 'acLandingTime',
        //   columnLabel: '实际降落\n时间',
        //   width: 73,
        // },
        {
          columnName: 'speCargoInfo',
          columnLabel: '特货信息',
          customRender: ellipsisCell,
        },
        // {
        //   columnName: 'transIn',
        //   columnLabel: '中转进\n(运单/件)',
        // },
        // {
        //   columnName: 'returns1',
        //   columnLabel: '退运\n(板卡/件)',
        // },
      ],
    },
    {
      groupName: '货站相关',
      children: [
        // {
        //   columnName: 'preLoad',
        //   columnLabel: '运单申报\n(运单/件)',
        // },
        {
          columnName: 'enterPark',
          columnLabel: '入园\n(运单/件)',
          labelClassName: 'bg-purple',
        },
        {
          columnName: 'enterParkTime',
          columnLabel: '入园时间',
          labelClassName: 'bg-purple',
        },
        {
          columnName: 'enterCustoms',
          columnLabel: '海关中控\n(运单/件)',
          labelClassName: 'bg-purple',
          defaultHidden: true,
        },
        {
          columnName: 'enterCustomsTime',
          columnLabel: '海关中控\n时间',
          labelClassName: 'bg-purple',
          defaultHidden: true,
        },
        {
          columnName: 'passCustoms',
          columnLabel: '海关放行\n(运单/件)',
          labelClassName: 'bg-purple',
        },
        {
          columnName: 'passCustomsTime',
          columnLabel: '海关放行\n时间',
          labelClassName: 'bg-purple',
        },
        // {
        //   columnName: 'nopassCustoms',
        //   columnLabel: '海关不通过\n(运单/件)',
        // },
        // {
        //   columnName: 'nopassCustomsTime',
        //   columnLabel: '海关不通过\n时间',
        // },
        {
          columnName: 'receiveSure1',
          columnLabel: '运抵货站\n(运单/件)',
          // labelClassName: 'bg-purple',
        },
        {
          columnName: 'receiveSureTime',
          columnLabel: '运抵货站\n时间',
          // labelClassName: 'bg-purple',
        },
        // {
        //   columnName: 'returns2',
        //   columnLabel: '退运\n(板卡/件)',
        // },
        // {
        //   columnName: 'returnTime',
        //   columnLabel: '退运时间',
        // },
        {
          columnName: 'securityYes',
          columnLabel: '安检放行\n(运单/件)',
        },
        {
          columnName: 'securityTime',
          columnLabel: '安检放行\n时间',
        },
        {
          columnName: 'receiveSure2',
          columnLabel: '收运核单\n(运单/件)',
        },
        {
          columnName: 'receiveSureResultTime',
          columnLabel: '收运核单\n时间',
        },
        // {
        //   columnName: 'receiveSure_No',
        //   columnLabel: '核单不通过\n(运单/件)',
        // },
        // {
        //   columnName: 'receiveSureTime_No',
        //   columnLabel: '核单不通过\n时间',
        // },
        {
          columnName: 'tally_out',
          columnLabel: '理货\n(板卡/运单/件)',
        },
        {
          columnName: 'tallyTime_out',
          columnLabel: '理货时间',
        },
        // {
        //   columnName: 'planJoinTime',
        //   columnLabel: '计划交接\n时间',
        // },
        // {
        //   columnName: 'wait',
        //   columnLabel: '待运区\n(板卡/件)',
        // },
        // {
        //   columnName: 'waitTime',
        //   columnLabel: '待运区\n时间',
        // },

        {
          columnName: "concat(requestDepotJoinBoard,'/',requestDepotJoin)",
          columnLabel: '发起货站交接\n(板卡/件)',
        },
        {
          columnName: 'requestDepotJoinTime',
          columnLabel: '发起货站交接\n时间',
        },
      ],
    },
    {
      groupName: '地服相关',
      children: [
        // {
        //   columnName: 'depotJoin',
        //   columnLabel: '货站交接\n(板卡/件)',
        // },
        // {
        //   columnName: 'depotJoinTime',
        //   columnLabel: '货站交接\n时间',
        // },
        {
          columnName: 'resure',
          columnLabel: '交接复核\n(板卡/件)',
        },
        {
          columnName: 'resureTime',
          columnLabel: '交接复核\n时间',
        },
        // {
        //   columnName: 'planeDown',
        //   columnLabel: '机下交接\n(板卡/件)',
        //   columnDescribe: '根据交接复核计算所得',
        // },
        {
          columnName: 'planeDownTime',
          columnLabel: '机下交接\n时间',
        },
        // {
        //   columnName: "concat(loadPlaneSureBoard,'/',loadPlaneSure)",
        //   columnLabel: '装机\n(板卡/件)',
        // },
        {
          columnName: 'loadPlaneSureTime',
          columnLabel: '装机完成\n时间',
          defaultHidden: true,
        },
        // {
        //   columnName: 'loadPlane',
        //   columnLabel: '关闭舱门\n(板卡/件)',
        // },
        {
          columnName: 'loadPlaneTime',
          columnLabel: '关闭舱门\n时间',
        },
        {
          columnName: 'register',
          columnLabel: '拉下登记\n(板卡/运单/件)',
        },
        {
          columnName: 'pullregisterTime',
          columnLabel: '拉下登记\n时间',
        },
        {
          columnName: 'stowage',
          columnLabel: '实配\n(板卡/件)',
        },
        {
          columnName: 'stowageTime',
          columnLabel: '实配时间',
        },
        // {
        //   columnName: 'pullSure',
        //   columnLabel: '拉回确认\n(板卡/件)',
        // },
        // {
        //   columnName: 'pullSureTime',
        //   columnLabel: '拉回确认\n时间',
        // },
      ],
    },
  ],
  InternationalArrivalAirport: [
    {
      groupName: '航班相关',
      children: [
        {
          columnName: 'IATACode',
          columnLabel: '航司',
          needFilters: 1,
        },
        {
          columnName: 'flightNO',
          columnLabel: '航班号',
          needFilters: 1,
        },
        {
          columnName: 'flightDate',
          columnLabel: '执飞日期',
        },
        {
          columnName: 'planLandingTime',
          columnLabel: '计划降落\n时间',
        },
        {
          columnName: 'acLandingTime',
          columnLabel: '实际降落\n时间',
        },
        {
          columnName: 'target',
          columnLabel: '航程',
          width: 100,
          needFilters: 1,
        },
        {
          columnName: 'planeType',
          columnLabel: '机型',
          needFilters: 1,
        },
        {
          columnName: 'flightType',
          columnLabel: '航班类型',
          needFilters: 1,
        },
        {
          columnName: 'landingStand',
          columnLabel: '停机位',
        },
        {
          columnName: 'speCargoInfo',
          columnLabel: '特货信息',
          customRender: ellipsisCell,
        },
        // {
        //   columnName: 'preUnLoad',
        //   columnLabel: '预计卸载数\n(运单/件)',
        // },
        // {
        //   columnName: 'transOut',
        //   columnLabel: '中转出\n(运单/件)',
        // },
        // {
        //   columnName: 'receiveDiff',
        //   columnLabel: '收货差异\n(板卡/件)',
        // },
      ],
    },
    {
      groupName: '地服相关',
      children: [
        // {
        //   columnName: 'unloadBill',
        //   columnLabel: '卸机单\n(板/箱/卡)',
        // },
        // {
        //   columnName: 'unloadBillTime',
        //   columnLabel: '卸机时间',
        // },
        {
          columnName: 'unLoad',
          columnLabel: '卸机\n(板/箱/卡)',
        },
        {
          columnName: 'unLoadTime',
          columnLabel: '卸机时间',
        },
        {
          columnName: 'planeDown',
          columnLabel: '机下交接\n(板/箱/卡)',
        },
        {
          columnName: 'planeDownTime_IN',
          columnLabel: '机下交接\n时间',
        },
      ],
    },
    {
      groupName: '货站相关',
      children: [
        {
          columnName: 'depotJoin',
          columnLabel: '货站交接\n(板/箱/卡)',
        },
        {
          columnName: 'depotJoinTime_IN',
          columnLabel: '货站交接\n时间',
        },
        {
          columnName: 'tally',
          columnLabel: '理货\n(板卡/运单/件/重量)',
          width: 120,
        },
        {
          columnName: 'tallyTime_in',
          columnLabel: '理货时间',
        },
        {
          columnName: "concat(passCustomsList_in,'/',passCustoms_in)",
          columnLabel: '海关放行\n(运单/件)',
          labelClassName: 'bg-purple',
        },
        {
          columnName: 'passCustomsTime_in',
          columnLabel: '海关放行\n时间',
          labelClassName: 'bg-purple',
        },
        {
          columnName: 'outWarehouse',
          columnLabel: '出库\n(运单/件/重量)',
        },
        {
          columnName: 'outWarehouseTime',
          columnLabel: '出库时间',
        },
        // {
        //   columnName: "concat(punishCustomsList_in,'/',punishCustoms_in)",
        //   columnLabel: '海关罚没\n(运单/件)',
        // },
        // {
        //   columnName: 'punishCustomsTime_in',
        //   columnLabel: '海关罚没\n时间',
        // },
      ],
    },
  ],
}

const headerClassMap = {
  航班相关: 'bg-yellow',
  运单相关: 'bg-purple',
  货站相关: 'bg-green',
  地服相关: 'bg-cyan',
}

const computedWidth = (column: SimpleColumn) => {
  let width = 0
  column.columnLabel.split('\n').forEach(line => {
    const len = line.length
    let realLength = 0
    for (let i = 0; i < len; i++) {
      realLength += line.charCodeAt(i) > 255 ? 2 : 1
    }
    if (width < realLength * 8) {
      width = realLength * 8
    }
  })
  // 筛选/排序按钮宽度
  // if (column.needFilters || column.needSort) {
  //   width += 8
  // }
  width += 8
  // border
  return width + 1
}

export function useAirportTable(name: string, formData: CommonData) {
  const tableColumns = ref<CommonTableColumn[]>([])
  const tableData = ref<CommonData[]>([])
  const getTableColumns = () => {
    const groups = columnGroupsMap[name]
    tableColumns.value = groups.reduce(
      (columns: CommonTableColumn[], group) => {
        group.children.forEach(column => {
          columns.push({
            key: column.columnName,
            dataKey: column.columnName,
            title: column.columnLabel,
            columnDescribe: '',
            dataType: '',
            listqueryTemplateID: null,
            needCount: ['航班相关'].includes(group.groupName) ? 0 : 1,
            countMode: column.columnLabel.includes('/') ? 'split' : 'notNull',
            needFilters: null,
            needGroup: null,
            needSearch: null,
            needShow: 1,
            needSort: 1,
            orderNumber: null,
            queryTemplateColumnSetID: null,
            queryTemplateID: null,
            width: name.includes('Departure')
              ? computedWidth(column)
              : undefined,
            flexGrow: 1,
            align: 'center',
            labelClassName: headerClassMap[group.groupName] ?? '',
            groupName: group.groupName,
            ...column,
          })
        })
        return columns
      },
      []
    )
  }
  const getTableData = async () => {
    try {
      const {
        startDate,
        endDate,
        flightState,
        flightWarning,
        waybillType,
      } = formData
      const dataContent = [
        startDate,
        endDate,
        // flightState,
        // flightWarning
      ]
      // if (name.includes('International')) {
      //   dataContent.push(waybillType)
      // }
      const {
        code,
        returnData: { columnSet, listValues },
        message,
      } = await Query<CommonData>({
        id: DATACONTENT_ID[name.slice(0, 1).toLowerCase() + name.slice(1)],
        dataContent,
      })
      if (Number(code) !== 0) {
        throw new Error(message || '失败')
      }
      // tableColumns.value = columnSet.map(column => ({
      //   key: column.columnName,
      //   dataKey: column.columnName,
      //   title: column.columnLabel,
      //   width: column.columnLabel.length * 50,
      //   flexGrow: 1,
      //   align: 'center',
      //   ...column,
      // }))
      tableData.value = listValues.map(row => ({
        ...row,
        rowKey: `${row.IATACode}-${row.flightNO}-${row.flightDate}`,
      }))
    } catch (error) {
      console.error(error)
    }
  }
  onMounted(() => {
    getTableColumns()
  })

  return {
    tableColumns,
    tableData,
    getTableData,
  }
}
