<template>
  <el-table
    v-loading="listLoading"
    :data="sensorData"
    border
    stripe
    style="width: 100%"
  >
    <el-table-column
      prop="name"
      :label="$t('siteDetails.cgqmc')"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="dataKey"
      :label="$t('siteDetails.csid')"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="dataValue"
      :label="$t('siteDetails.zhi')"
      align="center"
    >
      <template #default="{ row }">
        <el-tag type="success">{{ row.dataValue }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column
      prop="unit"
      :label="$t('siteDetails.dw')"
      align="center"
    ></el-table-column>
    <el-table-column :label="$t('defalult.cz')" align="center">
      <template #default="{ row }">
        <el-button
          v-if="row.readWrite == $t('defalult.zx')"
          type="text"
          @click="distribute(row)"
        >
          {{ $t('siteDetails.xfzl') }}
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    :background="background"
    :current-page="queryForm.page"
    :layout="layout"
    :page-size="queryForm.size"
    :total="total"
    style="text-align: right"
    @current-change="handleCurrentChange"
    @size-change="handleSizeChange"
  ></el-pagination>
  <distribute ref="distribute"></distribute>
</template>

<script>
import { sensorsOfWrite } from '@/api/monitor';
import distribute from '@/views/server/siteDetails/distribute';
export default {
  components: {
    distribute,
  },
  data() {
    return {
      sensorData: [],
      lazy: true,
      activeName: 'first',
      layout: 'prev, pager, next',
      total: 0,
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 5,
        deviceCode: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  methods: {
    showData(row) {
      this.queryForm.deviceCode = row.deviceCode;
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await sensorsOfWrite(this.queryForm);
      this.sensorData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    distribute(row) {
      const dataKey = row.dataKey;
      const deviceCode = this.queryForm.deviceCode;
      this.$refs['distribute'].showEdit({
        deviceCode: deviceCode,
        dataKey: dataKey,
        unit: row.unit,
        convertFlag: row.convertFlag,
      });
    },
    handleQuery() {
      this.queryForm.pageNo = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.pageNo = val;
      this.fetchData();
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-button {
  margin-bottom: 30px;
}
</style>
