<template>


  <el-container :style="{height: mainHeight + 'px'}">
    <el-aside>
      <el-tree :data="parentList"
               :expand-on-click-node="false"
               :load="expandTable"
               lazy
               @node-click="handleNodeClick"
               :props="defaultProps">
        <div class="custom-tree-node"  slot-scope="{ node, data }">
          <div v-if="data.type === 0" @click="select(data.id)">{{ data.accessSourceName + '(' + data.databaseName + '-' + data.dataSourceSystemName + ')' }}</div>
          <div v-if="data.type === 1">{{ data.name + '-'+data.as + '(' + data.dataTotal+'条)' }}</div>
        </div>
      </el-tree>
    </el-aside>
    <el-main>
      <el-container>
        <el-main>
          <el-empty v-if="showAssets === null" description="暂无数据" style="height: 100%;background-color: white"></el-empty>
          <auth-data-source v-if="showAssets === 0" :baseInfo="asset" />
          <auth-table v-else-if="showAssets === 1" :baseInfo="asset" :asset2="asset2"/>
        </el-main>
      </el-container>
    </el-main>
  </el-container>

</template>
<script>

import {
  findStructure,
  structureList,
} from '@/api/dataSource/source'
import AuthDataSource from '@/views/dataSource/assetAuthorization/auth/AuthDataSource.vue'
import AuthTable from '@/views/dataSource/assetAuthorization/auth/AuthTable.vue'
export default {

  name: 'assetAuthorization',
  components: { AuthTable, AuthDataSource},
  data() {
    return {
      mainHeight: window.innerHeight - 85,
      showAssets:null,
      parentList:[],
      defaultProps: {
        children: 'childrenList',
        label: 'name'
      },
      showAuth:null,
      asset:{},
      asset2:{},
    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleNodeClick(data,node) {
      if (node.level==1){
        this.asset ={}
        console.log(data)
        this.asset.accessSourceName=data.accessSourceName
        this.asset.dataSourceSystemName=data.dataSourceSystemName
        this.asset.databaseName=data.databaseName
        console.log("asset",this.asset)
      }

      if(node.level === 2) {
        this.asset2 ={}
        console.log(data)
        this.asset2.name=data.name
        this.asset2.as=data.as
        this.asset2.dataTotal=data.dataTotal
        console.log("asset",this.asset)
      }
    },
    expandTable(node, resolve) {
      if (node.level === 0){
        return resolve(this.parentList);
      }

      const { data } = node;

      this.showAssets = data.type;

      if (data.type === 1) {
        return resolve([])
      }
      // 模拟异步操作，延迟返回子节点数据
      setTimeout(() => {
        resolve(this.childrenList);
      }, 500);

      // 设置标题
      this.title = data.accessSourceName + '(' + data.databaseName + '-' + data.dataSourceSystemName + ')';
    },
    select(id){
      structureList(id).then(
          res=>{
            this.childrenList=res.data.databaseTableInformationList
          }
      )
    },
    //树状列表
    getList(){
      this.loading =true;
      findStructure().then(
          res=>{
            this.parentList=res.data.assetStructureList;
          }
      )
    },
  }
}
</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>
