<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.tableName + '-'+data.asas + '(' + 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-name="overallName"/>
        <overall-asset-structure v-if="showAssets === 'dataTable'" :table-params="tableParams"/>
      </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'
import { selectFrimary } from "@/api/system/accredit";

export default {
  name: 'assetStructure',
  components: { OverallAssetStructure, OverallSpecificAssets, OverallAssets },
  data() {
    return {
      mainHeight: window.innerHeight - 85,
      defaultProps: {
        children: 'childrenList',
        label: 'name'
      },
      assetStructureList: [],
      childrenList: [],
      overallName: [],
      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(data.tableLists)
      }, 500);
    },
    showAssetsFun(data){
      this.title = data.name + '('+data.databaseName + '-' + data.systemName+')'
      this.showAssets = data.type;
      if (data.type === 'dataSource') {
        this.overallName = data.tableLists
      }
      if (data.type === "dataTable"){
        this.tableParams = {
          tableName: data.tableName,
          databaseName: data.databaseName
        }
      }
    },
    init() {
      selectFrimary().then(res => {
        this.assetStructureList = res.data
      })
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）",
  created() {
    this.init()
  }
}
</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>
