<template>
  <app-container>
    <!--筛选条件-->
    <search-area size="small" @search="search">
      <search-item>
        <el-input v-model.trim="listQuery.keywords" size="small" placeholder="点位编号/设备编号" clearable />
      </search-item>
      <search-item>
        <dept-select
          v-model="listQuery.deptid"
          :need-top="false"
          dept-type="03"
          size="small"
          placeholder="选择权属单位"
        />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="timeRange"
          size="small"
          type="datetimerange"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </search-item>
    </search-area>
    <!--查询结果Table显示-->
    <normal-table
      ref="normalTable"
      :data="list"
      :total="total"
      :query="listQuery"
      :list-loading="listLoading"
      :options="options"
      size="small"
      @change="changePage"
    >
      <template slot="btns">
        <el-button v-if="hasPerm('/tempdata/export')" size="small" @click="batchExport">
          导出记录
        </el-button>
      </template>
      <template slot="columns">
        <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 v-if="column.checkCell" :class="checkCell(scope.row.cell)">{{ scope.row[column.value] }}</span>
            <span v-else>{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
      </template>
    </normal-table>
  </app-container>
</template>

<script>
import { getTempData, batchExportTempData } from '@/api/data/data'
import DeptSelect from '../../../components/DeptSelect/index'
import { parseUrl } from '@/utils/parseutils'
import { getSearchLastWeekTime } from '@/utils/dateutils'
import { exportFile } from '@/utils/exportutils'

export default {
  name: 'ListTempData',
  components: { DeptSelect },
  props: {
    type: {
      type: String,
      required: true
    }, // 设备类型
    name: {
      type: String,
      default: ''
    } // 设备名称
  },
  data() {
    return {
      listQuery: {
        keywords: '', // 关键字
        deptid: '', // 部门id
        beginTime: '', // 开始时间
        endTime: '', // 结束时间
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      timeRange: [], // 时间范围
      columns: [
        { text: '设备编号', value: 'devcode', align: 'center' },
        { text: '点位编号', value: 'wellCode', align: 'center' },
        { text: '设备类型', value: 'deviceTypeName', align: 'center' },
        { text: '温度(℃)', value: 'temperature', align: 'center' },
        { text: '湿度(%RH)', value: 'humidity', align: 'center' },
        { text: '电量(%)', value: 'cell', width: 80, align: 'center', checkCell: true },
        { text: '权属单位', value: 'deptName', align: 'center' },
        { text: '上传时间', value: 'uptime', width: 170, align: 'center' }
      ], // 显示列
      // multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      total: 0, // 数据总数
      options: {
        needIndex: true, // 是否需要序号列
        border: true // 是否需要上方边框
      },
      listLoading: true, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      editShow: false // 是否显示编辑框
    }
  },
  mounted() {
    // 如果路径里带参数，解析devcode参数
    this.timeRange = getSearchLastWeekTime()
    if (window.location.href) {
      const params = parseUrl(window.location.href)
      if (params && params.deviceType === this.type && params.devcode) {
        this.listQuery.keywords = params.devcode
        this.fetchData()
      }
    }
  },
  activated() {
    // 要是路由里有参数
    if (this.$route.query && this.$route.query.devcode) {
      this.listQuery.keywords = this.$route.query.devcode
      this.fetchData(false)// 获取数据
    } else {
      this.fetchData()
    }
  },
  methods: {
    // 批量导出
    batchExport() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '数据处理中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      batchExportTempData(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        let fileName = `${this.name}数据-${this.listQuery.beginTime}-${this.listQuery.endTime}.xlsx`
        fileName = fileName.replace(/-|:| /g, '')
        const blob = new Blob([res.data])
        exportFile(blob, fileName)
      }).catch((res) => {
        loading.close()
      })
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取温湿度配置数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      this.handleDateTime()
      getTempData(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        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 = ''
      }
    },
    checkCell(value) {
      var cell = parseFloat(value)
      if (cell < 10) {
        return 'warning-state'
      }
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .warning-state{
    color:red
  }
</style>
