<template>
  <div class="dialog-data-container">
    <!--标题及查询条件-->
    <div class="title-header">
      <div class="title-left">
        <i class="el-icon-menu" />设备数据情况
      </div>
      <div class="title-right">
        <time-buttons ref="timeButtons" @change="changeTime" />
        <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="fetchData"
        />
      </div>
    </div>
    <el-row :gutter="10">
      <el-col :span="24">
        <normal-table
          ref="normalTable"
          :head="headConfig"
          :data="list"
          :height="350"
          :query="listQuery"
          :columns="columns"
          :pagination="false"
          :list-loading="listLoading"
          :options="options"
          size="small"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getHydrantDataSingle } from '@/api/data/data'
import TimeButtons from '@/components/BigData/TimeButtons'
export default {
  name: 'SingleHydrantData',
  components: { TimeButtons },
  props: {
    devcode: {
      type: String,
      require: true
    } // 设备编号
  },
  data() {
    return {
      timeRange: [], // 时间范围
      listQuery: {
        devcode: '',
        beginTime: '',
        endTime: ''
      }, // 查询条件
      options: {
        needIndex: false
      }, // 是否需要序号列
      columns: [
        { text: '上报时间', value: 'logtime', align: 'center' },
        { text: '消防栓状态', value: 'descn', align: 'center' }
      ], // 显示列
      headConfig: {
        show: false
      }, // 表格头配置
      list: [], // 列表数据
      listLoading: false,
      pickerOptions: {
        disabledDate(time) { // 限制只能选100天内的时间
          const curDate = (new Date()).getTime()
          const three = 100 * 24 * 3600 * 1000
          const threeMonths = curDate - three
          return time.getTime() > Date.now() || time.getTime() < threeMonths
        }
      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    init() {
      this.$refs.timeButtons.initTime()
    },
    // 切换时间的时候
    changeTime(timeRange) {
      console.log('changeTime')
      this.timeRange = timeRange
      this.fetchData()
    },
    // 获取设备数据
    fetchData() {
      this.listLoading = true
      this.listQuery.devcode = this.devcode
      this.handleDateTime()
      getHydrantDataSingle(this.listQuery).then(response => {
        const data = response.data.map(item => {
          return { ...item, statusName: item.status === '1' ? '开盖' : (item.status === '2' ? '出水' : '心跳') }
        })
        this.list = data
        this.listLoading = false
      }).catch(_ => this.listLoading = false)
    },
    // 处理时间
    handleDateTime() {
      if (this.timeRange && this.timeRange.length > 0) {
        this.listQuery.beginTime = this.timeRange[0]
        this.listQuery.endTime = this.timeRange[1]
      } else {
        this.listQuery.beginTime = ''
        this.listQuery.endTime = ''
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.dialog-data-container{
  padding: 15px 0px;
  .title-header {
    display: flex;
    justify-content: space-between;
    height: 50px;
    padding: 10px;
    margin-bottom: 10px;
    border-top: solid 2px #F3F3F3;
    color: #606266;
    .title-left{
      //padding: 10px;
      line-height: 30px;
    }
  }
}
</style>
