import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE } from '@/config/serviceSettings.js'

export const setUpdateForm = (self, title) => {
  let PICK_UP_CHECK_DETAIL_SWITCH = self.PICK_UP_CHECK_DETAIL_SWITCH;
  let checkDetails = []

  if (PICK_UP_CHECK_DETAIL_SWITCH == 1) {

    checkDetails = [{
      span: 24,
      colon: false,
      name: 'checkDetails',
      label: '\n',
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      type: 'check',
      options: self.options.PICK_UP_CHECK_DETAIL?.map(({ label }) => {
        return { value:label, label }
      })
    }]
  }
  let 启运确认 = [
    { name: 'shipbillIds', hidden: true },
    {
      name: 'departureSignTime',
      label: '签到时间',
      type: 'date',
      format: 'YYYY-MM-DD HH:mm',
      showTime: true,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      value: self.moment().format('YYYY-MM-DD HH:mm'),
    },
    {
      name: 'departureTime',
      label: '启运时间',
      type: 'date',
      format: 'YYYY-MM-DD HH:mm',
      required: true,
      showTime: true,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      value: self.moment().format('YYYY-MM-DD HH:mm'),
    },

    {
      name: 'vehicleDeviceName',
      label: '设备号',
      placeholder: '请选择设备号',
      type: 'searchModal',
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      targetClick: true,
      targetValueKey: 'name',
      remoteUrl: { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE + '/device/pop', method: 'post' },
      rowKey: 'id',
      hidden: true,
      tableConfig: {
        rowSelectType: 2,
        scroll: { x: true },
        columns: [
          { dataIndex: 'code', title: '设备编码' },
          { dataIndex: 'name', title: '设备名称' },
        ]
      },
      on: {
        change(event, value, dataArr, form) {
          if (!value) {
            return form.setFieldsValue({
              vehicleDeviceId: '',
              vehicleDeviceCode: '',
            })
          }
          if (dataArr) {
            let vehicleDeviceId = [], vehicleDeviceCode = [];
            dataArr.forEach(item => {
              vehicleDeviceId.push(item.id)
              vehicleDeviceCode.push(item.code)
            })
            form.setFieldsValue({
              vehicleDeviceId: vehicleDeviceId.join(','),
              vehicleDeviceCode: vehicleDeviceCode.join(',')
            })
          }

        },
      },
    },
    {
      name: 'vehicleDeviceId',
      label: '设备号ID',
      hidden: true,
    },
    {
      name: 'vehicleDeviceCode',
      label: '设备号code',
      hidden: true,
    },
    {
      name: 'containerName',
      label: '容器号',
      placeholder: '请选择容器号',
      type: 'searchModal',
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      targetClick: true,
      targetValueKey: 'name',
      remoteUrl: { url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE + '/container/pop', method: 'post' },
      rowKey: 'id',
      get hidden() {

        //系统参数
        return !(self.RELATION_THIRD_SYSTEM.includes('3'))
      },
      tableConfig: {
        rowSelectType: 2,
        scroll: { x: true },
        columns: [
          { dataIndex: 'code', title: '容器编码' },
          { dataIndex: 'name', title: '容器名称' },
          { dataIndex: 'typeName', title: '类型' }
        ]
      },

      on: {
        change(event, value, dataArr, form) {
          if (!value) {
            self.currentData.temperatureVOList = []
            return form.setFieldsValue({
              containerId: '',
              containerCode: '',
              containerName: '',
              temperatureVOList:[]
            })
          }
          if (dataArr) {

            let containerId = [], containerCode = [], containerName = [], temperatureVOList = []
            dataArr.forEach(item => {
              containerId.push(item.id)
              containerCode.push(item.code)
              containerName.push(item.name)

              if (self.currentData.showDepartureTemperature && item.typeName == '保温箱') {
                temperatureVOList.push({
                  containerId: item.id,
                  containerCode: item.code,
                  containerName: item.name,
                  temperature: form.getFieldValue('departureTemperature')
                })
              }
            })
            let o = {
              containerName: containerName.join(','),
              containerId: containerId.join(','),
              containerCode: containerCode.join(',')
            }
           o.temperatureVOList=temperatureVOList
            form.setFieldsValue(o);
            self.currentData.temperatureVOList = temperatureVOList
            // self.currentData={...self.currentData,...o,temperatureVOList}



          }

        },
      },
    },
    {
      name: 'containerId',
      label: '容器号ID',
      hidden: true,
    },
    {
      name: 'containerCode',
      label: '容器号ID',
      hidden: true,
    },

    {
      name: 'departureTemperature',
      label: '所有容器-启运温度',
      type: 'number',
      unit: ' °c',
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      get hidden() {
        //业务规则策略
        return !self.currentData.showDepartureTemperature || !self.currentData.temperatureVOList.length
      },
      on: {
        change(value, form) {
          console.log(value)
          console.log(self.currentData)
          self.currentData?.temperatureVOList.forEach(item => {
            item.temperature = value

          })
        }
      }
    },
    {
      itemHeight: 'auto',
      span: 24,
      labelCol: { span: 0 },
      wrapperCol: { span: 24 },
      scopedSlots: { customRender: 'temperatureVOList' },
    },
    {
      name: 'temperatureVOList',
      hidden: true,
      type: 'txt',
      value: [],
    },
    {
      name: 'showDepartureTemperature',
      label: '是否显示启运温度',
      hidden: true,
      type: 'txt',

    },

    {
      name: 'departureMemo',
      span: 24,
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      value: '',
      label: '备注',
      type: 'textarea',
      help() {
        return self._c('a-alert', {
          style: {
            marginBottom: '10px'
          },
          attrs: {
            message: '货物图片上传需要备注描述，货物需要上传三向图片！',
            banner: true
          }
        })
      }
    },
    ...checkDetails,
    {
      name: 'departurePhoto',
      itemHeight: 'auto',
      label: '货物照片',
      span: 24,
      // required: true,
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      scopedSlots: { customRender: 'photo' },
      value: '',
    },

  ]

  return eval(title)
}

export const setUpdateGrid = (self, title) => {
  return [
    {
      dataIndex: 'itemCode',
      title: '商品编码',
      width: 120,
    },
    {
      dataIndex: 'itemName',
      title: '商品名称',
      width: 120,
    },
    {
      dataIndex: 'itemCategoryName',
      title: '商品品类',
      width: 120,
    },
    {
      dataIndex: 'packTypeName',
      title: '包装类型',
      width: 120,
    },
    {
      dataIndex: 'cargoThermosphereName',
      title: '货物温层',
      width: 120,
    },
    {
      dataIndex: 'pcs',
      title: '件数',
      width: 120,
    },
    {
      dataIndex: 'editPcs',
      title: title,
      width: 120,
      scopedSlots: { customRender: 'editPcs' },
    },
    {
      dataIndex: 'weight',
      title: '重量',
      width: 120,
    },
    {
      dataIndex: 'volume',
      title: '体积',
      width: 120,
    },
    {
      dataIndex: 'memo',
      title: '备注',
      width: 120,
    },
    {
      dataIndex: 'temperatureCtlUpperLimit',
      title: '温控区间（下限）',
      width: 120,
    },
    {
      dataIndex: 'temperatureCtlLowerLimit',
      title: '温控区间（上限）',
      width: 120,
    },
    {
      dataIndex: 'branchName',
      title: '默认存放仓库',
      width: 120,
    },
    {
      dataIndex: 'inventoryPcs',
      title: '库存件数',
      width: 120,
    },
    {
      dataIndex: 'stagingLocation',
      title: '暂存位',
      width: 120,
    },
  ]
}
