<template>
  <el-container :style="{height: mainHeight + 'px'}">
    <el-aside>
      <el-tree :data="assetStructureList"
               :load="expandTable"
               :expand-on-click-node="false"
               lazy
               @node-click="(data) => showAuth=data.type"
               :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>
        <auth-data-source v-if="showAuth === 'dataSource'"/>
        <auth-table v-else-if="showAuth === 'dataTable'"/>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>

import AuthDataSource from './auth/AuthDataSource.vue'
import AuthTable from './auth/AuthTable.vue'

export default {
  name: 'assetStructure',
  components: { AuthTable, AuthDataSource },
  data() {
    return {
      mainHeight: window.innerHeight - 85,
      defaultProps: {
        children: 'childrenList',
        label: 'name'
      },
      showAuth: null,
      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: []
        }
      ]
    }
  },
  methods: {
    expandTable( node, resolve){
      if (node.level === 0) return resolve(this.assetStructureList);
      const {data} = node;
      if (data.type === 'dataTable') {
        return resolve([])
      }
      setTimeout(() => {
        resolve(this.childrenList)
      }, 500);
    }
  }
}
</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>
