<template>
    <div>
        <el-card style="height: 958px">
          <el-tabs v-model="activeName">
            <el-tab-pane label="电表总览" name="first" style="height: 867px">
              <div style="display: flex;flex-wrap: wrap;overflow-y: auto">
                <div v-for="(item, index) in energyList" :key="index" class="toolCard">
                  <div style="width: 520px;height: 160px;border: 1px solid #afafaf;border-radius: 5px">
                    <div style="font-size: 20px;font-weight: bold;padding: 5px;background-color: #009e1c;color: #fff">{{'【' + item.设备名称 + '】' }}</div>
                    <div style="text-align: left;margin-left: 10px;font-size: 16px">
                      <el-col :span="10">
                        <div style="height: 40px;line-height: 40px">昨日累计：{{ item.前一日的电能 }}kW·h</div>
                        <div style="height: 40px;line-height: 40px">今日累计：{{ item.电能 }}kW·h</div>
                        <div style="height: 40px;line-height: 40px">产线能耗：{{ item.总能耗 }}kW·h</div>
                      </el-col>
                      <el-col :span="14">
                        <el-row style="margin-top: 20px">
                          <el-col :span="6">
                            <img src="../../img/erengy.png">
                          </el-col>
                          <el-col :span="18" style="font-size: 34px;font-weight: bolder">
                            <div style="color: #009e1c">{{ item.今日电能 }}kW·h</div>
                          </el-col>
                        </el-row>
                        <el-row>今日能耗</el-row>
                      </el-col>
                    </div>
                    <div style="margin-left: 20px;">
                    </div>
                  </div>
                </div>
              </div>
            </el-tab-pane>
            <el-tab-pane label="电表详情" name="second" style="height: 867px">
              <span>电流表：</span>
              <el-input v-model="deviceName" style="width:250px;margin-left: 10px" clearable placeholder="设备名称"></el-input>
              <span style="margin-left: 5px">日期：</span>
              <el-date-picker v-model="deliveryDate" type="daterange" range-separator="至" start-placeholder="开始日期" value-format="yyyy-MM-dd" end-placeholder="结束日期" style="width:250px"/>
              <el-button :disabled="loading" type="success" plain icon="el-icon-search" style="margin-left: 10px" @click="searchTable()">查询</el-button>
              <el-button :disabled="loading" type="warning" plain icon="el-icon-download" style="margin-left: 10px" @click="searchTable1()">导出</el-button>
              <div style="margin-top: 10px">
                <el-col :span="15">
                  <el-table
                      v-loading="loading"
                      :data="tableData"
                      :header-cell-style="{background:'#F5F5F5'}"
                      element-loading-text="数据加载中"
                      element-loading-spinner="el-icon-loading"
                      element-loading-background="rgba(0, 0, 0, 0.2)"
                      tooltip-effect="dark"
                      style="width: 100%"
                      height="780"
                      border
                      highlight-current-row
                      stripe>
                    <el-table-column align="center" label="日期" width="120">
                      <template slot-scope="scope">
                        {{ scope.row.日期 }}
                      </template>
                    </el-table-column>
                    <el-table-column align="center" label="设备名称" min-width="90">
                      <template slot-scope="scope">
                        {{ scope.row.设备名称 }}
                      </template>
                    </el-table-column>
                    <el-table-column align="center" label="当日开始(kW·h)" min-width="90">
                      <template slot-scope="scope">
                        {{ scope.row.当日开始 }}
                      </template>
                    </el-table-column>
                    <el-table-column align="center" label="当日结束(kW·h)" min-width="90">
                      <template slot-scope="scope">
                        {{ scope.row.当日结束 }}
                      </template>
                    </el-table-column>
                    <el-table-column align="center" label="当日消耗(kW·h)" min-width="90">
                      <template slot-scope="scope">
                        {{ scope.row.当日消耗 }}
                      </template>
                    </el-table-column>
                  </el-table>
                  <div class="block" style="margin-top: 10px;">
                    <el-pagination
                        :current-page="PageCurrent"
                        :page-sizes="[50, 70, 100, 150]"
                        :page-size="pageSize"
                        :total="total"
                        layout="total, sizes, prev, pager, next, jumper"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"/>
                  </div>
                </el-col>
                <el-col :span="9">
                  <el-row>
                    <div style="height: 370px;border: 1px solid #c4c4c4">
                      <div id="myChart22gt324422" style="width: 600px;height: 350px;margin-top: 5px"/>
                    </div>
                  </el-row>
                  <el-row>
                    <el-card style="height: 370px;border: 1px solid #c4c4c4">
                      <div id="myChart22gt324ddd422" style="width: 590px;height: 350px;margin-top: 5px"/>
                    </el-card>
                  </el-row>
                </el-col>
              </div>
            </el-tab-pane>
          </el-tabs>
        </el-card>
    </div>
