<template>
  <component-layout-one v-if="loaded" :siderProps="{width: leftWidth, collapsible: collapsible, collapsedWidth: collapsedWidth, theme: 'light'}">
    <template slot="left">
      <div class="bg-white pd-20" :style="{height: treeHeight}">
        <x-tree-pro
          :config-name="configName"
          :serviceName="serviceName"
          :env="env"
          :countVisible="countVisible"
          @action="action"
          @treeOnChecked="treeOnChecked"
          @itemChecked="itemChecked"
          ref="xtree"
        ></x-tree-pro>
      </div>
    </template>
    <div style="margin-left: 12px">
      <slot></slot>
    </div>
  </component-layout-one>
</template>

<script>
  import ComponentLayoutOne from '@vue2-client/layouts/ComponentLayoutOne'
  import XTreePro from '@vue2-client/base-client/components/common/XTree/XTreePro'
  export default {
    name: 'XTreeView',
    components: { ComponentLayoutOne, XTreePro },
    data () {
      return {
        // 加载状态
        loaded: false,
        // xTree 配置名称
        configName: undefined,
        // 环境
        env: undefined,
        // 命名空间
        serviceName: undefined,
        // 左侧宽度
        leftWidth: undefined,
        // 是否允许收起
        collapsible: undefined,
        // 左侧收起后宽度
        collapsedWidth: undefined,
        // 是否显示数量
        countVisible: undefined,
        // 树高度
        treeHeight: undefined,
        // 用于展示筛选出的数据
        searchData: [],
        // 查询值
        searchValue: ''
      }
    },
    methods: {
      init (params) {
        const {
          // xTree 配置名称
          configName,
          // 环境
          env = 'prod',
          // 命名空间
          serviceName,
          // 左侧宽度
          leftWidth = 300,
          // 是否允许收起
          collapsible = true,
          // 左侧收起后宽度
          collapsedWidth = 0,
          // 是否显示数量
          countVisible = false,
          // 树高度
          treeHeight = '85vh'
        } = params
        this.loaded = false
        this.configName = configName
        this.env = env
        this.serviceName = serviceName
        this.leftWidth = leftWidth
        this.collapsible = collapsible
        this.collapsedWidth = collapsedWidth
        this.countVisible = countVisible
        this.treeHeight = treeHeight
        this.loaded = true
      },
      /**
       * 详情按钮事件
       * @param record 本条数据
       * @param id 数据标识
       * @param actionType 操作类型
       * @param fun 向上级传递的事件
       */
      action (record, id, actionType, fun = 'action') {
        this.$emit(fun, record, id, actionType)
      },
      refresh () {
        this.$refs.xtree.refresh()
      },
      treeOnChecked (checkedKeys, deepNodes, deepKeys) {
        this.$emit('treeOnChecked', checkedKeys, deepNodes, deepKeys)
      },
      itemChecked (node) {
        this.$emit('itemChecked', node)
      },
      onChange (e) {
        const name = e.target.value
        this.searchValue = name
        this.searchData = []
        for (const row of this.treeData) {
          if (row.title.indexOf(name) > -1) {
            this.searchData.push(row)
          }
        }
      },
      close () {
        this.loaded = false
      }
    }
  }
</script>

<style lang="less" scoped>
  .pd-20 {
    padding: 20px;
  }
  /deep/ .ant-tree-switcher-icon svg{
    width: 1.5em;
    height: 1.5em;
  }
</style>
