<!-- 联云平台类型选择器 -->
<!-- change方法输出对象 { platformTypeId, platformTypeName } -->
<!--
<PlatformTypeSelect v-model="searchParams.platformTypeId" search-mode />
<PlatformTypeSelect v-model.trim="formData.platformTypeName" @change="Object.assign(formData, $event)" />
-->
<template>
  <el-select
    ref="selector"
    v-bind="$attrs"
    filterable
    :value="value"
    :filter-method="filterCloud"
    @input="$emit('input', $event)"
    @focus="filterCloud()"
    @change="handleSelectChange"
  >
    <el-option value="" hidden />
    <el-option v-if="searchMode" value="" label="全部" />
    <el-tree
      ref="tree"
      :data="platformTypeList"
      :props="{ children: 'children', label: 'name' }"
      :expand-on-click-node="true"
      :check-on-click-node="true"
      :filter-node-method="filterNode"
      accordion
      node-key="id"
      placeholder="请选择"
      empty-text="暂无数据"
      filterable
      @node-click="handleNodeClick"
    />
  </el-select>
</template>
<script>
// import { getDeptTree } from '@/apiV2/dept'

export default {
  name: 'PlatformTypeSelect',
  props: {
    value: {
      type: String,
      default: ''
    },
    searchMode: {
      type: Boolean,
      default: false
    },
    options: {
      type: Array,
      default: () => ([])
    }
  },
  data() {
    return {
      platformTypeList: []
    }
  },
  mounted() {
    this.getOptions()
  },
  methods: {
    filterNode(value, data) {
      if (!value) {
        return true
      }
      return data.name.indexOf(value) !== -1
    },
    filterCloud(e) {
      this.$refs.tree.filter(e)
    },
    handleSelectChange(e) {
      if (e === '' && this.searchMode) {
        this.handleNodeClick({ id: '', name: '' })
      }
    },
    handleNodeClick(e) {
      if (e.disabled) return
      this.$emit('change', { platformTypeId: e.id, platformTypeName: e.name })
      this.$emit('input', e.name)
      this.$forceUpdate()
      this.$refs.selector.blur()
    },
    async getOptions() {
      if (this.options && this.options.length) {
        this.platformTypeList = this.options.map(i => ({ ...i, disabled: true }))
      } else { // 暂时只支持传入options模式，可以参考cloud/platform/components/tree.vue中的数据获取组装
        // const res = await getDeptTree()
        // if (res.success) {
        //   this.platformTypeList = res.data.filter(item => item.status === 1)
        // }
      }
    }
  }
}
</script>
<style lang="scss">

</style>
