<template>
  <el-container :style="{height: mainHeight + 'px'}">
    <el-aside>
      <el-tree :data="assetStructureList"
               :expand-on-click-node="false"
               :load="expandTable"
               lazy
               @node-click="showAssetsFun"
               :props="defaultProps">
        <div class="custom-tree-node" slot-scope="{ node, data }">
          <div v-if="data.type === 'dataSource'">{{ data.name + '('+data.databaseName + '-' + data.systemName+')' }}</div>
          <div v-if="data.type === 'dataTable'">{{ data.name + '-'+data.as + '(' + data.dataTotal+'条)' }}</div>
        </div>
      </el-tree>
    </el-aside>
    <el-container>
      <el-main>
        <OverallAssets v-if="showAssets == null"/>
        <overall-specific-assets v-if="showAssets === 'dataSource'" :title="title"/>
        <overall-asset-structure v-if="showAssets === 'dataTable'" :table-params="tableParams" :title="title"/>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import OverallAssets from './dashboard/OverallAssets.vue'
import OverallSpecificAssets from './dashboard/OverallSpecificAssets.vue'
import OverallAssetStructure from './dashboard/OverallAssetStructure.vue'

export default {
  name: 'assetStructure',
  components: { OverallAssetStructure, OverallSpecificAssets, OverallAssets },
  data() {
    return {
      mainHeight: window.innerHeight - 85,
      defaultProps: {
        children: 'childrenList',
        label: 'name'
      },
      assetStructureList: [
        {
          name: "测试1",
          systemName: "云计算系统",
          databaseName: "yunjisuan",
          type: "dataSource"
        },
        {
          name: "测试2",
          systemName: "网站系统",
          databaseName: "wangzhan",
          type: "dataSource"
        },
        {
          name: "测试3",
          systemName: "物联网系统",
          databaseName: "wulianwang",
          type: "dataSource"
        },
        {
          name: "测试4",
          systemName: "传媒系统",
          databaseName: "chuanmei",
          type: "dataSource"
        },
      ],
      childrenList: [
        {
          name: "sys_user",
          as: "用户表",
          dataTotal: 635847,
          type: "dataTable",
          childrenList: []
        },
        {
          name: "sys_dept",
          as: "部门表",
          dataTotal: 362548,
          type: "dataTable",
          childrenList: []
        },
        {
          name: "sys_notice",
          as: "通知公告",
          dataTotal: 6347,
          type: "dataTable",
          childrenList: []
        }
      ],
      showAssets: null,
      title: null,
      tableParams: null
    }
  },
  methods: {
    expandTable( node, resolve){
      if (node.level === 0) return resolve(this.assetStructureList);
      const {data} = node;
      this.showAssets = data.type;
      if (data.type === 'dataTable') {
        this.tableParams = {
          tableName: data.name,
          tableAsName: data.as
        }
        return resolve([])
      }
      setTimeout(() => {
        resolve(this.childrenList)
      }, 500);
    },
    showAssetsFun(data){
      this.title = data.name + '('+data.databaseName + '-' + data.systemName+')'
      this.showAssets = data.type;
      if (data.type === "dataTable"){
        this.tableParams = {
          tableName: data.name,
          tableAsName: data.as
        }
      }
    }
  }
}
</script>
<style scoped lang="scss">
.el-aside {
  margin: 0;
  padding: 0;
  width: 400px;
  background-color: white;
}
.el-main {
  background-color: #f1f1f1;
}
.custom-tree-node{
  height: 30px;
}
</style>
