<template>
  <div id="eqInSp">
    <el-row v-if="hidden">
      <el-button type="primary" @click="add">添加{{inSpaceType}}</el-button>
      <el-tooltip class="item" effect="dark" :content="`可前往“全部关系总览”中，按坐标计算${typeToTips[type]}`" placement="right">
        <el-button v-show="type=='Equipment'">按空间计算</el-button>
      </el-tooltip>
    </el-row>
    <div class="table-box">
      <el-table :data="tableData" style="width: 100%" height="100%" :header-cell-style="headerStyle" v-loading="loading">
        <el-table-column :label="`${inSpaceType}名称`" show-overflow-tooltip min-width="100">
          <template slot-scope="scope">
            <div>
              {{scope.row.EquipLocalName||scope.row.EquipName||''}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="EquipLocalID" :label="`${inSpaceType}本地编码`" show-overflow-tooltip min-width="100"></el-table-column>
        <el-table-column :label="`${inSpaceType}类`" show-overflow-tooltip min-width="100">
          <template slot-scope="scope">
            <div>
              {{scope.row.EquipCategory?scope.row.EquipCategory.EquipName:''}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="action" label="操作" min-width="100">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="删除关系" placement="left">
              <el-button size="mini" @click="handleDelete(scope.$index, scope.row)" type="danger" plain icon="el-icon-delete"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
        <template slot="empty">
          <div style="height: 60%;transform: translateY(50%);lineHeight:20px;">
            <i class="icon-wushuju iconfont"></i>
            {{type=='Equipment'?`可前往“全部关系总览”中，按坐标计算${typeToTips[type]}`:'无数据'}}
          </div>
        </template>
      </el-table>
    </div>
    <!-- 添加设备弹窗 -->
    <addEquipDialog ref="addEqDialog" :type="type" :spaceId="params.RoomID" :zone="params.zone" @refresh='getTableData'></addEquipDialog>
  </div>
</template>

<script>
import {
  zoneQuery,
  eqInSpaceDelete, //设备所在业务空间--删除关系
  eqForSpaceDelete, //设备服务业务空间--删除关系
} from "@/api/scan/request";
import { mapGetters } from "vuex";
import addEquipDialog from "@/components/business_space/newAddDialogs/addEquipDialog"
export default {
  components: {
    addEquipDialog
  },
  computed: {
    ...mapGetters("layout", ["projectId"])
  },
  data() {
    return {
      inSpaceType: '设备',
      headerStyle: {
        backgroundColor: '#e1e4e5',
        color: '#2b2b2b',
        lineHeight: '30px'
      }, // 列表样式
      loading: false, // loading
      tableData: [], //列表数据
      typeToTips: {
        Equipment: '空间内的设备',
        EquipmentFor: '服务于空间的设备',
      },
      hidden: true,
    };
  },
  props: {
    params: {},
    type: {}
  },
  created() {
    this.getTableData()
    this.changeContentStyle();
  },
  methods: {
    // 删除关系
    handleDelete(index, row) {
      this.$confirm("确认删除该关系？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let pa = {
          data: [row],
          type: this.params.zone
        }
        if (this.type == 'Equipment') {
          this.deleteEqInSpace(pa);
        } else {
          this.deleteEqForSpace(pa);
        }
      }).catch(() => {
        this.$message("取消删除")
      })
    },
    // 删除设备所在空间关系
    deleteEqInSpace(pa) {
      eqInSpaceDelete(pa, res => {
        this.$message.success('删除成功')
        this.getTableData()
      })
    },
    // 删除设备服务空间关系
    deleteEqForSpace(pa) {
      eqForSpaceDelete(pa, res => {
        this.$message.success('删除成功')
        this.getTableData()
      })
    },
    // 改变pagesize
    handleSizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getTableData();
    },
    // 改变pageno
    handleCurrentChange(pageNo) {
      this.page.pageNumber = pageNo;
      this.getTableData();
    },
    // 获取列表数据
    getTableData() {
      this.loading = true;
      let pa = {
        Filters: `RoomID='${this.params.RoomID}'`,
        Cascade: [
          {
            Name: this.type.replace(this.type[0], this.type[0].toLowerCase()),
            Cascade: [{ Name: 'equipCategory' }]
          }
        ],
        ZoneType: this.params.zone
      }
      zoneQuery(pa, res => {
        this.loading = false;
        this.tableData = res.Content[0][this.type] || []
        this.tableData.map(t => {
          t.SpaceId = res.Content[0].RoomID
          return t;
        })
      })
    },
    // 添加设备
    add() {
      this.$refs.addEqDialog.floor = this.params.buildFloorSelectd;
      this.$refs.addEqDialog.showDialog()
    },
    changeContentStyle() {
      this.$route.name == "spaceLedger" ? this.hidden = false : this.hidden = true;
    }
  },
  watch: {
    type() {
      this.getTableData()
    },
    params() {
      this.getTableData()
    }
  }
};
</script>
<style lang="less" scoped>
#eqInSp {
  height: 100%;
  .table-box {
    margin-top: 10px;
    height: calc(100% - 50px);
  }
}
</style>