<template>
  <el-row :gutter="10">
    <el-col>
      <el-date-picker
        v-model="timeRange"
        type="datetimerange"
        range-separator="至"
        size="small"
        :picker-options="pickerOptions"
        value-format="yyyy-MM-dd HH:mm:ss"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        @change="fetchDeviceType"
      />
    </el-col>
    <el-col :span="12">
      <ve-line
        :data="chartData"
        :settings="chartSettings"
      />
    </el-col>
    <el-col :span="12">
      <normal-table
        ref="normalTable"
        :data="list"
        :query="listQuery"
        :columns="columns"
        :list-loading="listLoading"
        :options="options"
        size="small"
      />
    </el-col>
  </el-row>
</template>

<script>
import { tempdataList } from '@/api/device/deviceTypeDetail'
export default {
  name: 'ListTempData',
  data() {
    return {
      timeRange: '',
      listQuery: {
        devcode: '',
        beginTime: '',
        endTime: ''
      },
      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])
          }
        }]
      },
      // 是否需要序号列
      options: {
        needIndex: false
      },
      columns: [
        {
          text: '温度',
          value: 'temperature',
          align: 'center'
        },
        {
          text: '湿度',
          value: 'humidity',
          align: 'center'
        },
        {
          text: '上报时间',
          value: 'uptime',
          align: 'center'
        }
      ], // 显示列
      list: [],
      listLoading: false,
      chartSettings: {
        labelMap: {
          'temperature': '温度',
          'humidity': '湿度'
        },
        metrics: ['temperature', 'humidity'],
        dimension: ['uptime']
      },
      chartData: {
        columns: ['uptime', 'temperature', 'humidity'],
        rows: []
      }
    }
  },
  created() {
    this.fetchDeviceType()
  },
  mounted() {
    this.$refs.normalTable.initColumnsState(false)
  },
  methods: {
    initData: function(row = null) {
      this.listQuery.devcode = row.devcode
    },
    fetchDeviceType(val) {
      this.listLoading = true
      if (val !== undefined) {
        this.listQuery.startTime = val[0]
        this.listQuery.endTime = val[1]
      }
      tempdataList(this.listQuery).then(response => {
        this.list = response.data
        this.chartData.rows = response.data
        this.listLoading = false
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-date-editor--datetimerange.el-input,
.el-date-editor--datetimerange.el-input__inner{
  width: 340px;
}
</style>
