<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <search-area @submit="getList" @exportD="exportData"/>
    <!-- 表格 -->
    <base-table
      :table-props="tableProps"
      :table-data="list"
      class="qoq-out-table"
    />
    <div style='width: 100%;height: 300px;padding-top: 30px;'>
      <line-chart ref="analysisLineChart" :chartData="chartData"/>
    </div>
  </div>
</template>
<script>
import { getQoq } from "@/api/analysis/energyAnalysis"
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: 'QoqAnalysis',
  components: { SearchArea, LineChart },
  data() {
    return {
      chartData: [],
      tableProps: [],
      list: []
    }
  },
  methods: {
    getList(params) {
      getQoq({ ...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
            tempX[j].children.push(obj)
          }
        }
      }
      this.tableProps = [{prop: 'time',label: '时间'}].concat(tempX)
      // 数据
      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(".qoq-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>