<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="space_deviceList"
      :default-sort="{prop: 'createTime', order: 'descending'}"
      @sort-change="sortChange"
    >
      <el-table-column type="index" label="序号" align="center" :index="indexFormatter" width="80px"></el-table-column>
      <el-table-column label="设备名称" align="left" width="200px" prop="deviceName" />
      <el-table-column label="所属型号" align="left" prop="modelName" />
      <el-table-column label="设备key" align="left" prop="deviceKey" />
      <el-table-column label="设备类型" align="left" width="120px" prop="deviceTypeName" />
      <el-table-column label="创建时间" sortable="custom" align="center" width="200" prop="createTime" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改空间设备对话框 -->
  </div>
</template>

<script>
import {
  listSpace_device
} from "@/api/tenant/project";

export default {
  name: "SpaceDeviceWrap",
  props: ["sourceId", "projectId"],
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 空间设备表格数据
      space_deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        spaceId: "",
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "createTime",
        isAsc: "desc"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    sortChange(column) {
      const sort = {
        isAsc: column.order === "descending" ? "desc" : "asc",
        orderByColumn: column.prop
      };
      this.queryParams = Object.assign(this.queryParams, sort);
      this.handleQuery();
    },
    indexFormatter(val) {
      return (
        val + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      );
    },

    /** 查询空间设备列表 */
    getList() {
      this.loading = true;
      this.queryParams.spaceId = this.sourceId;
      listSpace_device(this.queryParams).then(response => {
        this.space_deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.spaceId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有空间设备数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportSpace_device(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    }
  }
};
</script>
