import selectDatasourceMixin from 'mars-pro/packages/pro-select/src/mixins/select-datasource';
import selectBaseMixin from 'mars-pro/packages/pro-select/src/mixins/select-base';
import ProSelectTreeList from './tree-list';
import treeArray from 'mars-pro/src/pro/tree-array';

export default {
  name: 'ProSelectTree',
  componentName: 'ProSelectTree',
  components: {
    ProSelectTreeList
  },
  mixins: [selectBaseMixin, selectDatasourceMixin],
  props: {},
  computed: {
    isShowSearch() {
      return this.showSearch
    },
    filterDatas() {
      return this.selectDatas
    }
  },
  methods: {
    // 重构 selectDatasourceMixin.methods.addSelectCacheOptions
    addSelectCacheOptions(optionItems) {
      const useAtrrs = this.$attrs
      const props = useAtrrs.props || {}
      const labelKey = props.label || this.labelKey
      const nodeKey = useAtrrs.nodeKey || this.valueKey
      let _optionItems = treeArray(optionItems, props.children || 'children', function (item) {
        return {
          ...item,
          label: item[labelKey],
          value: item[nodeKey],
          currentLabel: item[labelKey],
        }
      })
      // 将optionItems转换成 数组
      selectDatasourceMixin.methods.addSelectCacheOptions.call(this, _optionItems)
    },
    handleSearch(query) {
      this.$refs.eltree.$refs.eltree.$refs.eltree.store.filter(query)
    },
    defaultFilterNodeMethod(query, data, node) {
      query = (query || '').trim()
      const useAtrrs = this.$attrs
      const props = useAtrrs.props || {}
      const labelKey = props.label || this.labelKey
      if (!query) {
        return true
      }
      return data[labelKey].indexOf(query) > -1
    }
  },
  render() {
    const content = <ProSelectTreeList ref="eltree" key="eltree"/>
    return this.renderSelectWapper(content, {})
  }
}

