<template>
  <!-- :load-tree-data="loadTreeData" -->
  <auditTreePicker
    v-bind="pickerMixinProps"
    :load-tree-data="loadTreeData"
    :module-id="treeparams.moduleId"
    :selectclick="selectclick"
    :default-expanded-keys="defaultExpandedKeys"
    :root-node="rootNode"
    :selectchecked="selectchecked"
    option-value="code"
    :search-tree-data="searchTreeData"
    :render="(item, direction) => renderItem(item, direction)"
    v-on="$listeners"
    ><a-icon v-if="!readOnly" slot="suffixIcon" type="apartment" />
  </auditTreePicker>
</template>

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

/**
 * 组织选择器
 * @displayName AuditGroupPicker 组织选择器
 */
export default {
  name: 'AuditGroupPicker',
  components: {
    ...components,
    auditTreePicker,
  },
  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',
    },
    // 特殊树不需要获取节点
    selectchecked: {
      type: Boolean,
      default: false,
    },
    selectcheckfn: {
      type: Function,
      defult: null,
    },
  },
  methods: {
    loadTreeData(depId) {
      let topDepId = null
      const params = {
        type: this.treeparams.type,
      }
      const getdata = new Promise((resolve) => {
        if (this.selectchecked) {
          Promise.resolve(this.selectcheckfn()).then((data) => {
            console.log(data)
            resolve(data)
          })
        } else {
          if (this.selectall) {
            topDepId = !depId ? 0 : depId
            const text = this.topNodeText
            auditTreeService.getCategoryTree(topDepId, this.treeparams, params).then((res) => {
              this.spinning = false
              const treeNode = [
                {
                  id: 0,
                  oldId: 0,
                  text: text,
                  name: text,
                  title: this.defaultTopNodeText,
                  code: 0,
                  leaf: false,
                  isleaf: false,
                  props: {},
                  children: [],
                  differentDisplay: false,
                  expandable: true,
                  type: 'Group',
                  key: 0,
                },
              ]
              if (res.data.length) {
                res.data.forEach((r) => {
                  r.oldId = r.id
                  r.code = r.id
                  r.title = r.text
                  r.name = r.text
                })
                treeNode[0].children = res.data
              }
              resolve(treeNode)
            })
          } else {
            auditPermissionTreeService.getPcParentOrgInfo().then((res) => {
              if (res.data.length > 0) {
                let id = res.data[0].id
                let text = res.data[0].text
                const name = res.data[0].name
                if (text === undefined) {
                  text = name
                }
                let code = this.defaultTopNodeId
                let oldId = res.data[0].id
                if (res.data[0].props.ORG_ID !== null) {
                  code = res.data[0].props.ORG_ID
                  id = res.data[0].props.ORG_ID
                }
                const obj = this.rootNode
                topDepId = !depId ? id : depId
                if (obj !== undefined) {
                  text = this.rootNode.name
                  code = this.rootNode.code
                  id = this.rootNode.code
                  topDepId = this.rootNode.id
                  oldId = this.rootNode.id
                } else {
                  text = '审计机构库'
                }
                this.defaultTopNodeId = oldId
                this.defaultTopNodeText = text
                this.defaultTreeExpandedKeys = [oldId]
                this.defaultSelectedKeys = [oldId]
                const params = {
                  moduleId: this.treeparams.moduleId,
                }
                auditPermissionTreeService.getCategoryTree(topDepId, params).then((res1) => {
                  this.spinning = false
                  res1.data.forEach((item) => {
                    if (item.name !== undefined) {
                      item.text = item.name
                    } else {
                      item.name = item.text
                    }
                    item.oldId = item.id
                    if (item.props.ORG_ID !== null) {
                      item.code = item.props.ORG_ID.toString()
                      item.id = item.props.ORG_ID
                    } else {
                      item.code = item.id.toString()
                    }
                    item.type = 'Group'
                  })
                  let treeNode = []
                  if (!depId) {
                    treeNode = [
                      {
                        id: id,
                        oldId: oldId,
                        text: this.defaultTopNodeText,
                        name: this.defaultTopNodeText,
                        title: this.defaultTopNodeText,
                        code: id.toString(),
                        leaf: false,
                        isleaf: false,
                        props: {},
                        children: res1.data.length > 0 ? res1.data : [],
                        differentDisplay: false,
                        expandable: res1.data.length > 0,
                        type: 'Group',
                      },
                    ]
                  } else {
                    treeNode = res.data
                  }
                  resolve(treeNode)
                })
              } else {
                resolve([])
              }
            })
          }
        }
      })
      return getdata
    },

    searchTreeData(searchValue) {
      if (searchValue) {
        if (searchValue) {
          return auditPermissionTreeService
            .groupsQuery({
              groupName: searchValue,
              moduleId: this.treeparams.moduleId,
              id: this.rootNode.id,
            })
            .then((data) => {
              return data
            })
        }
      }
    },
    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>
