import {  queryCarriersUrl } from '../../api'


import departmenTree from '@/blocks/components/formConfig/departmenTree.multiple.js'
function divider(self, value) {
  return {
    label: '',
    span: 24,
    itemHeight: '60px',
    labelCol: { span: 0 },
    wrapperCol: { span: 24 },
    type: () => {
      return self._c('a-divider', { attrs: { orientation: 'left' } }, [self._v(value)])
    },
  }
}

/**0=不显示,1=查看,2=新建,3=修改,4=复制 */
// addressData: {
//   address: '越秀财富中心',
//   longitude: 114.250019,
//   latitude: 30.568682,
// },
export const setFormItems = (self, expData = []) => {
  let selectedRowKeys = []
  let inputType = (self) => (self.st === 1 ? 'txt' : 'input')
  let numberType = (self) => (self.st === 1 ? 'txt' : 'number')
  let e = []
  if (expData.length > 0) {
    expData.forEach((item) => {
      let type = item.type;
      Object.defineProperty(item, 'type', {
        get() {
          return self.st === 1 ? 'txt' : type
        }
      })
    })
    e = [divider(self, '扩展信息'), ...expData]
  }
  let userCategory = self.$store.getters?.userInfo?.userCategory ?? null

  return [
    { name: 'id', label: 'id', hidden: true, value: '' },
    {
      label: '设备编码',
      name: 'code',
      required: true,
      get readOnly() {
        return self.st === 3 ? true : false
      },
      get type() {
        return inputType(self)
      },
    },
    {
      label: '设备名称',
      name: 'name',
      get type() {
        return inputType(self)
      },
      required: true,
      get readOnly() {
        return self.st === 3 ? true : false
      },
    },
    {
      required: true,
      label: '设备类型',
      name: 'type',
      type: 'select',
      get mode() {
        return self.st === 1 ? 'txt' : ''
      },
      get options() {
        return self.options.DEVICE_TYPE
        },
    },
    {
      required: true,
      label: '设备生产厂商',
      name: 'company',
      type: 'select',
      get mode() {
        return self.st === 1 ? 'txt' : ''
      },
      get options() {
        return self.options.DEVICE_COMPANY
        },
    },
    ...departmenTree(self),

    { name: 'belongToName', label: '归属名称', hidden: true },
    {
      name: 'belongTo',
      label: '归属',
      required: true,
      value: userCategory !== 'I' ? 2 : 1,
      allowClear: false,
      get mode() {
        return self.st === 1 ? 'txt' : ''
      },
      type: 'select',
      get options() {
        if (userCategory !== 'I' && self.options.DRIVER_TYPE) {
          //非自有
          let DRIVER_TYPE = []
          self.options.DRIVER_TYPE.forEach((item, index) => {
            if (index !== 0) {
              DRIVER_TYPE.push(item)
            }
          })
          return DRIVER_TYPE
        }

        return self.options.DRIVER_TYPE
      },
      on: {
        change(value, label, options, form) {
          self.affiliationType = value
          form.setFieldsValue({
            typeName: label,
            affiliationName: '',
            affiliationType: value > 1 ? 2 : '',
            affiliationId: '',
            affiliationCode: '',
          })
        },
      },

    },
    { name: 'affiliationId', label: '隶属机构ID', hidden: true, value: '' },
    { name: 'affiliationType', label: '隶属机构类型', hidden: true, value: userCategory !== 'I' ? 2 : ''},
    { name: 'affiliationCode', label: '隶属机构编码', hidden: true, value: '' },
    {
      name: 'affiliationName',
      label: '隶属机构',
      get disabled() {
        return self.affiliationType === 1
      },
      get required() {
        return self.affiliationType === 1||userCategory == 'I'?false:true
      },
      get type() {
        return self.st === 1 ? 'txt' : 'searchModal'
      },
      targetClick: true,
      toolBar: {
        labelCol: { span: 8 },
        wrapperCol: { span: 16 },
        items: [
          {
            type: 'input',
            name: 'code',
            label: '承运商编码',
            value: '',
          },
          {
            type: 'input',
            name: 'name',
            label: '承运商名称',
            value: '',
          },
        ],
      },

      remoteUrl: {
        url: queryCarriersUrl,
        method: 'post',
      },
      rowKey: 'id',
      targetValueKey: 'name',

      on: {
        change(...args) {
          let data = args[2][0],
            form = args[3],
            obj = null
          if (self.affiliationType > 1) {
            obj = { affiliationType: data.type, affiliationId: data.id, affiliationCode: data.code }
          } else {
            obj = { affiliationName: data.cargoOwnerType, affiliationId: data.cargoOwnerId, affiliationCode: data.cargoOwnerCode }
          }
          form.setFieldsValue(obj)
          selectedRowKeys = args[2]
        },
      },
      tableConfig: {
        //selectedRowKeys:"10000002",
        columns: [
          { dataIndex: 'id', title: 'ID', hidden: true },
          { dataIndex: 'name', title: '承运商名称' },
          { dataIndex: 'code', title: '承运商编码' },
        ],
      },
    },

    {
      name: 'memo',
      label: '备注',
      get type() {
        return inputType(self)
      },
      value: '',
    },

    ...e,
  ]
}
