<template>
  <el-dialog title="提示" :visible.sync="dialogVisible" :width="dialogWidth" @close="handleClose" id="delRelation">
    <span>确定要清除对应关系？</span>
    <div v-loading="loading">
      <div v-show="ltableData.length>1">
        <div style="margin:10px 0;">有多个设备标识对应了该设备实例，是否一并清除对应关系？</div>
        <el-row class="bgf5">
          <el-col :span="18">
            <el-table :data="ltableData" style="width: 100%;max-height:400px;" height="calc(100% - 160px)" @selection-change="changeSelection"
              ref="ltable">
              <el-table-column label="设备标识" align="right" class-name="bgfff">
                <el-table-column label="数据源" prop="Datasource" align="right" class-name="bgfff" show-overflow-tooltip></el-table-column>
                <el-table-column label="位置标签" prop="LocationFlag" align="right" class-name="bgfff" show-overflow-tooltip>
                  <template slot-scope="scope">{{ scope.row.LocationFlag.toString() }}</template>
                </el-table-column>
                <el-table-column label="设备标识" prop="EquipmentMark" align="right" class-name="bgf5" show-overflow-tooltip></el-table-column>
                <el-table-column type="selection" class-name="bgf5"></el-table-column>
              </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="6">
            <div class="singleDir pl-10 font-default">设备实例</div>
            <div class="pl-10 font-default">{{rtableData[0].EquipLocalName}}</div>
            <div class="pl-10 font-default">{{rtableData[0].BuildLocalName}}-{{rtableData[0].FloorLocalName}}</div>
            <div class="pl-10 font-default">{{rtableData[0].RoomLocalName}}</div>
          </el-col>
        </el-row>
      </div>
      <div v-show="rtableData.length>1">
        <div style="margin:10px 0;">有多个设备实例对应了该设备标识，是否一并清除对应关系？</div>
        <el-row class="bgf5">
          <el-col :span="6">
            <div class="singleDir pr-10 font-default tr">设备标识</div>
            <div class="pr-10 font-default tr">{{ltableData[0].EquipmentMark}}</div>
            <div class="pr-10 font-default tr">{{ltableData[0].LocationFlag.toString()}}</div>
            <div class="pr-10 font-default tr">{{ltableData[0].Datasource}}</div>
          </el-col>
          <el-col :span="18">
            <el-table :data="rtableData" style="width: 100%;max-height:400px;" height="calc(100% - 160px)" @selection-change="changeSelection"
              ref="rtable">
              <el-table-column label="设备实例" class-name="bgfff">
                <el-table-column type="selection" class-name="bgf5"></el-table-column>
                <el-table-column label="设备实例名称" prop="EquipLocalName" class-name="bgf5" show-overflow-tooltip></el-table-column>
                <el-table-column label="设备实例编码" prop="ObjectID" class-name="bgfff" show-overflow-tooltip></el-table-column>
                <el-table-column label="所在建筑楼层" prop="FloorLocalName" class-name="bgfff" show-overflow-tooltip> </el-table-column>
                <el-table-column label="所在业务空间" prop="RoomLocalName" class-name="bgfff" show-overflow-tooltip></el-table-column>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取消</el-button>
      <el-button size="small" type="primary" v-show="1" v-loading="loading" @click="handleDelete">确定</el-button>
      <el-button size="small" type="primary" v-show="0" v-loading="loading">清除已选的对应规则</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { dynamicDeleteRelation, dynamicQueryPrompt } from "@/api/scan/request";
export default {
  data() {
    return {
      dialogVisible: false, //弹窗显示与隐藏
      ltableData: [], // 左侧数据
      rtableData: [], // 右侧数据
      activeName: "first", //当前所在tab页
      loading: false, //loading状态
      dialogWidth: "20%", //弹窗宽度
      selection: []
    };
  },
  methods: {
    //弹窗显示
    showDialog(row) {
      this.loading = true;
      this.dialogVisible = true;
      //需先用对象请求接口返回所需对象
      this.getDynamicQueryPrompt(row)
    },
    handleClick() { },
    handleClose(done) {
      this.dialogVisible = false;
    },
    //确认清除对应关系
    handleDelete(i, t) {
      console.log(i);
      console.log(t);
      //param.objs and param.point .length>1
      dynamicDeleteRelation(this.param, res => {
        console.log(res);
        this.$emit("refresh");
        this.dialogVisible = false;
      });
    },
    //表格中选择事件
    changeSelection() { },
    //查询两边数据,同时拼删除param
    getDynamicQueryPrompt(row) {
      let p1 = {
        Filters: `TypeCode='${row.TypeCode}';SubTypeCode='${row.SubTypeCode}';EquipmentMark='${row.EquipmentMark}'`,
        PageNumber: 1,
        PageSize: 500
      }
      //通过标识查询设备实例
      let promise1 = new Promise((resolve, reject) => {
        dynamicQueryPrompt(p1, res => {
          resolve(res)
        })
      })
      let p2 = {
        Filters: `ObjectID='${row.ObjectID}'`,
        PageNumber: 1,
        PageSize: 500
      }
      let promise2 = new Promise((resolve, reject) => {
        dynamicQueryPrompt(p2, res => {
          resolve(res)
        })
      })
      Promise.all([promise1, promise2]).then(res => {
        console.log(res)
        this.ltableData = res[1].Content
        this.rtableData = res[0].Content
        if (this.ltableData.length > 1 || this.rtableData.length > 1) {
          this.dialogWidth = "60%";
        }
        this.$nextTick(() => {
          this.toggleSelection(row);
        })
        this.loading = false;
      })
    },
    //设置默认选中
    toggleSelection(row) {
      console.log(row)
      this.ltableData.length > this.rtableData.length ?
        this.$refs.ltable.toggleRowSelection(row) : this.$refs.rtable.toggleRowSelection(row)
    }
  },
  mounted() { },
  created() { }
};
</script>
<style lang="scss" scoped>
#delRelation {
  /deep/ .bgf5 {
    background-color: #f5f7fa;
  }
  /deep/ .bgfff {
    background-color: #fff;
  }
  div.singleDir {
    font-weight: 800;
    color: #606266;
    border-bottom: 1px solid #ebeef5;
    background: #fff;
    border-top: 1px solid #ebeef5;
  }
  .pl-10 {
    padding-left: 10px;
  }
  .pr-10 {
    padding-right: 10px;
  }
  .font-default {
    line-height: 42px;
    font-size: 12px;
  }
  .tr {
    text-align: right;
  }
}
</style>