import myClass from '../$common/mainTable/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 const setMainColumns = (self, expData = []) => {


  let consignee = new myClass({ name: '收货方', key: 'consignee', customRender })
  let shipper = new myClass({ name: '发货方', key: 'shipper', customRender })
  let operation = []
  if (self.operation) {
    operation = [{
      key: 'operation',
      title: '操作',
      fixed: 'right',
      align: 'center',
      scopedSlots: { customRender: 'operation' },
      ...self.operation,
    }]
  }
  return [
    { dataIndex: 'id', title: 'ID', hidden: true, width: 200 },
    { dataIndex: 'shipbillId', title: '运单Id', hidden: true, width: 200 },
    {
      dataIndex: 'shipbillNo',
      title: '运单号',
      width: 200,
      sorter: (a, b) => {
        if (!b.shipbillNo || !b.shipbillNo) return
        return a.shipbillNo.replace(/[^0-9]/gi, '') - b.shipbillNo.replace(/[^0-9]/gi, '')
      },
      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 ? txt : '')]
        )
      },
    },
    {
      dataIndex: 'cargoOwnerName',
      title: '货主',
      width: 150,
      sorter: (a, b) => {
        return a.cargoOwnerName != b.cargoOwnerName
      },
    },

    {
      dataIndex: 'orderNo',
      title: '订单号',
      width: 200,

      sorter: (a, b) => {
        if (!a.orderNo || !b.orderNo) {
          return
        }
        return a.orderNo.replace(/[^0-9]/gi, '') - b.orderNo.replace(/[^0-9]/gi, '')
      },
    },
    {
      dataIndex: 'wmsOrderNo',
      title: 'WMS单号',
      width: 200,

      sorter: (a, b) => {
        if (!a.wmsOrderNo || !a.wmsOrderNo) return
        return a.wmsOrderNo.replace(/[^0-9]/gi, '') - b.wmsOrderNo.replace(/[^0-9]/gi, '')
      },
    },
    {
      dataIndex: 'departmentName',
      title: '所属部门',
      width: 100,

      sorter: (a, b) => {
        return a.departmentName != b.departmentName
      },
    },

    {
      width: 80,

      dataIndex: 'statusName',
      title: '运单状态',
      align: 'center',
      sorter: (a, b) => {
        return a.status - b.status
      },
    },
    {
      dataIndex: 'transportStatusName',
      title: '运输状态',
      width: 80,

      align: 'center',
      sorter: (a, b) => {
        return a.transportStatus - b.transportStatus
      },
    },
    {
      width: 80,

      dataIndex: 'abnormalStatusName',
      title: '异常状态',
      align: 'center',
      sorter: (a, b) => {
        return a.abnormalStatus - b.abnormalStatus
      },
    },
    {
      width: 80,

      dataIndex: 'scanStatusName',
      title: '扫描状态',
      align: 'center',
      sorter: (a, b) => {
        return a.scanStatus - b.scanStatus
      },
    },
    {
      width: 120,
      dataIndex: 'carrierTypeName',
      title: '承运方类型',
      align: 'center',
      sorter: (a, b) => {
        return a.carrierType - b.carrierType
      },
    },


    {
      width: 100,
      dataIndex: 'loadingBillNo',
      title: '调度单号',
      align: 'center',
      sorter: (a, b) => {
        if (!a.loadingBillNo || !a.loadingBillNo) return
        return a.loadingBillNo.replace(/[^0-9]/gi, '') - b.loadingBillNo.replace(/[^0-9]/gi, '')
      },
    },

    {
      width: 160,
      dataIndex: 'affiliationName',
      title: '承运商',
      align: 'center',
      sorter: (a, b) => {
        return a.carrier - b.carrier
      },
    },
    {
      width: 160,
      dataIndex: 'carrierName',
      title: '承运方',
      align: 'center',
      sorter: (a, b) => {
        return a.carrier - b.carrier
      },
    },
    {
      width: 150,
      dataIndex: 'driverPhoneNumber',
      title: '承运方手机号',
      align: 'center',
      sorter: (a, b) => {
        return a.driverPhoneNumber - b.driverPhoneNumber
      },
    },
    {
      width: 150,

      dataIndex: 'deliverymanName',
      title: '送货员姓名',
      align: 'center',
      sorter: (a, b) => {
        return a.deliverymanName != b.deliverymanName
      },
    },
    {
      width: 150,

      dataIndex: 'deliverymanPhoneNumber',
      title: '送货员手机号',
      align: 'center',
      sorter: (a, b) => {
        return a.deliverymanName != b.deliverymanName
      },
    },
    {
      width: 100,
      dataIndex: 'vehiclePlateNumber',
      title: '车牌号',
      align: 'center',
      sorter: (a, b) => {
        return a.vehiclePlateNumber != b.vehiclePlateNumber
      },
    },
    {
      width: 100,
      dataIndex: 'orderType',
      title: '订单类型',
      customRender: customRender(self, 'ORDER_TYPE'),
      sorter: (a, b) => {
        return a.orderType - b.orderType
      },
    },
    {
      dataIndex: 'orderBusinessType',
      title: '业务类型',
      customRender: customRender(self, 'ORDER_BUSINESS_TYPE'),
      sorter: (a, b) => {
        return a.orderBusinessType - b.orderBusinessType
      },
    },
    {
      dataIndex: 'deliveryMethodType',
      title: '配送方式',
      align: 'center',
      width: 150,
      customRender: customRender(self, 'DELIVERY_METHOD_TYPE'),
      sorter: (a, b) => {
        return a.deliveryMethodType - b.deliveryMethodType
      },
    },
    {
      dataIndex: 'transportTypeName',
      title: '运输模式',
      width: 150,
      sorter: (a, b) => {
        return a.transportType - b.transportType
      },
    },
    {
      dataIndex: 'temperatureRequirement',
      title: '温度要求',
      width: 100,
      customRender: customRender(self, 'TEMPERATURE_REQUIREMENT'),
      sorter: (a, b) => {
        return a.temperatureRequirement - b.temperatureRequirement
      },
    },
    {
      dataIndex: 'lineName',
      title: '线路',
      width: 100,
      sorter: (a, b) => {
        return a.temperatureRequirement != b.temperatureRequirement
      },
    },
    {
      dataIndex: 'memo',
      title: '备注',
      width: 120,
      sorter: (a, b) => {
        return a.temperatureRequirement != b.temperatureRequirement
      },
    },
    ...expData,

    {
      dataIndex: 'orderDate',
      title: '订单下单日期',
      width: 120,

      sorter: (a, b) => {
        return +new Date(a.orderDate) - +new Date(b.orderDate)
      },
    },
    {
      dataIndex: 'estimatedPickupStartTime',
      title: '预计提货时间',
      width: 120,
      customRender(text, data) {
        if (!text) {
          return data.estimatedPickupEndTime
        }
        return text
      },
      sorter: (a, b) => {
        return +new Date(a.estimatedPickupStartTime) - +new Date(b.estimatedPickupStartTime)
      },
    },
    {
      dataIndex: 'estimatedDeliveryStartTime',
      title: '预计送达时间',
      width: 120,
      sorter: (a, b) => {
        return +new Date(a.estimatedDeliveryStartTime) - +new Date(b.estimatedDeliveryStartTime)
      },
    },
    {
      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: 'totalDeliveryPcs',
      title: '总发货件数',
      width: 120,
      align: 'center',
      sorter: (a, b) => {
        return a.totalDeliveryPcs - b.totalDeliveryPcs
      },
    },
    {
      dataIndex: 'totalDeliveryVolume',
      title: '总发货体积',
      width: 120,
      align: 'center',
      sorter: (a, b) => {
        return a.totalDeliveryVolume - b.totalDeliveryVolume
      },
    },
    {
      dataIndex: 'totalDeliveryWeight',
      title: '总发货重量',
      align: 'center',
      width: 120,
      sorter: (a, b) => {
        return a.totalDeliveryWeight - b.totalDeliveryWeight
      },
    },
    {
      dataIndex: 'totalPcs',
      title: '总剩余件数',
      align: 'center',
      width: 120,
      sorter: (a, b) => {
        return a.totalPcs - b.totalPcs
      },
    },
    {
      dataIndex: 'totalVolume',
      title: '总剩余体积',
      align: 'center',

      width: 120,
      sorter: (a, b) => {
        return a.totalVolume - b.totalVolume
      },
    },
    {
      dataIndex: 'totalWeight',
      title: '剩余总重量',
      align: 'center',

      width: 120,
      sorter: (a, b) => {
        return a.totalWeight - b.totalWeight
      },
    },
    {
      dataIndex: 'createperson',
      title: '创建人',
      width: 100,
      sorter: (a, b) => {
        return a.createperson != b.createperson
      },
    },
    {
      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 != b.updateperson
      },
    },
    {
      dataIndex: 'updatetime',
      title: '修改时间',
      width: 150,
      sorter: (a, b) => {
        return +new Date(a.updatetime) - +new Date(b.updatetime)
      },
    },
    ...operation

  ].filter(item => item)
}
