<template>
  <!-- :load-tree-data="loadTreeData"
    :module-id="treeparams.moduleId" -->
  <icTreePicker
    :is-abandonment="isAbandonment"
    v-bind="pickerMixinProps"
    :load-tree-data="loadTreeData"
    :selectclick="selectclick"
    :default-expanded-keys="defaultExpandedKeys"
    :selecttype="selecttype"
    :types="types"
    :org-id="orgId"
    :version-id="versionId"
    option-value="code"
    option-label="text"
    :search-tree-data="searchTreeData"
    :render="(item, direction) => renderItem(item, direction)"
    v-on="$listeners"
    ><a-icon v-if="!readOnly" slot="suffixIcon" type="apartment" />
  </icTreePicker>
</template>

<script>
// import AddressBook from '@/addressbook/group-user-service'
// import auditPermissionTreeService from '../audit-permission-tree-service'
import icTreeService from './ic-tree-service'
import icTreePicker from './ic-tree-picker.vue'
import pickerMixin from './picker-mixin'
import components from './_import-components/ic-measure-picker-import'

/**
 * 组织选择器
 * @displayName icMeasurPicker 组织选择器
 */
export default {
  name: 'IcMeasurPicker',
  components: {
    ...components,
    icTreePicker,
  },
  mixins: [pickerMixin],
  props: {
    selectall: {
      type: Boolean,
      default: false,
    },
    selectclick: {
      type: Function,
      default: null,
    },
    /**
     * 根节点{code:'200000',name:'西安分公司'}
     */
    rootNode: {
      type: [Object, Array],
      default: undefined,
    },
    /**
     * 默认展开的节点['200000']
     */
    defaultExpandedKeys: {
      type: Array,
      default: undefined,
    },
    /**
     * 当开启分级授权时，是否过滤掉启用分级授权的子公司，true时过滤,false时不过滤
     *
     */
    hierarchical: {
      type: Boolean,
      default: undefined,
    },
    // 地址树接口数据源
    treeparams: {
      type: Object,
      default: () => {
        return {}
      },
    },
    // 根节点名称
    topNodeText: {
      type: String,
      default: '审计机构',
    },
    // 根节点ID
    topNodeId: {
      type: String,
      default: '0',
    },
    // 事项选择器
    versionId: {
      type: String,
      default: '',
    },
    orgId: {
      type: String,
      default: '',
    },
    orgName: {
      type: String,
      default: '',
    },
    selecttype: {
      type: String,
      default: 'control',
    },
    types: {
      type: String,
      default: 'NK',
    },
    isAbandonment: {
      type: String,
      default: 'NO',
    },
  },
  data() {
    return {
      defaultTopNodeId: -1,
      // defaultTopNodeText: '风控库',
    }
  },
  methods: {
    loadTreeData(depId) {
      const params = {
        auditOrgId: parseInt(this.orgId),
        versionId: parseInt(this.versionId),
      }
      this.defaultTopNodeText = this.orgName
      const getdata = new Promise((resolve) => {
        icTreeService.getCategoryTree(this.defaultTopNodeId, params, this.types).then((res1) => {
          this.spinning = false
          res1.data.forEach((item) => {
            item.code = item.id.toString()
            item.cat = true
            item.isLeaf = false
          })
          const treeNode = [
            {
              id: this.defaultTopNodeId,
              text: this.defaultTopNodeText,
              code: this.defaultTopNodeId.toString(),
              isLeaf: false,
              props: {
                isroot: true,
              },
              children: res1.data,
              key: this.defaultTopNodeId.toString(),
              cat: true,
            },
          ]
          resolve(treeNode)
        })
      })
      return getdata
    },

    searchTreeData(searchValue) {
      if (searchValue) {
      }
    },
    renderItem(item, direction) {
      let name = item.name
      if (name === undefined) {
        name = item.text
      }
      if (name.indexOf('/') > -1) {
        return <span title={name}>{name.split('/').pop()}</span>
      } else {
        return <span title={name}>{name}</span>
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
