<template>
  <div id="project-section-tree">
    <el-input v-model="treeFilterText" placeholder="输入关键字进行过滤" clearable />
    <!-- <el-scrollbar wrap-class="scrollbar-wrapper" :style="{ height: height }"> -->
    <el-tree
      ref="tree"
      style="margin-top:5px;"
      class="filter-tree"
      :props="treeProps"
      :filter-node-method="filterTree"
      :load="loadTreeNode"
      :highlight-current="true"
      lazy
      @node-click="nodeClick"
    >
      <span slot-scope="{ node, data }" class="custom-tree-node">
        <el-tag v-if="node.level === 1" size="mini">项目</el-tag>
        <el-tag v-if="node.level === 2" size="mini" type="success">标段</el-tag>
        <el-tag v-if="node.level === 3 && data.isDevice && data.deviceType===1" size="mini" type="danger">架桥机</el-tag>
        <el-tag v-if="node.level === 3 && data.isDevice && data.deviceType===2" size="mini" type="danger">运梁车</el-tag>
        <span>{{ data.name.substring(0, 10) }}</span>
        <!-- <el-tooltip :content="data.name" placement="right" effect="light" transition>
        </el-tooltip> -->
      </span>
    </el-tree>
    <!-- </el-scrollbar> -->
  </div>
</template>

<script>
import * as projectApi from '@/api/backbone/project'
import * as sectionApi from '@/api/backbone/section'
// import * as areaApi from '@/api/backbone/section-area'
import * as deviceApi from '@/api/device-manage'
export default {
  props: {
    height: {
      type: String,
      default: 'calc(100vh - 220px)'
    },
    deviceType: {
      type: String,
      default: undefined
    }
  },
  data() {
    return {
      treeFilterText: '',
      treeData: [],
      treeProps: {
        children: 'children',
        label: 'name',
        isLeaf: 'isSite'
      }
    }
  },
  watch: {
    treeFilterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    filterTree(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    loadTreeNode(node, resolve) {
      if (node.level === 0) {
        projectApi.lookupAll().then(projects => {
          projects.forEach(t => {
            t.isProject = true
            t.name = t.shortName || t.name
          })
          this.$emit('project-loaded', projects)
          return resolve(projects)
        })
      } else {
        if (node.data.isProject) {
          // sectionApi.lookupAllByProject(node.data.id).then(res => {
          sectionApi.lookupConstructions(node.data.id).then(res => {
            res.forEach(t => {
              t.isSection = true
            })
            return resolve(res)
          })
        } else if (node.data.isSection) {
          var params = {
            sectionId: node.data.id,
            deviceType: this.deviceType ?? undefined
          }
          deviceApi.lookUpByDevices(params)
            .then(res => {
              res.forEach(t => {
                t.isDevice = true
                t.code = t.deviceNo
                t.name = t.deviceName
                t.projectId = node.data.projectId
                t.sectionId = node.data.id
              })
              return resolve(res)
            })
        } else {
          return resolve([])
        }
      }
    },
    nodeClick(data, node) {
      this.$emit('node-click', data, node)
    }
  }
}
</script>
