import { driverAttr, deliveryAttr, deviceAttr_scanPage, carAttr, vehicleTypeAttr, carrierAttr, containerAttr } from '@/blocks/components/searchModalConfig/index'
import BigJs from "big.js";
import { getCarData } from "../../api.js"
let _driverAttr = _.cloneDeep(driverAttr)
let _deviceAttr = _.cloneDeep(deviceAttr_scanPage)
let _deliveryAttr = _.cloneDeep(deliveryAttr)
let _carrierAttr = _.cloneDeep(carrierAttr)
let _carAttr = _.cloneDeep(carAttr)
let _vehicleTypeAttr = _.cloneDeep(vehicleTypeAttr)
_driverAttr.tableConfig.rowSelectType = 2;
_deliveryAttr.tableConfig.rowSelectType = 2;
_deviceAttr.tableConfig.rowSelectType = 2;



// @ApiModelProperty(value = "分组名称")
// private String groupName;

// @ApiModelProperty(value = "承运方类型 数据字典LINE_CARRIER_TYPE:1.承运商  2.司机")
// private Integer carrierType;




// @ApiModelProperty(value = "运单数")
// private Integer shipbillNumber;

// @ApiModelProperty(value = "客户数")
// private Integer customerNumber;

// @ApiModelProperty(value = "总站点数")
// private Integer siteNumber;

// @ApiModelProperty(value = "总里程数")
// private BigDecimal totalMileage;
// @ApiModelProperty(value = "总件数")
// private BigDecimal totalPcs;


// @ApiModelProperty(value = "总体积")
// private BigDecimal totalVolume;

// @ApiModelProperty(value = "总重量")
// private BigDecimal totalWeight;

// @ApiModelProperty(value = "实际总件数")
// private BigDecimal actualTotalPcs;

// @ApiModelProperty(value = "实际总体积")
// private BigDecimal actualTotalVolume;


// @ApiModelProperty(value = "实际总重量")
// private BigDecimal actualTotalWeight;

// @ApiModelProperty(value = "总整件数")
// private BigDecimal totalWholePiece;

// @ApiModelProperty(value = "总拼箱数")
// private BigDecimal totalLclPiece;

// @ApiModelProperty(value = "配载体积装载率")
// private String volumeLoadingRate;

// @ApiModelProperty(value = "配载重量装载率")
// private String weightLoadingRate;

// @ApiModelProperty(value = "备注")
// private String memo;

// @ApiModelProperty(value = "线路ID")
// private Long lineId;

// @ApiModelProperty(value = "车辆类别数据字典VEHICLE_TYPE_CATEGORY：1冷藏车、2平顶车、3厢式货车")
// private String vehicleTypeCategory;



// @ApiModelProperty(value = "配载体积装载率")
// private String volumeLoadingRate;

// @ApiModelProperty(value = "配载重量装载率")
// private String weightLoadingRate;

