/**
 * 选择部门tree配置
 */
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 = !!item.isLeaf

    map[item.id] = item
  })
  let value = []

  data.forEach((item) => {
    var p = map[item.parentId]
    if (p) {
      if (!p.children) {
        p.children = []
      }
      //item.isLeaf = true
      p.children.push(item)
    } else {
      value.push(item)
    }
  })
  return value
}

export default (self) => {

 return {
    placeholder: '请选择部门',
    title: '部门',
    children:null,
    is: 'a-tree-select',
    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
    },
  }
}
