<template>
  <SdTreePicker
    v-bind="pickerMixinProps"
    :load-tree-data="loadTreeData"
    :default-expanded-keys="defaultExpandedKeys"
    option-value="code"
    :search-tree-data="searchTreeData"
    :render="(item, direction) => renderItem(item, direction)"
    @change="change"
    ><a-icon v-if="!readOnly" slot="suffixIcon" type="apartment" />
  </SdTreePicker>
</template>

<script>
import AddressBook from '@/addressbook/group-user-service'
import pickerMixin from '@/common/components/sd-picker/picker-mixin'
import SdTreePicker from '@/common/components/sd-tree-picker.vue'

/**
 * 组织选择器
 * @displayName SdGroupPicker 组织选择器
 */
export default {
  name: 'AuditCompanySelect',
  components: {
    SdTreePicker,
  },
  mixins: [pickerMixin],
  props: {
    /**
     * 根节点{code:'200000',name:'西安分公司'}
     */
    rootNode: {
      type: [Object, Array],
      default: undefined,
    },
    /**
     * 默认展开的节点['200000']
     */
    defaultExpandedKeys: {
      type: Array,
      default: undefined,
    },
    /**
     * 当开启分级授权时，是否过滤掉启用分级授权的子公司，true时过滤,false时不过滤
     *
     */
    hierarchical: {
      type: Boolean,
      default: undefined,
    },
  },
  methods: {
    loadTreeData(id) {
      if (!id) {
        const expandedKey = this.defaultExpandedKeys
        if (Array.isArray(this.rootNode) && this.rootNode.length) {
          return Promise.all(
            this.rootNode.map((rootNode) => {
              return AddressBook.getGroupTree({
                expandedKey,
                rootNode,
                hierarchical: this.hierarchical,
                levPath: true,
              })
            })
          ).then((nodes) => {
            return nodes.map((node) => node[0])
          })
        } else {
          return AddressBook.getGroupTree({
            expandedKey,
            rootNode: this.rootNode,
            hierarchical: this.hierarchical,
            levPath: true,
          })
        }
      } else {
        return AddressBook.getChildNodes(id, {
          type: 'G',
          isContainSub: false,
          hierarchical: this.hierarchical,
          levPath: true,
        }).then((res) => {
          return res.map((item) => {
            item.checkable = true
            return item
          })
        })
      }
    },
    searchTreeData(searchValue) {
      if (searchValue) {
        return AddressBook.groupsQuery(searchValue, {
          levPath: true,
          hierarchical: this.hierarchical,
        }).then((data) => {
          return data
        })
      }
    },
    renderItem(item, direction) {
      return <span title={item.name}>{item.name.split('/').pop()}</span>
    },
    change(value) {
      this.$emit(
        'change',
        value?.map((item) => {
          return {
            name: item.name,
            code: item.code,
            type: item.type,
            orgCode: item.props?.code || item.orgCode,
          }
        })
      )
    },
  },
}
</script>

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