/**将返回车辆数据返回带回 */
export function resultData_car(data, fieldsValue) {
  const {
    _d,
    hasVehicleTypeDriver,
    volume,
    weight,
    vehicleAscription,
    vehicleAscriptionName,
    vehicleTypeId,
    vehicleTypeName,//车型
    vehicleTypeCode,
    typeCategory: vehicleTypeCategory,//车辆类别
    typeCategoryName: vehicleTypeCategoryName,
    affiliationCode: vehicleAffiliationCode,
    affiliationId: vehicleAffiliationId,
    affiliationName: vehicleAffiliationName,
    plateNumber: vehiclePlateNumber,
    id: vehicleId,
    code:vehicleCode,
    deviceId: vehicleDeviceId,
    deviceCode: vehicleDeviceCode,
    deviceName: vehicleDeviceName,
    containerId,
    containerCode,
    containerName,
  } = data

  let volumeLoadingRate = volume ? parseFloat(BigJs(fieldsValue?.totalVolumeQty ?? 0).div(volume)).toFixed(2) : 0
  let weightLoadingRate = weight ? parseFloat(BigJs(fieldsValue?.totalWeightQty ?? 0).div(weight)).toFixed(2) : 0
       volumeLoadingRate = BigJs(volumeLoadingRate).times(100).toString() + '%';
       weightLoadingRate = BigJs(weightLoadingRate).times(100).toString() + '%';
  let volumeRate = '体积:' + volumeLoadingRate + ',重量:' + weightLoadingRate
  let obj = {
    volume,
    weight,
    vehicleTypeId,
    vehicleTypeName,
    vehicleTypeCode,
    vehicleTypeCategory,
    vehicleTypeCategoryName,
    vehicleDeviceId,
    vehicleDeviceCode,
    vehicleDeviceName,
    vehiclePlateNumber,
    weightLoadingRate,
    volumeLoadingRate,
    vehicleId,
    vehicleCode,
    vehicleAffiliationCode,
    vehicleAffiliationId,
    vehicleAffiliationName,
    containerId,
    containerCode,
    containerName,
    volumeRate,
    vehicleAscription,
    vehicleAscriptionName,

 }

 if (_d=='司机'&&hasVehicleTypeDriver !== '1') {
      // delete obj.vehicleTypeId
      // delete obj.vehicleTypeName
      // delete obj.vehicleTypeCode
 }
  return obj
}


