/**
 * 选择部门tree配置
 *
 *  @ApiModelProperty(value = "结算部门")
    private String settlementDepartment;

    @ApiModelProperty(value = "结算部门ID")
    private String settlementsettlementDepartmentId;

    @ApiModelProperty(value = "结算部门编码")
    private String settlementsettlementDepartmentCode;
 */
import { SERVICE_PLATFORM_POWER_BASE } from '@/config/serviceSettings.js'

const PinyinMatch = require('pinyin-match').default

export const getOrganization = (_this, data = {}) => {
  return _this
    .axios({
      url: SERVICE_PLATFORM_POWER_BASE + '/organization/all',
      data,
    })
    .then((response) => {
      if (response.success) {
        let ROOT_DEPARTURE = response.data.find((item) => {
          return item.parentId === '0'
        })
        _this.$set(_this.options, 'DEPARTMENT', response.data)
        _this.$set(_this.options, 'ROOT_DEPARTURE', ROOT_DEPARTURE)

        console.log(ROOT_DEPARTURE)
      }
    })
}
function resultTreeNodes(data, map = {}) {
  if (!data) return []
  data.forEach((item) => {
    delete item.children
    item.scopedSlots = { icon: 'custom' }
    item.isLeaf=true
    map[item.id] = item
  })
  let value = []
  data.forEach((item) => {
    var p = map[item.parentId]
    if (p) {
      if (!p.children) {
        p.children = []
      }
      p.isLeaf=false
      p.children.push(item)
    } else {
      value.push(item)
    }
  })
  return value
}

export default (self, view = 1) => {
  let user = self.$store.getters?.userInfo?.userCategory ?? null;

  return [
    {
      name: 'settlementDepartmentId',
      label: '结算部门ID',
      hidden: true,
      get value() {
        if (user&&user !== 'I') {
          return self?.options.ROOT_DEPARTURE?.id ?? undefined;
        }
      }
    },
    {
      name: 'settlementDepartment',
      label: '结算部门名称',
      type: 'txt',
      get hidden() {

        //I-内部员工(自有) C-客户 V-供应商 H-货主 S-服务商(承运商)
        if (user === 'I') {
          if (self.st === view) {
            return false
          }
          return true
        } else {
          return false
        }
      },
      readOnly: true,
      get value() {
        if (user&&user !== 'I') {
          return self?.options.ROOT_DEPARTURE?.name ?? undefined;
        }
      }
    },
    {

      name: 'settlementDepartmentCode',
      get hidden() {
        //I-内部员工(自有) C-客户 V-供应商 H-货主 S-服务商(承运商)
        if (user === 'I') {
          if (self.st === view) {
            return true
          }
          return false
        } else {
          return true
        }
      },
      get value() {
        if (user&&user !== 'I') {
          return self?.options.ROOT_DEPARTURE?.code ?? undefined;
        }
      },
      type: 'a-tree-select',
      label: '结算部门',
      required: true,
      dropdownClassName: 'p-tree',
      treeNodeFilterProp: 'title',
      replaceFields: { key: 'id', title: 'name', value: 'code' },
      showSearch: true,
      get treeDefaultExpandedKeys() {
        if (self.options?.DEPARTMENT?.length > 0) {
          let item = self.options.DEPARTMENT.find((item) => item.parentId == '0')
          return item ? [item.id] : []
        }
      },
      get treeData() {
        if (self.options?.DEPARTMENT?.length > 0) {
          return resultTreeNodes(self.options.DEPARTMENT)
        }
      },
      suffixIcon: () => {
        return self._c('a-icon', { class: 'picon', props: { theme: 'filled', type: 'folder' } })
      },
      filterTreeNode(inputValue, treeNode) {
        return PinyinMatch.match(treeNode.data.props.title, inputValue).length > 0 && treeNode
      },
      on: {
        change(value, label, extra, form) {
          if (!value) {
            return form.setFieldsValue({ settlementDepartmentId: '', settlementDepartment: '', settlementDepartmentCode: '' })
          }
          form.setFieldsValue({ settlementDepartmentId: extra.triggerNode.eventKey, settlementDepartment: label[0] })
        },
      },
      placeholder: '请选择部门',
    },
  ]
}
