<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <search-area @submit="getList" @exportD="exportData"/>
    <div v-show='chartData.length'>
      <div class="chartTitle">同比分析图</div>
      <div style='width: 100%;height: 400px;'>
        <line-chart ref="analysisLineChart" :chartData="chartData"/>
      </div>
      <!-- 表格 -->
      <base-table
        :table-props="tableProps"
        :table-data="list"
        :max-height="tableH"
        class="yoy-out-table"
      />
    </div>
    <!-- 没有数据 -->
    <div class="no-data-bg" v-show='!chartData.length'></div>
  </div>
</template>
<script>
import { getYoy } from "@/api/analysis/energyAnalysis"
import subRate from "./components/subRate.vue"
import SearchArea from "./components/searchArea"
import LineChart from "./components/lineChart"
import FileSaver from "file-saver"
import * as XLSX from 'xlsx/xlsx.mjs'
export default {
  name: 'YoyAnalysis',
  components: { SearchArea, LineChart },
  data() {
    return {
      chartData: [],
      tableProps: [],
      list: [],
      tableH: this.tableHeight(640)
    }
  },
  mounted() {
    window.addEventListener('resize', () => {
      this.tableH = this.tableHeight(640)
    })
  },
  methods: {
    getList(params) {
      getYoy({ ...params }).then((res) => {
        if (res.code === 0 && res.data) {
          this.getTableList(res.data)
        } else {
          this.chartData = []
          this.list = []
        }
      })
    },
    getTableList(arr) {
      let data = arr.data
      let nameData = arr.nameData
      let tempX = []
      data[0].data.map((item) => {
        let obj = {}
        obj.prop = item.dynamicName
        obj.label = item.dynamicName
        obj.id = item.id
        obj.children = []
        tempX.push(obj)
      })
      for (let i = 0; i < nameData.length; i++) {
        for (let j = 0; j < tempX.length; j++) {
          if (tempX[j].id === nameData[i].parentId) {
            let obj = {}
            obj.prop = tempX[j].prop + '_' + nameData[i].name
            obj.label = nameData[i].name
            if (obj.label.indexOf('同比')!= -1) {
              obj.subcomponent = subRate
            }
            tempX[j].children.push(obj)
          }
        }
      }
      this.tableProps = [{prop: 'time',label: '时间'}].concat(tempX)
      console.log(this.tableProps)
      // 数据
      this.list = []
      for (let k = 0; k < data.length; k++) {
        let obj = {}
        obj.time = data[k].time
        let arr1 = data[k].data
        obj.type = []
        for (let q = 0; q < arr1.length; q++) {
          let name = arr1[q].dynamicName
          obj.type.push(name)
          let arr2 = arr1[q].children
          for (let p = 0; p < arr2.length; p++) {
            let prop = name + '_' + arr2[p].dynamicName
            obj[prop] = arr2[p].dynamicValue
          }
        }
        this.list.push(obj)
      }
      this.chartData = this.list
    },
    exportData(val) {
      if (this.list.length > 0) {
        var wb = XLSX.utils.table_to_book(document.querySelector(".yoy-out-table"))
        let fileName = val.name + "同比分析.xlsx"
        var wbout = XLSX.write(wb, {
            bookType: "xlsx",
            bookSST: true,
            type: "array"
        })
        try {
            FileSaver.saveAs(
            new Blob([wbout], { type: "application/octet-stream" }),
            fileName
            )
        } catch (e) {
            if (typeof console !== "undefined") console.log(e, wbout);
        }
        return wbout
      } else {
        this.$modal.msgWarning("暂无数据导出")
      }
    }
  }
}
</script>
<style lang='scss' scoped>
.chartTitle {
  font-size: 16px;
  color: #000;
}
.chartTitle::before {
  content: '';
  display: inline-block;
  width: 4px;
  height: 18px;
  background-color: #0B58FF;
  border-radius: 1px;
  margin-right: 8px;
  vertical-align: bottom;
}
</style>