<template>
  <div class="manage-container">
    <el-table
      v-loading="listLoading"
      :data="data"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        prop="name"
        :label="$t('sensor.cgqmc')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="type"
        label="传感器类型"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dataKey"
        label="传输ID"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dataValue"
        label="传感器数值"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit"
        :label="$t('sensor.dw')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" :label="$t('defalult.cz')" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="historical(row)">历史数据</el-button>
        </template>
      </el-table-column>
    </el-table>
    <sensor-add ref="edit"></sensor-add>
  </div>
</template>
<script>
import { getLatestDataByDevice, exportData } from '@/api/device';
import ajax from '@/api/download';
import sensorAdd from '@/views/iot/sensor/add';
export default {
  components: {
    sensorAdd,
  },
  data() {
    return {
      sensorData: [],
      data: [],
      deviceCode: '',
      projectId: '',
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      deviceId: '',
      historyData: [],
      queryForm: {
        sensorName: '',
        deviceCode: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.queryForm.deviceCode = this.$route.query.deviceCode;
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const { data } = await getLatestDataByDevice(this.queryForm);
      this.data = data;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    async exportData() {
      const url = await exportData(this.queryForm);
      ajax.downloadFile(url, { fileName: '站点最新数据.xls' });
    },
    historical(row) {
      this.$router.push({
        path: '/showIotData',
        name: '站点历史数据',
        query: { dataKey: row.dataKey, deviceCode: this.queryForm.deviceCode },
      });
    },
    handleAdd() {
      this.$refs['edit'].showEdit(this.queryForm.deviceId, 0);
    },
    handleEdit(row) {
      row.deviceId = this.queryForm.deviceId;
      this.$refs['edit'].showEdit(row, 1);
    },
    search() {
      this.fetchData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-container {
  width: 100%;
  .vab-query-form {
    margin-bottom: 30px;
    .el-input {
      width: 180px;
      margin-right: 20px;
    }
  }
}
</style>

<style lang="scss">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
</style>
