<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="readWrite"
        :label="$t('sensor.dxcx')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dataKey"
        :label="$t('sensor.csid')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dataValue"
        :label="$t('sensor.cgqsz')"
        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
        prop="img"
        :label="$t('sensor.tp')"
        width="120"
        align="center"
      >
        <template #default="{ row }">
          <el-image
            v-if="row.img"
            style="width: 100px; height: 100px"
            :src="fileUrl + row.img"
            :preview-src-list="[fileUrl + row.img]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('defalult.cz')" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="historical(row)">
            {{ $t('sensor.lssj') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <vab-query-form>
      <vab-query-form-right-panel>
        <el-pagination
          :background="background"
          :current-page="queryForm.page"
          :layout="layout"
          :page-size="queryForm.size"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        ></el-pagination>
      </vab-query-form-right-panel>
    </vab-query-form>
    <sensor-add ref="edit"></sensor-add>
  </div>
</template>
<script>
import { exportData, getLatestData } from '@/api/device';
import ajax from '@/api/download';
import sensorAdd from '../sensor/add';
import { baseURL } from '@/config';
export default {
  components: {
    sensorAdd,
  },
  data() {
    return {
      fileUrl: baseURL + '/static/img/',
      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: '',
        page: 1,
        size: 10,
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.queryForm.deviceCode = this.$route.query.deviceCode;
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const { data } = await getLatestData(this.queryForm);
      this.data = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    async exportData() {
      const url = await exportData(this.queryForm);
      ajax.downloadFile(url, { fileName: '站点最新数据.xls' });
    },
    historical(row) {
      this.$router.push({
        path: '/showdata',
        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>
