<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <div class="search-left">
        <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
          <el-row>
            <el-form-item class="selectForm-container-item">
              <el-date-picker
                v-model="timeRange"
                :picker-options="pickerOptions"
                :clearable="false"
                type="daterange"
                value-format="yyyy-MM-dd"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"/>
            </el-form-item>
            <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            <el-button class="filter-item" type="warning" icon="el-icon-delete" @click="clearInput">重置</el-button>
          </el-row>
        </el-form>
      </div>
      <div class="clearfloat"/>
    </div>
    <el-tabs v-model="tab" type="border-card">
      <el-tab-pane label="街道统计" name="street">
        <el-row v-if="tab === 'street'" style="padding-top: 20px;">
          <el-col :span="12" >
            <div ref="chart1" class="chart-body"/>
          </el-col>
          <el-col :span="12" >
            <div ref="chart3" class="chart-body"/>
          </el-col>
        </el-row>
        <el-row v-if="tab === 'street'" style="padding-top: 20px;">
          <el-col :span="12" >
            <div ref="chart5" class="chart-body"/>
          </el-col>
          <el-col :span="12" >
            <div ref="chart7" class="chart-body"/>
          </el-col>
        </el-row>
      </el-tab-pane>
      <el-tab-pane label="网格统计" name="area">
        <el-row v-if="tab === 'area'" style="padding-top: 20px;">
          <el-col :span="12" >
            <div ref="chart2" class="chart-body"/>
          </el-col>
          <el-col :span="12" >
            <div ref="chart4" class="chart-body"/>
          </el-col>
        </el-row>
        <el-row v-if="tab === 'area'" style="padding-top: 20px;">
          <el-col :span="12" >
            <div ref="chart6" class="chart-body"/>
          </el-col>
          <el-col :span="12" >
            <div ref="chart8" class="chart-body"/>
          </el-col>
        </el-row>
      </el-tab-pane>
    </el-tabs>
    <el-row style="margin-top: 40px;">
      <el-col :span="12" offset="6">
        <div ref="chart" class="chart-body"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import echarts from 'echarts'
import { staticsAlarmType, staticsLightTimeByStreet, staticsLightTimeByGrid, staticsLightRateByStreet, staticsLightRateByGrid, staticsQuantityByStreet, staticsQuantityByGrid, staticsSaveRateByStreet, staticsSaveRateByGrid } from '@/api/system/report'

