import myClass from '@/blocks/components/scheduling/js/table/Class.js'
import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE, UPCLOUD_RDC_TMS_BUSINESS_SERVICE } from '@/config/serviceSettings.js'

function disabledTime(name) {
  return function (endValue, form) {
    let startValue = form.getFieldValue(name)
    if (!startValue || !endValue) {
      return false
    }
    return new Date(startValue).valueOf() >= endValue.valueOf()
  }
}

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
  }
}
let consignee = new myClass({ name: '收货方', key: 'consignee', customRender })
let shipper = new myClass({ name: '发货方', key: 'shipper', customRender })

export const setQueryFormItmes = (self) => {
  return {
    // 运单
    waybillAttr: {
      modalWidth: 1000,
      label: '运单',
      placeholder: '请选择运单',
      targetClick: true,
      targetValueKey: 'shipbillId',
      remoteUrl: { url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/booking/shipbill_pop', method: 'post' },
      rowKey: 'shipbillId',
      remoteBefore: (vNode, remoteUrl) => {
        remoteUrl.data = { appointmentId: self.dialogForm.appointmentId, ...remoteUrl.data }
      },
      toolBar: {
        labelCol: { span: 8 },
        wrapperCol: { span: 16 },
        items: [
          { type: 'input', name: 'cargoOwnerName', label: '货主' },
          { type: 'input', name: 'orderNo', label: '订单号' },
          { type: 'input', name: 'shipbillNo', label: '运单号' },
          {
            name: 'orderBusinessType',
            label: '业务类型',
            type: 'select',
            get options() {
              return self.options.ORDER_BUSINESS_TYPE
            },
          },
          { type: 'input', name: 'shipperName', label: '发货方' },
          { type: 'input', name: 'consigneeName', label: '收货方' },
          {
            name: 'orderDateStart',
            disabledDate: disabledTime('orderDateEnd'),
            label: '下单开始时间',
            type: 'date',
            showTime: true,
            // format: 'YYYY-MM-DD',
            placeholder: '年/月/日',
            on: {
              openChange(open, form) {
                if (!open) {
                  form.setItemProps('orderDateEnd', { open: true })
                }
              },
            },
          },
          {
            name: 'orderDateEnd',
            label: '下单结束时间',
            disabledDate: disabledTime('orderDateStart'),
            type: 'date',
            showTime: true,
            // format: 'YYYY-MM-DD',
            placeholder: '年/月/日',
            on: {
              openChange(open, form) {
                form.setItemProps('orderDateEnd', { open })
              },
            },
          },
        ],
      },
      tableConfig: {
        rowSelectType: 2,
        scroll: { x: true },
        columns: [
          { width: 120, dataIndex: 'shipbillNo', title: '运单号' },
          { width: 120, dataIndex: 'cargoOwnerName', title: '货主' },
          { width: 120, dataIndex: 'orderNo', title: '订单号' },
          { width: 120, dataIndex: 'wmsOrderNo', title: 'WMS单号' },
          { width: 120, dataIndex: 'orderTypeName', title: '订单类型' },
          { width: 120, dataIndex: 'orderBusinessTypeName', title: '业务类型' },
          { width: 120, dataIndex: 'deliveryMethodTypeName', title: '配送方式' },
          { width: 120, dataIndex: 'transportTypeName', title: '运输模式' },
          { width: 120, dataIndex: 'temperatureRequirementName', title: '温度要求' },
          { width: 120, dataIndex: 'lineName', title: '线路' },
          { width: 120, dataIndex: 'orderDate', title: '订单下单日期' },
          { width: 120, dataIndex: 'estimatedPickupStartTime', title: '预计提货时间' },
          { width: 120, dataIndex: 'estimatedDeliveryStartTime', title: '预计送达时间' },
          { width: 120, dataIndex: 'transportAging', title: '时效' },
          { width: 120, dataIndex: 'shipperTypeName', title: '发货方类型' },
          { width: 120, dataIndex: 'shipperUnitName', title: '发货方单位' },
          { width: 120, dataIndex: 'shipperName', title: '发货方' },
          { width: 120, dataIndex: 'shipperProvinceCityDistrict', title: '始发地' },
          { width: 120, dataIndex: 'shipperContact', title: '联系人' },
          { width: 120, dataIndex: 'shipperPhoneNumber', title: '手机号' },
          { width: 120, dataIndex: 'shipperLandlineNumber', title: '座机号' },
          { width: 120, dataIndex: 'shipperEmail', title: '邮箱' },
          { width: 120, dataIndex: 'shipperAddress', title: '详细地址' },
          { width: 120, dataIndex: 'shipperMemo', title: '备注' },
          { width: 120, dataIndex: 'shipperPositioning', title: '发货方位置' },
          { width: 120, dataIndex: 'consigneeTypeName', title: '收货方类型' },
          { width: 120, dataIndex: 'consigneeUnitName', title: '收货方单位' },
          { width: 120, dataIndex: 'consigneeName', title: '收货方' },
          { width: 120, dataIndex: 'consigneeProvinceCityDistrict', title: '目的地' },
          { width: 120, dataIndex: 'consigneeContact', title: '联系人' },
          { width: 120, dataIndex: 'consigneePhoneNumber', title: '手机号' },
          { width: 120, dataIndex: 'consigneeLandlineNumber', title: '座机号' },
          { width: 120, dataIndex: 'consigneeEmail', title: '邮箱' },
          { width: 120, dataIndex: 'consigneeAddress', title: '详细地址' },
          { width: 120, dataIndex: 'consigneeMemo', title: '备注' },
          { width: 120, dataIndex: 'consigneePositioning', title: '收货方位置' },
          { width: 120, dataIndex: 'totalPcs', title: '总件数' },
          { width: 120, dataIndex: 'totalWeight', title: '总重量' },
          { width: 120, dataIndex: 'totalVolume', title: '总体积' },
          { width: 120, dataIndex: 'packageWholePiece', title: '整件数' },
          { width: 120, dataIndex: 'packageLclPiece', title: '拼箱数' },
          { width: 120, dataIndex: 'packageTotalWeight', title: '包裹总重量' },
          { width: 120, dataIndex: 'packageTotalVolume', title: '包裹总体积' },
        ],
      },
    },
  }
}

// 运单
export const setDialogWaybillColumns = (self, expData = []) => {
  return [
    {
      dataIndex: 'shipbillNo',
      title: '运单号',
      width: 120,
      sorter: (a, b) => {
        if (!a.shipbillNo || !b.shipbillNo) return
        return a.shipbillNo.replace(/[^0-9]/gi, '') - b.shipbillNo.replace(/[^0-9]/gi, '')
      },
    },
    {
      dataIndex: 'cargoOwnerName',
      title: '货主',
      width: 120,
      sorter: (a, b) => {
        return a.cargoOwnerName != b.cargoOwnerName
      },
    },
    {
      dataIndex: 'orderNo',
      title: '订单号',
      width: 120,
      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: 120,
      sorter: (a, b) => {
        return a.wmsOrderNo != b.wmsOrderNo
      },
    },
    {
      dataIndex: 'orderTypeName',
      title: '订单类型',
      width: 120,
      sorter: (a, b) => {
        return a.orderType - b.orderType
      },
    },
    {
      dataIndex: 'orderBusinessTypeName',
      title: '业务类型',
      width: 120,
      sorter: (a, b) => {
        return a.orderBusinessType - b.orderBusinessType
      },
    },
    {
      dataIndex: 'deliveryMethodTypeName',
      title: '配送方式',
      width: 120,
      sorter: (a, b) => {
        return a.deliveryMethodType - b.deliveryMethodType
      },
    },
    {
      dataIndex: 'transportTypeName',
      title: '运输模式',
      width: 120,
      sorter: (a, b) => {
        return a.transportType - b.transportType
      },
    },
    {
      dataIndex: 'temperatureRequirementName',
      title: '温度要求',
      width: 120,
      sorter: (a, b) => {
        return a.temperatureRequirement - b.temperatureRequirement
      },
    },
    {
      dataIndex: 'lineName',
      title: '线路',
      width: 120,
      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: 'estimatedPickupStartTime',
      title: '预计提货时间',
      width: 120,
      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: 120,
      sorter: (a, b) => {
        return a.transportAging - b.transportAging
      },
    },
    ...shipper.init(self), //发
    ...consignee.init(self), //收
    {
      dataIndex: 'totalPcs',
      title: '总件数',
      width: 120,
      sorter: (a, b) => {
        return a.totalPcs - b.totalPcs
      },
    },
    {
      dataIndex: 'totalWeight',
      title: '总重量',
      width: 120,
      sorter: (a, b) => {
        return a.totalWeight - b.totalWeight
      },
    },
    {
      dataIndex: 'totalVolume',
      title: '总体积',
      width: 120,
      sorter: (a, b) => {
        return a.totalVolume - b.totalVolume
      },
    },
    {
      dataIndex: 'packageWholePiece',
      title: '整件数',
      width: 120,
      sorter: (a, b) => {
        return a.packageWholePiece - b.packageWholePiece
      },
    },
    {
      dataIndex: 'packageLclPiece',
      title: '拼箱数',
      width: 120,
      sorter: (a, b) => {
        return a.packageLclPiece - b.packageLclPiece
      },
    },
    {
      dataIndex: 'packageTotalWeight',
      title: '包裹总重量',
      width: 120,
      sorter: (a, b) => {
        return a.packageTotalWeight - b.packageTotalWeight
      },
    },
    {
      dataIndex: 'packageTotalVolume',
      title: '包裹总体积',
      width: 120,
      sorter: (a, b) => {
        return a.packageTotalVolume - b.packageTotalVolume
      },
    },
  ]
}
