<template>
  <!-- :load-tree-data="loadTreeData" -->
  <lawCaseTreePicker
    v-bind="pickerMixinProps"
    :load-tree-data="loadTreeData"
    :selectclick="selectclick"
    :default-expanded-keys="defaultExpandedKeys"
    :root-node="rootNode"
    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" />
  </lawCaseTreePicker>
</template>

<script>
import AddressBook from '@/addressbook/group-user-service'
import lawCaseTreeService from './law-case-tree-service'
import lawCaseTreePicker from './law-case-tree-picker.vue'
import pickerMixin from './picker-mixin'
import components from './_import-components/law-case-select-picker-import'

/**
 * 案由选择
 * @displayName 案由选择
 */
export default {
  name: 'LawCaseSelectPicker',
  components: {
    ...components,
    lawCaseTreePicker,
  },
  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',
    },
  },
  data() {
    return {
      searchValue: '',
    }
  },
  methods: {
    loadTreeData(depId) {
      if (!depId) {
        // 没有传depId，这是根节点
        return lawCaseTreeService.getSelectCaseNode('0').then((res) => {
          res.data.forEach((item) => {
            item.id = item.id.toString()
            item.code = item.id.toString()
          })
          return [
            {
              id: '0',
              code: '0',
              text: this.topNodeText,
              leaf: false,
              expandable: true,
              differentDisplay: false,
              props: {},
              children: res.data,
            },
          ]
        })
      } else {
        // 打开子节点
        return lawCaseTreeService.getSelectCaseNode(depId)
      }
    },

    searchTreeData(searchValue) {
      this.searchValue = searchValue
      if (searchValue) {
        return lawCaseTreeService.searchCaseNode(searchValue).then((data) => {
          if (data.data.length === 0) {
            return []
          }
          data.data.forEach((item) => {
            item.id = item.id.toString()
            item.code = item.id.toString()
          })
          return [
            {
              id: '0',
              code: '0',
              text: this.topNodeText,
              leaf: false,
              expandable: true,
              differentDisplay: false,
              props: {},
              children: data.data,
            },
          ]
        })
      }
    },

    renderItem(item, direction) {
      const searchValue = this.searchValue
      let name = item.name
      if (name === undefined) {
        name = item.text
      }
      if (name.indexOf('/') > -1) {
        return <span title={name}>{name.split('/').pop()}</span>
      } else {
        if (name.indexOf(searchValue) > -1) {
          return (
            <span title={name}>
              {name.substr(0, name.indexOf(searchValue))}
              <span class='searchTitle'>{searchValue}</span>
              {name.substr(name.indexOf(searchValue) + searchValue.length)}
            </span>
          )
        }
        return <span title={name}>{name}</span>
      }
    },
  },
}
</script>

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