export default {
  name: 'Statics',
  data() {
    return {
      tab: 'street',
      timeRange: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      listQuery: {
        startDate: '',
        endDate: ''
      }, // 筛选条件
      alarmType: [],
      lightTimeByStreet: [],
      lightTimeByGrid: [],
      lightRateByStreet: [],
      lightRateByGrid: [],
      quantityByStreet: [],
      quantityByGrid: [],
      saveRateByStreet: [],
      saveRateByGrid: [],
      listLoading: true // 列表加载
    }
  },
  watch: {
    tab(val) {
      this.fetchData()
    },
    timeRange(val) {
      if (val && val.length > 0) {
        this.listQuery.startDate = val[0]
        this.listQuery.endDate = val[1]
      } else {
        this.listQuery.startDate = ''
        this.listQuery.endDate = ''
      }
    }
  },
  created() {
    this.fetchData()// 获取数据
  },
  methods: {
    fetchData(isNowPage = true) {
      this.listLoading = true
      console.log(this.listQuery)
      staticsAlarmType(this.listQuery).then(response => {
        if (response.code === 200) {
          this.alarmType = response.data
          this.initChart()
        } else {
          this.$message.error(response.message)
        }
      })
      staticsLightTimeByStreet(this.listQuery).then(response => {
        if (response.code === 200) {
          this.lightTimeByStreet = response.data
          this.initChart1()
        } else {
          this.$message.error(response.message)
        }
      })
      staticsLightTimeByGrid(this.listQuery).then(response => {
        if (response.code === 200) {
          this.lightTimeByGrid = response.data
          this.initChart2()
        } else {
          this.$message.error(response.message)
        }
      })
      staticsLightRateByStreet(this.listQuery).then(response => {
        if (response.code === 200) {
          this.lightRateByStreet = response.data
          this.initChart3()
        } else {
          this.$message.error(response.message)
        }
      })
      staticsLightRateByGrid(this.listQuery).then(response => {
        if (response.code === 200) {
          this.lightRateByGrid = response.data
          this.initChart4()
        } else {
          this.$message.error(response.message)
        }
      })
      staticsQuantityByStreet(this.listQuery).then(response => {
        if (response.code === 200) {
          this.quantityByStreet = response.data
          this.initChart5()
        } else {
          this.$message.error(response.message)
        }
      })
      staticsQuantityByGrid(this.listQuery).then(response => {
        if (response.code === 200) {
          this.quantityByGrid = response.data
          this.initChart6()
        } else {
          this.$message.error(response.message)
        }
      })
      staticsSaveRateByStreet(this.listQuery).then(response => {
        if (response.code === 200) {
          this.saveRateByStreet = response.data
          this.initChart7()
        } else {
          this.$message.error(response.message)
        }
      })
      staticsSaveRateByGrid(this.listQuery).then(response => {
        if (response.code === 200) {
          this.saveRateByGrid = response.data
          this.initChart8()
        } else {
          this.$message.error(response.message)
        }
      })
    },
    initChart() {
      var list = []
      for (var i = 0; i < this.alarmType.length; i++) {
        var data = { value: this.alarmType[i].num, name: this.alarmType[i].alarmTypeName }
        list.push(data)
      }
      var chart = echarts.init(this.$refs.chart)
      chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c} ({d}%)'
        },
        title: {
          text: '报警类型统计',
          left: 'center'
        },
        legend: {
          orient: 'vertical',
          left: 'left',
          data: this.alarmType.map(item => item.alarmTypeName)
        },
        series: [
          {
            type: 'pie',
            radius: '55%',
            center: ['50%', '60%'],
            data: list,
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
      chart.resize()
    },
    initChart1() {
      var chart = echarts.init(this.$refs.chart1)
      chart.setOption({
        tooltip: {
          show: true
        },
        title: {
          text: '各街道亮灯时间统计',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.lightTimeByStreet.map(item => item.streetName)
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          barMaxWidth: 50, // 柱图宽度
          data: this.lightTimeByStreet.map(item => item.duration),
          type: 'bar',
          tooltip: {
            formatter: '{b}<br/>亮灯时长: {c}h'
          }
        }]
      })
      chart.resize()
    },
    initChart2() {
      var chart = echarts.init(this.$refs.chart2)
      chart.setOption({
        tooltip: {
          show: true
        },
        title: {
          text: '各网格亮灯时间统计',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.lightTimeByGrid.map(item => item.areaName)
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          barMaxWidth: 50, // 柱图宽度
          data: this.lightTimeByGrid.map(item => item.duration),
          type: 'bar',
          tooltip: {
            formatter: '{b}<br/>亮灯时长: {c}h'
          }
        }]
      })
      chart.resize()
    },
    initChart3() {
      var chart = echarts.init(this.$refs.chart3)
      chart.setOption({
        tooltip: {
          show: true
        },
        title: {
          text: '各街道亮灯率统计',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.lightRateByStreet.map(item => item.streetName)
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          barMaxWidth: 50, // 柱图宽度
          data: this.lightRateByStreet.map(item => item.lightRate),
          type: 'bar',
          tooltip: {
            formatter: '{b}<br/>亮灯率: {c}'
          }
        }]
      })
      chart.resize()
    },
    initChart4() {
      var chart = echarts.init(this.$refs.chart4)
      chart.setOption({
        tooltip: {
          show: true
        },
        title: {
          text: '各网格亮灯率统计',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.lightRateByGrid.map(item => item.areaName)
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          barMaxWidth: 50, // 柱图宽度
          data: this.lightRateByGrid.map(item => item.lightRate),
          type: 'bar',
          tooltip: {
            formatter: '{b}<br/>亮灯率: {c}'
          }
        }]
      })
      chart.resize()
    },
    initChart5() {
      var chart = echarts.init(this.$refs.chart5)
      chart.setOption({
        tooltip: {
          show: true
        },
        title: {
          text: '各道路用电量统计',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.quantityByStreet.map(item => item.streetName)
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: this.quantityByStreet.map(item => item.quantity),
          type: 'line',
          tooltip: {
            formatter: '{b}<br/>用电量: {c}度'
          }
        }]
      })
      chart.resize()
    },
    initChart6() {
      var chart = echarts.init(this.$refs.chart6)
      chart.setOption({
        tooltip: {
          show: true
        },
        title: {
          text: '各网格用电量统计',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.quantityByGrid.map(item => item.areaName)
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: this.quantityByGrid.map(item => item.quantity),
          type: 'line',
          tooltip: {
            formatter: '{b}<br/>用电量: {c}度'
          }
        }]
      })
      chart.resize()
    },
    initChart7() {
      var chart = echarts.init(this.$refs.chart7)
      chart.setOption({
        tooltip: {
          show: true
        },
        title: {
          text: '各街道节能率统计',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.saveRateByStreet.map(item => item.streetName)
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: this.saveRateByStreet.map(item => item.saveRate),
          type: 'bar',
          tooltip: {
            formatter: '{b}<br/>节能率: {c}'
          }
        }]
      })
      chart.resize()
    },
    initChart8() {
      var chart = echarts.init(this.$refs.chart8)
      chart.setOption({
        tooltip: {
          show: true
        },
        title: {
          text: '各网格节能率统计',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.saveRateByGrid.map(item => item.areaName)
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: this.saveRateByGrid.map(item => item.saveRate),
          type: 'bar',
          tooltip: {
            formatter: '{b}<br/>节能率: {c}'
          }
        }]
      })
      chart.resize()
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    clearInput() {
      this.listQuery = {
        keyword: '',
        groupId: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }
      this.timeRange = []
      this.fetchData(false)
    },
    // 序号计算
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .app-container{
    margin-bottom:20px
  }
  .table{
    margin-bottom: 20px;
  }
  .small-row-class{
    height: 15px;
  }
  .pagination-container{
    margin-bottom: 50px;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 15px;
      i{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
  .edit_btns{
    .edit_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
</style>
<style rel="stylesheet/scss" lang="scss">
  .small-row-class td{
    padding:2px 0px;
  }
  .chart-body{
    text-align: center;
    height: 40vh;
    width: 100%;
    /*margin-right: auto;*/
    /*margin-left: auto;*/
  }
</style>
