<template>
  <el-container :style="{height: mainHeight + 'px'}">
    <el-aside>
      <el-tree :data="assetStructureList"
               :load="expandTable"
               lazy
               @node-click="showAssetsFun"
               :props="defaultProps">
        <div class="custom-tree-node" slot-scope="{ node, data }">
          <div v-if="data.type === 0">{{ 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 AuthDataSource from '@/views/dataSource/assetAuthorization/auth/AuthDataSource.vue'
import AuthTable from '@/views/dataSource/assetAuthorization/auth/AuthTable.vue'
import {findAssetStructure, findInformationById} from "@/api/dataSource/source";
export default {

  name: 'assetAuthorization',
  components: { AuthTable, AuthDataSource},
  data() {
    return {
      mainHeight: window.innerHeight - 85,
      defaultProps: {
        children: 'childrenList',
        label: 'name'
      },

      assetStructureList: [],
      childrenList: [],
      showAssets: null,
      asset:{},
      asset2:{},
    }
  },
  created() {
    this.getList();
  },
  methods: {
    expandTable(node, resolve) {

      if (node.level === 0){
        return resolve(this.assetStructureList);
      }

      const { 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 ={}
        this.asset2.name=data.name
        this.asset2.as=data.as
        this.asset2.dataTotal=data.dataTotal
        console.log("asset",this.asset)
      }
      findInformationById(data.id).then(
        res=>{
          this.childrenList=res.data;
        }
      )
      this.showAssets = data.type;

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

      // 设置标题
      this.title = data.accessSourceName + '(' + data.databaseName + '-' + data.dataSourceSystemName + ')';
    },

    //树状列表
    getList(){
      findAssetStructure().then(
          res=>{
            this.assetStructureList=res.data;
          }
      )
    },
  }
}
</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>
