<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-container>
      <el-main>
        <overall-assets v-if="showAssets==null" :sum="sum"/>
        <overall-specific-assets v-if="showAssets === 0"  :num="num" :dataTotal="dataTotal" :databaseTableInformationList="databaseTableInformationList"   :title="title"/>
        <overall-asset-structure v-if="showAssets === 1" :databaseTable="databaseTable" :databaseTableInformation="databaseTableInformation"  :childrenList="childrenList" :title="title"/>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>

import OverallSpecificAssets from "@/views/dataSource/assets/OverallSpecificAssets.vue";
import OverallAssets from "@/views/dataSource/assets/OverallAssets.vue";
import {
  findAssetStructure, findDataBaseByAssetId, findDataBaseByInformationId, findDataBaseTable,
  findDataBaseTableById,
  findInformationById
} from "@/api/dataSource/source";
import OverallAssetStructure from "@/views/dataSource/assets/OverallAssetStructure.vue";

export default {
  name: 'assets',
  components: {OverallAssetStructure, OverallSpecificAssets, OverallAssets },
  data() {
    return {
      mainHeight: window.innerHeight - 85,
      defaultProps: {
        children: 'childrenList',
        label: 'name'
      },

      assetStructureList: [],
      childrenList: [],
    showAssets: null,
      title: null,
      databaseTableInformation:{},
      databaseTableInformationList:[],
      databaseTable:[],
      sum:0,
      num:0,
      dataTotal:0,
  }
  },
  created() {
    this.findAssetStructure();
  },
  methods: {
    expandTable(node, resolve) {
      if (node.level === 0) return resolve(this.assetStructureList);
      const {data} = node;
      this.showAssets = data.id;

        findDataBaseByAssetId(data.id).then(
          res=>{
            this.databaseTableInformationList=res.data;
            this.num=this.databaseTableInformationList.length;
            res.data.forEach(item=>{
              this.dataTotal+=item.dataTotal
            })
          }
        )
        findDataBaseTable(data.id).then(
          res=>{
            this.databaseTableInformation=res.data
          }
        )
        findInformationById(data.id).then(
          res=>{
            this.childrenList=res.data;
          }
        )
      if (data.type === 1){
        findDataBaseByInformationId(data.id).then(
          res=>{

            this.databaseTable=res.data;
            console.log("res",this.databaseTable)
          }
        )
      }

      setTimeout(() => {
        resolve(this.childrenList)
      }, 500);
    },
    showAssetsFun(data){
      this.title = data.accessSourceName + '('+data.databaseName + '-' + data.dataSourceSystemName+')'
      this.showAssets = data.type;
    },
    findAssetStructure(){
      findAssetStructure().then(
        res=>{
          this.assetStructureList=res.data;
          this.sum=this.assetStructureList.length;
        }
      )
    },

  }
}
</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>
