
import { carAttr, carrierAttr, carAutoCompleteMethod, consignorAttr, driverAutoCompleteMethod } from '@/blocks/components/searchModalConfig';

export default function (self) {


  let driver = driverAutoCompleteMethod(self,
    {
      subName: ['driverId', 'driverCode'],
      name: 'driverName',
      label: '司机姓名',
      required: true,
      disabled: false,
      remoteBefore(vode, res, form) {
        if (self.tabKey == '2') {
          res.data.affiliationId = form.getFieldValue('carrierId')
        }
      },
      on: {
        ok(event, value, dataArr, form) {
          if (dataArr.length > 0) {
            let _vehiclePlateNumber = [], _vehicleCode = [], _vehicleId = [], _driverPhoneNumber = []
            dataArr.forEach(item => {

              const { phoneNumber, vehiclePlateNumber, vehicleCode, vehicleId } = item
              _vehiclePlateNumber.push(vehiclePlateNumber)
              _vehicleCode.push(vehicleCode)
              _vehicleId.push(vehicleId)
              _driverPhoneNumber.push(phoneNumber)
            });
            form.setFieldsValue({
              driverPhoneNumber: _driverPhoneNumber.join(', '),
              vehiclePlateNumber: _vehiclePlateNumber.join(','),
              vehicleCode: _vehicleCode.join(','),
              vehicleId: _vehicleId.join(','),
            })
          }
        }
      }
    }
  );

  Object.defineProperties(driver, {
    disabled: {
      get() {
        return self.tabKey == '2'&&!self.carrierId
      }
    },

  });



  let car = carAutoCompleteMethod(self, {
    subName: ['vehicleId', 'vehicleCode'],
    ...carAttr,
    targetClick: false,
    name: 'vehiclePlateNumber',
    label: '车牌',
    disabled: false,
    required: true,
    validateTrigger: 'blur',
    rules: [
      {
        required: true,
        type: 'plateNumber',
      }
    ]
  })
  Object.defineProperties(car, {
    disabled: {
      get() {
        return self.tabKey == '2'&&!self.carrierId
      }
    },

  });
  let carrier = []
  if (self.tabKey == '2') {
    carrier = [{
      ...carrierAttr,
      label: '承运商',
      targetValueKey: 'name',
      subName: ['carrierId', 'carrierCode'],
      name: 'carrierName',
      type: 'searchModal',
      on: {
        change(event, value, dataArr, form) {
          let obj = { carrierId: '', carrierCode: '', driverPhoneNumber: '', driverId: '', driverCode: '', driverName: '', vehicleId: '', vehicleCode: '', vehiclePlateNumber: '' }
          let data = dataArr[0]
          if (data) {
            obj = { ...obj, carrierId: data.id, carrierCode: data.code }
            self.carrierId=data.id
          } else {
            self.carrierId=''
          }

          form.setFieldsValue(obj)
        },
      },
    }]

  }
  return [
    {
      name: 'carrierType',
      value: 2,
      hidden: true
    },
    ...carrier,
    driver,
    {
      get disabled() {
        return self.tabKey == '2'&&!self.carrierId
      },

      name: 'driverPhoneNumber',
      value: '',
      type: 'input',
      label: '手机号',
      required: true,
      rules: [
        {
          required: true,
          type: 'phone',
        },
      ],
      validateTrigger: 'blur',
      span: 12,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
    },
    car
  ]
}