<template>
  <div class="manage-container">
    <vab-query-form>
      <el-button type="primary" @click="handleAdd()">添加传感器</el-button>
    </vab-query-form>
    <el-table :data="sensorData" border stripe style="width: 100%">
      <el-table-column
        prop="id"
        label="ID"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="传感器名字"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="attribute.dataValue"
        label="类型"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="addressId"
        label="地址"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="offset"
        label="寄存器号"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" :label="$t('defalult.cz')" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ $t('defalult.bj') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </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>
  </div>
</template>
<script>
import { doDelete, getList } from '@/api/sensor';
export default {
  name: 'Index',
  components: {},
  data() {
    return {
      sensorData: [],
      deviceId: '',
      projectId: '',
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        deviceId: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    const param = this.$route.params.deviceId;
    const projectId = this.$route.params.projectId;
    const deviceIds = localStorage.getItem('deviceId');
    const deviceId = param === undefined ? deviceIds : param;
    if (deviceId !== '') {
      this.deviceId = deviceId;
      this.queryForm.deviceId = deviceId;
      this.projectId = projectId;
      localStorage.setItem('deviceId', deviceId);
      this.fetchData();
    }
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      console.log(data);
      this.sensorData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    handleAdd() {
      this.$refs['edit'].showEdit(this.deviceId, 0);
    },
    handleEdit(row) {
      row.deviceId = this.deviceId;
      this.$refs['edit'].showEdit(row, 1);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
    handleDelete(row) {
      const that = this;
      if (row.id) {
        this.$confirm('你确定要删除当前项吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const { msg } = doDelete({ sensorIds: row.id });
            this.$message({
              type: 'success',
              message: msg == undefined ? '删除成功' : msg,
            });
            setTimeout(function () {
              that.fetchData();
            }, 1000);
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            });
          });
      }
    },
  },
};
</script>

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