<template>
  <!-- 有毒气体监测-监测数据 -->
  <div class="fullHeight">
    <div class="searchBox whiteBlock">
      <el-form :inline="true" size="medium" class="demo-form-inline">
        <el-form-item label="上报时间">
          <el-date-picker
            v-model="time"
            type="daterange"
            value-format="yyyy-MM-dd"
            :range-separator="$t('message.lifter.to')"
            :start-placeholder="$t('message.lifter.startDate')"
            :end-placeholder="$t('message.lifter.deadline')"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="设备名称">
          <el-select v-model="devName" placeholder="请选择">
            <el-option
              :label="item.devName"
              :value="item.id"
              v-for="(item, index) in towerList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit" plain>查询</el-button>
          <el-button type="warning" plain @click="refreshBtn">刷新</el-button>
          <el-button type="primary" @click="exportBtn">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table_wrap whiteBlock">
      <el-table
        :data="tableData"
        class="tables"
        style="width: 100%"
        height="600px"
      >
        <el-table-column prop="devName" align="center" label="设备名称">
        </el-table-column>
        <el-table-column prop="location" align="center" label="安装位置">
        </el-table-column>
        <el-table-column prop="uploadTime" align="center" label="上报时间">
        </el-table-column>
        <el-table-column prop="gasType" align="center" label="气体类型">
          <template slot-scope="scope">{{ scope.row.gasType==1?'氧气':scope.row.gasType==2?'甲烷':'一氧化碳'}}</template>
        </el-table-column>
        <el-table-column prop="gasVal" align="center" label="监测值">
          <template slot-scope="scope">{{ scope.row.gasType==1?scope.row.gasVal+'(%VOL)':scope.row.gasType==2?scope.row.gasVal+'(%LEL)':scope.row.gasVal+'(ppm)'}}</template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 136px"
        class="pagerBox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-sizes="$store.state.PAGESIZRS"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next"
        :total="Number(total)"
        background
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import {
  getPoisonousGasPageApi,//分页
  getfpdcdListApi,//设备名称下拉
} from "@/assets/js/api/edgeProtection";
export default {
  data() {
    return {
      projectSn: "",
      towerList: [],
      devName: "",
      time: ['', ''],
      pageNo: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
    };
  },
  created() {
    this.projectSn = this.$store.state.projectSn;
    this.getTowerList();
    this.getListData();
  },
  methods: {
    //获取设备下拉
    getTowerList() {
      let data = {
        projectSn: this.projectSn,
      };
      getfpdcdListApi(data).then((res) => {
        if (res.code == 200) {
          this.towerList = res.result;
        }
      });
    },
    //查询数据
    getListData() {
      let data = {
        queryStartTime: this.time ? this.time[0] : '',
        queryEndTime: this.time ? this.time[1] : '',
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        projectSn: this.projectSn,
        devName: this.devName,
      };
      getPoisonousGasPageApi(data).then((res) => {
        if (res.code == 200) {
          this.tableData = res.result.records;
          this.total = res.result.total;
        }
      });
    },

    //导出
    exportBtn() {
      fetch(this.$http.defaults.baseURL + '/xmgl/poisonousGasDevCurrentData/exportXls?&projectSn=' + this.projectSn + '&queryEndTime=' + this.time[1] + '&queryStartTime=' + this.time[0] + '&devName=' + this.devName, {
        headers: {
          'Authorization': this.$store.state.userInfo.token
        }
      })
        .then(response => {
          // 处理响应
          if (!response.ok) {
            throw new Error('导出失败');
          }
          return response.blob();
        })
        .then(blob => {
          console.log('导出成功');
          // 创建一个下载链接
          const url = window.URL.createObjectURL(blob);
          // 创建一个<a>元素
          const link = document.createElement('a');
          link.href = url;
          link.download = '导出文件.xlsx'; // 指定下载文件的文件名
          // 模拟点击下载链接
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          // 释放URL对象
          window.URL.revokeObjectURL(url);
          // 处理导出的文件
          // 这里可以使用blob对象来获取导出的文件内容或者将其保存到本地
        })
        .catch(error => {
          // 处理错误
          console.error(error);
        });

    },
    //查询数据
    onSubmit() {
      this.getListData();
    },

    //刷新
    refreshBtn() {
      this.time = [];
      this.pageNo = 1;
      this.pageSize = 10;
      this.devName = "";
      this.getListData();
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.getListData();
    },
    handleCurrentChange(val) {
      this.pageNo = val;
      this.getListData();
    },
  },
};
</script>

<style lang="less" scoped>
</style>