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

export 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)])
    },
  }
}

export const setDetailsForm = (self, st) => {
  let selectedRowKeys = []

  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' },
    ...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({

            affiliationName: '',
            affiliationType: value > 1 ? 2 : '',
            affiliationId: '',
            affiliationCode: ''
          })
        }
      }
    },


    { name: 'affiliationId', label: '隶属机构ID', hidden: true },
    { name: 'affiliationType', label: '隶属机构类型', hidden: true, value: userCategory !== 'I' ? 2 : '' },
    { name: 'affiliationCode', label: '隶属机构编码', hidden: true },

    {
      name: 'affiliationName',
      label: '隶属机构',
      get type() {
        return st === 3 ? 'txt' : 'searchModal'
      },

      get disabled() {
        return self.affiliationType === 1
      },
      get required() {
        return self.affiliationType === 1 || userCategory == 'I' ? false : true
      },

      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',
      value: '',
      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: 3 },
      //className: 'itemNote',
      type: st === 3 ? 'txt' : 'input'
    },
    divider(self, '调度控制信息'),
    {
      wrapperCol: { span: 10 },
      labelCol: { span: 14 },
      name: 'maxTravelTime',
      label: () => {
        const icon = self._c('a-icon', { attrs: { type: 'exclamation-circle' }, });
        const tooltip = self._c('a-tooltip', { style: { 'color': '#2b80ff' }, attrs: { title: '选择司机时校验，派车单的最长行驶时间不能超过司机的最长行驶时间' } }, [icon]);
        return self._c('a-space', {}, [self._v('最长行驶时间'), tooltip]);
      },
      placeholder: '请输入最长行驶时间',
      unit: '小时',
      min: 0,
      type: st === 3 ? 'txt' : 'input',
      on: {
        blur(event, form) {
          let value = event.target.value
          let list = (value + '').split('.');
          if (list.length === 1) {
            form.setFieldsValue({ maxTravelTime: Number(value) })
            event.target.value = Number(value)
          } else if (list.length === 2) {
            form.setFieldsValue({ maxTravelTime: Number(list[0] + '.5') })
            event.target.value = Number(list[0] + '.5')
          }
        }
      },
    },
    {
      wrapperCol: { span: 10 },
      labelCol: { span: 14 },
      name: 'maxMileage',
      label: () => {
        const icon = self._c('a-icon', { attrs: { type: 'exclamation-circle' }, });
        const tooltip = self._c('a-tooltip', { style: { 'color': '#2b80ff' }, attrs: { title: '选择司机时校验，派车单的最长行驶里程不能超过司机的最长行驶里程' } }, [icon]);
        return self._c('a-space', {}, [self._v('最长行驶里程'), tooltip]);
      },
      placeholder: '请输入最长行驶里程',
      unit: '公里',
      min: 0,
      precision: 2,
      type: st === 3 ? 'txt' : 'number'
    },
    {
      wrapperCol: { span: 10 },
      labelCol: { span: 14 },
      name: 'maxDeliveryCustomer',
      label: () => {
        const icon = self._c('a-icon', { attrs: { type: 'exclamation-circle' }, });
        const tooltip = self._c('a-tooltip', { style: { 'color': '#2b80ff' }, attrs: { title: '选择司机时校验，派车单的最多配送客户不能大于司机的最多配送客户' } }, [icon]);
        return self._c('a-space', {}, [self._v('最多配送客户'), tooltip]);
      },
      placeholder: '请输入最多配送客户',
      unit: '个',
      min: 0,
      precision: 0,
      type: st === 3 ? 'txt' : 'number'
    },
    ...fns()
  ]
}
