<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>
    <div ref="chart" class="chart-body" style="padding-top: 20px;"/>
    <!--查询结果Table显示-->
    <div style="margin-bottom: 50px">
      <el-row class="table-title">
        <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>数据列表</div></el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="list" :row-style="{height: '47px'}" class="table" border>
        <el-table-column :index="indexMethod" label="序号" width="55" align="center" type="index" />
        <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
import { getQuantityListByStreet, staticsQuantityByStreet } from '@/api/system/report'

export default {
  name: 'QuantityData',
  data() {
    return {
      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: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '道路名称',
          value: 'streetName',
          align: 'center'
        },
        {
          text: '用电量',
          value: 'quantity',
          align: 'center'
        }
      ],
      groupList: [],
      total: 0, // 数据总数
      list: [], // 报警列表
      chart: '',
      staticsList: [], // 报警列表
      listLoading: true // 列表加载
    }
  },
  watch: {
    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
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      console.log(this.listQuery)
      getQuantityListByStreet(this.listQuery).then(response => {
        if (response.code === 200) {
          this.list = response.data
          this.total = parseInt(response.data.total)
        } else {
          this.$message.error(response.message)
        }
        this.listLoading = false
      })
      staticsQuantityByStreet(this.listQuery).then(response => {
        if (response.code === 200) {
          this.staticsList = response.data
          this.initChart()
        } else {
          this.$message.error(response.message)
        }
      })
    },
    initChart() {
      this.chart = echarts.init(this.$refs.chart)
      this.chart.setOption({
        tooltip: {
          show: true
        },
        title: {
          text: '统计各道路用电量',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.staticsList.map(item => item.streetName)
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: this.staticsList.map(item => item.quantity),
          type: 'line',
          tooltip: {
            formatter: '{b}<br/>用电量: {c}'
          }
        }]
      })
      this.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: 50vh;
    width: 60%;
    margin-right: auto;
    margin-left: auto;
    margin-bottom: 30px;
  }
</style>
