import myClass from './Class.js'

const customRender = (self, type) => {
  return (text, data) => {
    if (self.options[type] && Array.isArray(self.options[type])) {
      let value = self.options[type].find((item) => item.value === text)
      return value ? value.label : text
    }
    return text
  }
}
export let consignee = new myClass({ name: '收货方', key: 'consignee', customRender })
export let shipper = new myClass({ name: '发货方', key: 'shipper', customRender })

export const setMainColumns = (self, expData = []) => {
  return [
    { dataIndex: 'shipbillId', title: 'ID', hidden: true, width: 100 },
    {
      dataIndex: 'shipbillNo',
      width: 200,
      sorter: (a, b) => {
        if (!a.shipbillNo || !b.shipbillNo) return
        return a.shipbillNo.replace(/[^0-9]/gi, '') - b.shipbillNo.replace(/[^0-9]/gi, '')
      },
      title: '运单号',
      customRender: (txt, record) => {
        return self._c(
          'a',
          {
            attrs: {
              href: 'javascript:void(0)',
            },
            on: {
              click: (event) => {
                event.stopPropagation()
                return self.$emit('view', record)
              },
            },
          },
          [self._v(txt)]
        )
      },
    },
    {
      width: 100,
      dataIndex: 'departmentName',
      title: '部门',
      sorter: (a, b) => {
        return a.departmentName == b.departmentName
      },
    },
    {

      dataIndex: 'statusName',
      title: '状态',
      width: 140,
      align: 'center',
      sorter: (a, b) => {
        return a.status - b.status
      },
    },
    { width: 100, dataIndex: 'status', title: '状态编码', hidden: true },
    {
      width: 100,
      dataIndex: 'cargoOwnerName',
      title: '货主',
      sorter: (a, b) => {
        return a.cargoOwnerName != b.cargoOwnerName
      },
    },
    {
      width: 200,
      dataIndex: 'orderNo',
      title: '订单号',
      sorter: (a, b) => {
        if (!a.orderNo || !b.orderNo) {
          return
        }
        return a.orderNo.replace(/[^0-9]/gi, '') - b.orderNo.replace(/[^0-9]/gi, '')
      },
    },
    {
      width: 200,
      dataIndex: 'wmsOrderNo',
      title: 'WMS单号',
      sorter: (a, b) => {
        return a.wmsOrderNo != b.wmsOrderNo
      },
    },
    {
      width: 100,
      dataIndex: 'orderType',
      title: '订单类型',
      customRender: customRender(self, 'ORDER_TYPE'),
      sorter: (a, b) => {
        return a.orderType - b.orderType
      },
    },
    {
      width: 100,
      dataIndex: 'orderBusinessType',
      title: '业务类型',
      customRender: customRender(self, 'ORDER_BUSINESS_TYPE'),
      sorter: (a, b) => {
        return a.orderBusinessType - b.orderBusinessType
      },
    },
    {
      width: 100,
      dataIndex: 'deliveryMethodType',
      title: '配送方式',
      align: 'center',
      customRender: customRender(self, 'DELIVERY_METHOD_TYPE'),
      sorter: (a, b) => {
        return a.deliveryMethodType - b.deliveryMethodType
      },
    },
    {
      width: 100,
      dataIndex: 'transportType',
      title: '运输模式',
      align: 'center',
      customRender: customRender(self, 'TRANSPORT_TYPE'),
      sorter: (a, b) => {
        return a.transportType - b.transportType
      },
    },
    {
      width: 100,
      dataIndex: 'temperatureRequirement',
      title: '温度要求',
      align: 'center',
      customRender: customRender(self, 'TEMPERATURE_REQUIREMENT'),
      sorter: (a, b) => {
        return a.temperatureRequirement - b.temperatureRequirement
      },
    },
    {
      width: 100,
      dataIndex: 'lineName',
      title: '线路',
      sorter: (a, b) => {
        return a.lineName == b.lineName
      },
    },
    {

      dataIndex: 'memo',
      title: '备注',
      width: 120,
      sorter: (a, b) => {
        return a.memo == b.memo
      },
    },
    ...expData,
    {

      dataIndex: 'orderDate',
      title: '订单下单日期',
      width: 120,
      sorter: (a, b) => {
        return +new Date(a.orderDate) - +new Date(b.orderDate)
      },
    },
    {

      dataIndex: 'estimatedPickupEndTime',
      title: '预计提货时间',
      width: 120,
      sorter: (a, b) => {
        return +new Date(a.estimatedPickupEndTime) - +new Date(b.estimatedPickupEndTime)
      },
    },
    {
      dataIndex: 'estimatedDeliveryEndTime',
      title: '预计送达时间',
      width: 120,
      sorter: (a, b) => {
        return +new Date(a.estimatedDeliveryEndTime) - +new Date(b.estimatedDeliveryEndTime)
      },
    },
    {
      dataIndex: 'transportAging',
      title: '时效',
      width: 80,
      sorter: (a, b) => {
        return a.transportAging - b.transportAging
      },
    },
    {
      dataIndex: 'lineDirectionName',
      title: '方向',
      width: 80,
      sorter: (a, b) => {
        return a.lineDirection - b.lineDirection
      },
    },
    ...consignee.init(self), //收

    ...shipper.init(self), //发
    {
      dataIndex: 'totalPcs',
      title: '总件数',
      width: 120,
      sorter: (a, b) => {
        return a.totalPcs - b.totalPcs
      },
    },
    {
      dataIndex: 'totalVolume',
      title: '总体积',
      width: 120,
      sorter: (a, b) => {
        return a.totalVolume - b.totalVolume
      },
    },
    {
      dataIndex: 'totalWeight',
      title: '总重量',
      width: 120,
      sorter: (a, b) => {
        return a.totalWeight - b.totalWeight
      },
    },

    {
      dataIndex: 'createperson',
      title: '创建人',
      width: 100,
      sorter: (a, b) => {
        return a.createperson.length - b.createperson.length
      },
    },
    {
      dataIndex: 'createtime',
      title: '创建时间',
      width: 150,
      sorter: (a, b) => {
        return +new Date(a.createtime) - +new Date(b.createtime)
      },
    },
    {
      dataIndex: 'updateperson',
      title: '修改人',
      width: 100,
      sorter: (a, b) => {
        return a.updateperson.length - b.updateperson.length
      },
    },
    {
      dataIndex: 'updatetime',
      title: '修改时间',
      width: 150,
      sorter: (a, b) => {
        return +new Date(a.updatetime) - +new Date(b.updatetime)
      },
    },

    {
      key: 'operation',
      title: '操作',
      fixed: 'right',
      align: 'center',
      width: 250,
      scopedSlots: { customRender: 'operation' },
    },
  ]
}