</template>
<script>
import { getNowTime2 } from '@/utils/tool'
import * as echarts from 'echarts'

let myChartDom1 = null
let myChartDom2 = null
export default {
  data() {
    return {
      activeName: 'first',
      energyList: [],
      opMeasureRangeData: {
        name: '',
        min1: 0,
        max1: 0,
        min2: 0,
        max2: 0
      },
      upValue: 0,
      downValue: 0,
      daliyOutPutData_X: [],
      daliyOutPutData_Y: [],
      daliyOutPutData_Average: [],
      daliyOutPutData1_X: [],
      daliyOutPutData1_Y: [],
      daliyOutPutData1_Average: [],
      opnames: [],
      opnameThis: '',
      deviceName: '',
      opnameThisName: '',
      ymin: 0,
      ymax: 100,
      listOP: [],
      deliveryDate: [],
      loading: false,
      timer: null,
      tableData: [],
      total: 0, // 总条数
      pageSize: 50, // 每页显示条数
      PageCurrent: 1 // 后台获取页
    }
  },
  computed: {},
  mounted() {
    const scope = this
    myChartDom1 = echarts.init(document.getElementById('myChart22gt324422'))
    myChartDom2 = echarts.init(document.getElementById('myChart22gt324ddd422'))
    this.timer = setInterval(() => {
      scope.searchTableToday()
    }, 5000)
  },
  created() {
    const start = new Date()
    const end = new Date(start.getTime() - 3600 * 1000 * 24 * 20)
    this.deliveryDate[1] = getNowTime2(start)
    this.deliveryDate[0] = getNowTime2(end)
    this.searchTableToday()
    this.searchTable()
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    // 查询主表
    searchTableToday() {
      const param = []
      var Data = this.CreateData('11', '能源管理_今日能耗_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.energyList = response.data
        }
      })
    },
    // 查询主表
    searchTable() {
      if (!this.deliveryDate) {
        this.$message.warning('请选择查询时间')
        return
      }
      if (this.deliveryDate.length < 2) {
        this.$message.warning('请选择查询时间')
        return
      }
      if (this.deliveryDate[0] === '') {
        this.$message.warning('请选择查询时间')
        return
      }

      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['开始时间', this.deliveryDate[0]]
      param[1] = ['结束时间', this.deliveryDate[1]]
      param[2] = ['设备名称', this.deviceName]
      param[3] = ['PageCurrent', this.PageCurrent]
      param[4] = ['PageSize', this.pageSize]
      param[5] = ['PageCount', '1111', 'int', '1']
      param[6] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '能源管理_每日能耗_历史_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data.result
          this.total = parseInt(response.data.output[0].ItemCount)
        }
        this.loading = false
        this.searchTableChart()
      })
    },
    // 查询折线图
    searchTableChart() {
      this.daliyOutPutData_X = []
      this.daliyOutPutData_Y = []
      var param = []
      param[0] = ['开始时间', this.deliveryDate[0]]
      param[1] = ['结束时间', this.deliveryDate[1]]
      var Data = this.CreateData('11', '能源管理_每日能耗折线图_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          for (let i = 0; i < response.data.length; i++) {
            this.daliyOutPutData_X.push(response.data[i]['今日日期'].substring(5, 10))
            this.daliyOutPutData_Y.push(response.data[i]['今日电能'])
          }
        }
        this.searchTable3()
        this.drawLine51()
      })
    },
    // 查询饼图
    searchTable3() {
      this.daliyOutPutData_Y1 = []
      var param = []
      param[0] = ['开始时间', this.deliveryDate[0]]
      param[1] = ['结束时间', this.deliveryDate[1]]
      var Data = this.CreateData('11', '能源管理_单表占比能耗饼图_查询', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response.data)
        if (response.data.length !== 0) {
          for (const info of response.data) {
            this.daliyOutPutData_Y1.push({ name: info.设备名称, value: info.今日电能 })
          }
        }
        this.drawLine5133()
      })
    },
    searchTable1() {
      if (!this.deliveryDate) {
        this.$message.warning('请选择查询时间')
        return
      }
      if (this.deliveryDate.length < 2) {
        this.$message.warning('请选择查询时间')
        return
      }
      if (this.deliveryDate[0] === '') {
        this.$message.warning('请选择查询时间')
        return
      }
      this.tableData1 = []

      var param = []
      const blob = 'data:image/gif;base64,R0lGODlhCQACAIAAAMzMzP///yH5BAEAAAEALAAAAAAJAAIAAAIEjI9pUAA7'
      param[0] = ['开始时间', this.deliveryDate[0]]
      param[1] = ['结束时间', this.deliveryDate[1]]
      param[2] = ['opname', this.opnameThis]
      var Data = this.CreateData('2003', 'LG_临工看板_产量管理_测量数据导出', param)
      this.exportExcel_NPOI(Data, blob)
    },
    // 画折线图
    drawLine51() {
      if (this.daliyOutPutData_X.length === 0) return
      // 绘制图表
      myChartDom1.setOption(
        {
          title: {
            text: '总能耗信息(kW·h)'
          },
          grid: [
            {
              left: '15%',
              top: '10%',
              right: '5%',
              bottom: '15%'
            }
          ],
          tooltip: {
            trigger: 'axis'
          },
          xAxis: {
            type: 'category',
            data: this.daliyOutPutData_X,
            axisLine: {
              lineStyle: {
                color: '#409EFF'
              },
              symbol: ['none', 'arrow'],
              symbolOffset: [0, 10]
            },
            axisLabel: {
              color: '409EFF',
              interval: 0,
              rotate: 45
            }
          },
          yAxis: {
            style: '#409EFF',
            scale: true,
            type: 'value',
            axisLine: {
              lineStyle: {
                color: '#409EFF'
              },
              symbol: ['none', 'arrow'],
              symbolOffset: [0, 10]
            },
            axisLabel: {
              color: '409EFF'
            }
          },
          series: [
            {
              data: this.daliyOutPutData_Y,
              type: 'line',
              color: '#006868',
              label: {
                show: true
              }
            }
          ]
        }
      )
    },
    // 画饼图
    drawLine5133() {
      // 基于准备好的dom，初始化echarts实例
      if (this.daliyOutPutData_X.length === 0) return
      // 绘制图表
      myChartDom2.setOption(
        {
          title: {
            text: '单表能耗占比',
            left: 'left'
          },
          tooltip: {
            trigger: 'item'
          },
          legend: {
            orient: 'vertical',
            left: 'right'
          },
          series: [
            {
              name: '单表能耗',
              type: 'pie',
              radius: '65%',
              data: this.daliyOutPutData_Y1,
              emphasis: {
                itemStyle: {
                  shadowBlur: 8,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              },
              label: {
                show: true,
                normal: {
                  textStyle: {
                    fontWeight: 'normal',
                    fontSize: 16
                  }
                }
              }
            }
          ]
        }
      )
    },
    handleSizeChange(val) {
      this.PageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.PageCurrent = val
      this.searchTable()
    }
  }
}
</script>
<style scoped>
    .toolCard {
      text-align: center;
      font-size: 30px;
      margin: 10px 0 0 10px;
    }
    .div1{
        width: 140px;
        height: 105px;
        margin-left:15px;
        margin-top:15px;
        background: green;
        color: white;
        font-size: 25px;
    }
    .div2{
        width: 140px;
        height: 105px;
        margin-left:15px;
        margin-top:15px;
        background: #60839e;
        color: white;
        font-size: 25px;
    }
    .Checkbox {
        position: absolute;
        visibility: hidden;
    }
</style>
