import { cargo_ownerUrl, queryCarriersUrl } from '../../js/api'
import departmenTree from '@/blocks/components/formConfig/departmenTree.js'

export const setDetailsForm = (self, st) => {
  let fns = () => {
    return self.formExtendedField.map((item) => {
      let _item = { ...item }
      _item.type = st === 3 ? 'txt' : item.type
      return _item
    })
  }

  let userCategory = self.$store.getters?.userInfo?.userCategory ?? null
  return [
    { name: 'id', label: 'id', hidden: true, value: '' },
    { name: 'code', label: '送货员编码', required: true, type: st >= 2 ? 'txt' : 'input' },
    { name: 'name', label: '送货员姓名', required: true, type: st >= 2 ? 'txt' : 'input' },
    { name: 'typeName', label: '归属名称', hidden: true },
    ...departmenTree(self, 3),
    {
      name: 'type',
      label: '归属',
      required: true,
      value: userCategory !== 'I' ? 2 : 1,
      allowClear: false,
      mode: st === 3 ? 'txt' : '',
      type: 'select',
      get options() {
          console.log(userCategory)
        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 st === 3 ? '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: 'phoneNumber',
      label: '手机号',
      trigger: 'blur',
      rules: [
        {
          type: 'phone',
          required: true,
        },
      ],
      autocomplete: 'off',
      type: st === 3 ? 'txt' : 'input',
    },
    { name: 'password', label: '密码', required: true, autocomplete: 'off', type: st === 3 ? 'txt' : 'password' },
    {
      name: 'memo',
      label: '备注',
      span: 12,
      wrapperCol: { span: 20 },
      labelCol: { span: 4 },
      // className: 'itemNote',
      type: st === 3 ? 'txt' : 'input',
    },
    ...fns(),
  ]
}