export const setFormItem = self => {
  let form = self.getForm(self.orderActive);

  return [

    { name: 'groupItem', label: '系统分组', type: 'txt', hidden: true },
    { name: 'cargoOwnerQty', label: '总货主数', type: 'txt', value: 0 },
    { name: 'volumeLoadingRate', label: '配载体积装载率', type: 'txt', value: 0, hidden: true },
    { name: 'weightLoadingRate', label: '配载重量装载率', type: 'txt', value: 0, hidden: true },
    { name: 'shipbillQty', label: '总单数', type: 'txt', unit: '单', value: 0 },
    { name: 'coldChainShipbillQty', label: '冷链单数', type: 'txt', unit: '单', value: 0 },
    { name: 'urgentShipbillQty', label: '紧急单数', type: 'txt', unit: '单', value: 0 },
    { name: 'totalPickUpSiteQty', label: '总提货点数', type: 'txt', value: 0 },
    { name: 'totalDeliverySiteQty', label: '总送货点数', type: 'txt', value: 0 },
    { name: 'totalSitQty', label: '总站点数', type: 'txt', value: 0 },
    { name: 'totalPieceQty', label: '总件数', type: 'txt', value: 0 },
    { name: 'totalWeightQty', label: '总重量', type: 'txt', unit: 'KG', value: 0 },
    { name: 'totalVolumeQty', label: '总体积', type: 'txt', unit: 'm³', value: 0 },
    { name: 'totalMileage', label: '总里程', type: 'txt', unit: '公里', value: 0 },
    { name: 'totalMileageMap', label: '地图总里程', type: 'txt', unit: '公里', value: 0 },
    {
      name: 'deliveryLines',
      tooltip: {
        placement: 'bottomLeft',
      },
      labelCol: { span: 5 },
      wrapperCol: { span: 19 },
      label: '配送线路', type: 'txt', value: '', span: 12
    },

    { name: 'carrierTypeName', label: '承运方名称', type: 'txt', value: '', hidden: true },
    { name: 'consigneeQty', label: '客户数', type: 'txt', value: 0, hidden: true },
    { name: 'carrierSysFlag', label: '承运商系统标志', type: 'txt', hidden: true },

    {
      span: 12,
      name: 'carrierType',
      label: '承运方类型',
      allowClear: false,
      type: 'select',
      required: true,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      // get value() {
      //   return self.carrierType
      // },
      get options() {

        return self.options?.LINE_CARRIER_TYPE ?? []
      },
      // [
      //   { label: '承运商', value: 1 },
      //   { label: '司机', value: 2 },
      // ],
      on: {

        change: self.carrierTypChange,
      },
    },

    {
      name: 'carrierId',
      hidden: true,
      label: '承运方ID',
      value: '',
    },

    {
      ..._carrierAttr,
      name: 'carrierCode',
      value: '',
      hidden: true,
      label: '承运方编码',
      type: 'searchModal',
      span: 12,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      on: {
        change(event, value, dataArr, form) {
          if (!value) {
            // carrierCode:"[string] 承运方编码"
            // carrierId:"[integer] 承运方ID"
            // carrierName:"[string] 承运方名称"
            form.setFieldsValue({
              carrierId: '',
              carrierName: '',
              carrierSysFlag: ''
            })
          }
        },
        ok(event, value, dataArr, form) {
          console.log(dataArr)
          let data = dataArr[0]
          if (data) {
            const { name: carrierName, id: carrierId, carrierSysFlag } = data
            form.setFieldsValue({
              carrierName,
              carrierId,
              carrierSysFlag
            })
          }
        }

      }
    },
    {
      name: 'carrierName',
      label: '承运方名称',
      type: 'input',
      readOnly: true,
      hidden: true,
      value: '',
      span: 12,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },

    },
    {
      name: 'driverCode',
      label: '司机code',
      hidden: true,
      value: '',
    },
    {
      name: 'driverId',
      hidden: true,
      label: '司机id',
      value: '',
    },
    {
      name: 'deliverymanCode',
      label: '送货员编号',
      hidden: true,
      value: '',
    },
    {
      name: 'deliverymanId',
      hidden: true,
      label: '送货员ID',
      value: '',
    },
    {
      name: 'vehicleTypeId',
      label: '车型Id',
      hidden: true,
      value: '',
    },
    {
      name: 'vehicleTypeCode',
      hidden: true,
      label: '车型编码',
      value: '',
    },
    {
      hidden: true,
      ..._driverAttr,
      name: 'driverName',
      targetClick: false,
      //required: true,
      label: '司机',
      type: 'searchModal',
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      span: 12,
      value: '',
      remoteBefore(vode, res, form) {

        res.data.vehicleTypeId=form.getFieldValue('defaultVehicleTypeId')
      },
      on: {
        input(event, form) {
          form.setFieldsValue({ driverId: '', driverCode: '' })
        },
        change(event, value, dataArr, form) {
          if (!value) {
            form.setFieldsValue({
              driverId: '',
              driverCode: '',
              driverPhoneNumber: '',
            })
          }
        },
        ok(event, value, dataArr, form) {
          if (dataArr.length > 0) {
            let driverId = [], driverCode = [], driverPhoneNumber = []
            let obj = null

            dataArr.forEach(item => {
              if (dataArr.length === 1 && item.vehiclePlateNumber) {

                //查询车辆数据
                getCarData(self, item.vehiclePlateNumber).then(p => {
                  if (p) {
                    p._d = '司机';
                    p.hasVehicleTypeDriver = item.hasVehicleTypeDriver
                    p.vehicleTypeId = item.vehicleTypeId
                    p.vehicleTypeName = item.vehicleTypeName
                    p.vehicleTypeCode = item.vehicleTypeCode
                    //车型
                    form.setFieldsValue(resultData_car(p, form.getFieldsValue()))
                    self.$forceUpdate()
                  }
                })
                // obj=resultData_car(item)
              }
              const { id, code, phoneNumber } = item
              driverId.push(id)
              driverCode.push(code)
              driverPhoneNumber.push(phoneNumber)
            });
            form.setFieldsValue({
              ...obj,
              driverId: driverId.join(','),
              driverCode: driverCode.join(','),
              driverPhoneNumber: driverPhoneNumber.join(','),
            })
          }
        }
      }
    },
    {
      name: 'defaultVehicleTypeId',
      hidden: true,
      type:'input',
    },
    {
      hidden: true,
      name: 'driverPhoneNumber',
      value: '',
      label: '司机手机号',
      // rules: [
      //   {
      //     type: 'phone',
      //     required: true,
      //   },
      // ],
      validateTrigger: 'blur',
      span: 12,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
    },
    {
      hidden: true,
      ..._deliveryAttr,
      name: 'deliverymanName',
      targetClick: false,
      label: '送货员',
      type: 'searchModal',
      //  rules: [
      //   {
      //     type: 'phone',
      //   },
      // ],
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      span: 12,
      value: '',
      on: {
        input(event, form) {
          form.setFieldsValue({ deliverymanId: '', deliverymanCode: '' })
        },
        change(event, value, dataArr, form) {
          if (!value) {
            form.setFieldsValue({
              deliverymanId: '',
              deliverymanCode: '',
              deliverymanPhoneNumber: '',
            })
          }
        },
        ok(event, value, dataArr, form) {
          if (dataArr.length > 0) {
            let deliverymanId = [], deliverymanCode = [], deliverymanPhoneNumber = []
            dataArr.forEach(item => {
              const { id, code, phoneNumber } = item
              deliverymanId.push(id)
              deliverymanCode.push(code)
              deliverymanPhoneNumber.push(phoneNumber)
            });
            form.setFieldsValue({
              deliverymanId: deliverymanId.join(','),
              deliverymanCode: deliverymanCode.join(','),
              deliverymanPhoneNumber: deliverymanPhoneNumber.join(','),
            })
          }
        }
      }
    },

    {
      hidden: true,
      name: 'deliverymanPhoneNumber',
      label: '送货员手机号',
      validateTrigger: 'blur',
      value: '',
      span: 12,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
    },
    {
      name: 'vehicleCode',
      hidden: true,
      label: '车辆code',
      value: '',
    },
    {
      name: 'vehicleId',
      hidden: true,
      label: '车辆Id',
      value: '',
    },
    {
      hidden: true,
      ..._carAttr,
      name: 'vehiclePlateNumber',
      targetClick: false,
      remoteBefore(vode, res, form) {
        res.data.vehicleTypeId=form.getFieldValue('defaultVehicleTypeId')
        res.data.driverIds = form.getFieldValue('driverId')?.split(',')??''
      },
      label: '车辆',
      type: 'searchModal',
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      span: 12,
      value: '',
      on: {
        input(event, form) {
          self.selfCar = false
          form.setFieldsValue({ vehicleId: '',vehicleCode:'' })
        },
        change(event, value, dataArr, form) {
          if (!value) {
            form.setFieldsValue({
              vehicleAscription: '',
              vehicleAscriptionName: '',
              vehicleTypeId: '',
              vehicleTypeName: '',
              vehicleTypeCategoryName: '',
              vehicleTypeCategory: 0,
              vehiclePlateNumber: '',
              vehicleId: '',
              vehicleCode: '',
              vehicleAffiliationCode: '',
              vehicleAffiliationId: '',
              vehicleAffiliationName: '',
              weightLoadingRate: 0,
              volumeLoadingRate: 0,
              volumeRate: '',
              vehicleDeviceId: '',
              vehicleDeviceCode: '',
              vehicleDeviceName: '',
              containerId: '',
              containerCode: '',
              containerName: '',
            })
            self.$forceUpdate()
          }
        },
        ok(event, value, dataArr, form) {
          console.log(dataArr)
          let data = dataArr[0]

          if (data) {
            self.selfCar = true

            // form.setItemProps('vehicleDeviceName', { disabled: false })
            //'typeName 车型' 'typeCategoryName 车辆类别'
            form.setFieldsValue(resultData_car(data, form.getFieldsValue()))
            self.$forceUpdate()
          }
        }
      }
    },
    {
      hidden: true,
      ..._vehicleTypeAttr,
      name: 'vehicleTypeName',
      label: '车型',
      value: '',
      //validateTrigger: 'blur',
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      span: 12,
      on: {
        input(event, form) {

          form.setFieldsValue({ vehicleTypeId: '', vehicleTypeCode: '' })
        },
        change(event, value, dataArr, form) {
          if (!value) {
            form.setFieldsValue({
              vehicleTypeId: '',
              vehicleTypeCode: '',
              vehicleTypeCategory: 0,
              vehicleTypeCategoryName: '',
              weightLoadingRate: 0,
              volumeLoadingRate: 0,
              volumeRate: '',
              defaultVehicleTypeId:''
            })
          }
        },
        ok(event, value, dataArr, form) {
          let BigJs = self.BigJs;
          if (dataArr.length > 0) {
            let vehicleTypeId = [], vehicleTypeCode = [], vehicleTypeCategory = [], vehicleTypeCategoryName = [];
            dataArr.forEach(item => {
              const { id, code, typeName, volume, weight, type } = item
              vehicleTypeId.push(id)
              vehicleTypeCode.push(code)
              vehicleTypeCategory.push(type)
              vehicleTypeCategoryName.push(typeName)
            });

            const { volumeRate, weightLoadingRate, volumeLoadingRate } = resultData_car(dataArr[0], form.getFieldsValue())

            //res.data.vehicleTypeId=form.getFieldValue('defaultVehicleTypeId')

            form.setFieldsValue({
              defaultVehicleTypeId:vehicleTypeId.join(','),
              volumeRate,
              weightLoadingRate,
              volumeLoadingRate,
              vehicleTypeId: vehicleTypeId.join(','),
              vehicleTypeCode: vehicleTypeCode.join(','),
              vehicleTypeCategory: vehicleTypeCategory.join(','),
              vehicleTypeCategoryName: vehicleTypeCategoryName.join(','),
            })
          }
        }
      }
    },
    {
      get type() {
        return self.selfCar ? 'input' : 'select'
      },
      hidden: true,
      name: 'vehicleAscriptionName',
      label: '车辆归属',
      span: 12,
      placeholder: '请选择车辆',
      get readOnly() {
        return self.selfCar
      },
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      get options() {
        return self.options.VEHICLE_TYPE
      }
    },
    {
      type: 'txt',
      hidden: true,
      name: 'vehicleAscription',
      label: '车辆归属id',
    },
    {
      hidden: true,
      label: '装载率',
      value: '',
      type: 'input',
      name: 'volumeRate',
      placeholder: '请选择车辆',
      readOnly: true,
      span: 12,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
    },

    // form.volumeLoadingRate?'体积:'+BigJs(form.volumeLoadingRate || 0)
    //                 .times(100)
    //                 .toString()+'%,重量:'+  BigJs(form.weightLoadingRate || 0)
    //                 .times(100)
    //                 .toString()+'%':''
    {
      name: 'vehicleAffiliationId',
      label: '车辆隶属机构ID',
      hidden: true,
      value: '',
    },
    {
      name: 'vehicleAffiliationCode',
      hidden: true,
      label: '车辆隶属机构CODE',
      value: '',
    },

    {
      hidden: true,
      ..._carrierAttr,
      span: 12,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      label: '隶属机构',
      targetValueKey: 'name',
      type: 'searchModal',
      name: 'vehicleAffiliationName',
      value: '',
      on: {

        change(event, value, dataArr, form) {
          if (!value) {

            form.setFieldsValue({
              vehicleAffiliationCode: '',
              vehicleAffiliationId: ''

            })
          }
        },
        ok(event, value, dataArr, form) {
          console.log(dataArr)
          let data = dataArr[0]
          if (data) {
            const { code: vehicleAffiliationCode, id: vehicleAffiliationId } = data
            form.setFieldsValue({
              vehicleAffiliationId,
              vehicleAffiliationCode
            })
          }
        }
      }
    },
    {
      hidden: true,
      span: 12,
      label: '车辆类别',
      type: 'input',
      readOnly: true,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      name: 'vehicleTypeCategoryName',
      value: '',
    },

    {
      name: 'vehicleTypeCategory',
      label: '车辆类别ID',
      hidden: true,
      value: '',
    },
    {
      name: 'vehicleDeviceId',
      label: '车辆设备ID',
      hidden: true,
      value: '',
    },
    {
      name: 'vehicleDeviceCode',
      hidden: true,
      label: '车辆设备CODE',
      value: '',
    },
    {
      name: 'containerId',
      label: '容器ID',
      hidden: true,
      value: ''
    },
    {
      name: 'containerCode',
      hidden: true,
      label: '容器CODE',
      value: '',
    },
    {
      hidden: true,
      span: 12,
      ...containerAttr,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      label: '容器号',
      type: 'searchModal',
      name: 'containerName',
      get disabled() {
        let form = self.getForm(self.orderActive);
        if (form) {
          let name = form.getFieldValue('vehiclePlateNumber');
          return !name
        }
        return true
      },
      value: '',
      on: {
        change(event, value, dataArr, form) {
          if (!value) {
            form.setFieldsValue({
              containerId: '',
              containerCode: ''
            })
          }
        },
        ok(event, value, dataArr, form) {
          if (dataArr.length > 0) {
            let ids = [], codes = [];
            dataArr.forEach(item => {
              const { id, code } = item
              ids.push(id)
              codes.push(code)
            });

            form.setFieldsValue({
              containerId: ids.join(','),
              containerCode: codes.join(',')
            })
          }
        }
      }
    },
    {
      hidden: true,
      span: 12,
      ..._deviceAttr,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      label: '设备号',
      type: 'searchModal',
      name: 'vehicleDeviceName',
      get disabled() {
        let form = self.getForm(self.orderActive);
        if (form) {
          let name = form.getFieldValue('vehiclePlateNumber');
          return !name
        }
        return true
      },
      remoteBefore(vNode, remoteUrl, form) {
        let vehicleId = form.getFieldValue('vehicleId')
        remoteUrl.data.vehicleId = vehicleId
      },
      value: '',
      on: {
        change(event, value, dataArr, form) {
          if (!value) {
            form.setFieldsValue({
              vehicleDeviceCode: '',
              vehicleDeviceId: ''
            })
          }
        },
        ok(event, value, dataArr, form) {
          console.log(dataArr)
          if (dataArr.length > 0) {
            let vehicleDeviceId = [], vehicleDeviceCode = [];
            dataArr.forEach(item => {
              const { deviceId, deviceCode } = item
              vehicleDeviceId.push(deviceId)
              vehicleDeviceCode.push(deviceCode)
            });

            form.setFieldsValue({
              vehicleDeviceId: vehicleDeviceId.join(','),
              vehicleDeviceCode: vehicleDeviceCode.join(','),
            })
          }

        }
      }
    },
    {
      label: '备注',
      name: 'memo',
      type: 'input',
      span: 24,
      labelCol: { span: 3 },
      wrapperCol: { span: 20 },


    },
    {
      label: '是否过磅',
      name: 'isWeigh',
      type: 'switch',
      checkedChildren:"是",
      unCheckedChildren:"否",
      replaceFields: {
        true:1,false:0
      },
      value: 0,
      span: 12,
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },

    },






  ]
}
export const m_setFormItem = self => {
  return [
    { name: 'shipbillQty', label: '总单数', type: 'txt', unit: '单', value: 0 },
    { name: 'totalWeightQty', label: '总重量', type: 'txt', unit: 'KG', value: 0 },
    { name: 'totalVolumeQty', label: '总体积', type: 'txt', unit: 'm³', value: 0 },
    { name: 'totalMileage', label: '总里程', type: 'txt', unit: '公里', value: 0 },

  ]
}