<template>
  <el-dialog title="所属建筑楼层" :visible.sync="connectDialogVis" width="50%" id="messageDialog">
    <div class="cascader-row">
      <div style="line-height:32px;">添加{{system.SysLocalName}}所属建筑楼层 :</div>
      <div style="width:70%">
        <bfCascader ref="bfCascader" :SysID="system.SysID"></bfCascader>
      </div>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="connectDialogVis=false">取消</el-button>
      <el-button size="small" type="primary" @click="save">确认</el-button>
    </span>
  </el-dialog>
</template>
<script>
import bfCascader from './buildfloorCascader'
import { sysRelateBuild, sysRelateFloor } from "@/api/scan/request";

export default {
  data() {
    return {
      buildName: '',
      connectDialogVis: false,
      system: {}
    }
  },
  components: {
    bfCascader
  },
  props: {
    isCreate: {
      default: false
    }
  },
  methods: {
    showDialog(system) {
      this.system = system;
      this.connectDialogVis = true;
      this.$nextTick(() => {
        this.$refs.bfCascader.getCascader()
        let arr = this.system.BuildingFloorInfoList || [];
        let value = []
        if (arr.length) {
          arr.map(t => {
            if (t.FloorID) {
              value.push([t.BuildID, t.FloorID]);
            } else {
              value.push([t.BuildID])
            }
          })
        }
        this.$refs.bfCascader.value = value
        console.log(value)
      })
    },
    save() {
      // 如果是创建
      if (this.isCreate) {
        let data = this.$refs.bfCascader.getSelectedNodes();
        this.connectDialogVis = false;
        this.$emit('relateSuccess', data);
        return
      }
      let arr = this.$refs.bfCascader.value;
      let buildIds = [], floorIds = [];
      arr.map(t => {
        if (buildIds.indexOf(t[0]) < 0) {
          buildIds.push(t[0]);
        }
        if (t.length > 1 && floorIds.indexOf(t[1]) < 0) {
          floorIds.push(t[1]);
        }
      })
      let buildPa = {
        SysId: this.system.SysID,
        BuildingIdList: buildIds
      }
      let floorPa = {
        SysId: this.system.SysID,
        FloorIdList: floorIds
      }
      let promise1 = new Promise((resolve, reject) => {
        sysRelateBuild(buildPa, res => {
          resolve(res);
        })
      })
      let primise2 = new Promise((resolve, reject) => {
        sysRelateFloor(floorPa, res => {
          resolve(res);
        })
      })
      let buildFloor = this.$refs.bfCascader.getSelectedNodes();
      Promise.all([promise1, primise2]).then(res => {
        this.connectDialogVis = false;
        if (buildIds.length || floorIds.length) {
          this.$message.success("关联成功");
        } else {
          this.$message.success('清除关联成功');
        }
        this.$emit('relateSuccess', buildFloor);
      })
    }
  }
}
</script>
<style lang="less" scoped>
#messageDialog {
  .cascader-row {
    max-height: 200px;
    overflow: auto;
  }

  /*.el-row {*/
  /*  !*height: 50px;*!*/
  /*  max-height: 200px;*/
  /*  overflow-y: auto;*/
  /*  overflow-x: hidden;*/
  /*}*/
  /*.el-row > div {*/
  /*  float: left;*/
  /*}*/
  /*.el-row > div + div {*/
  /*  margin-left: 10px;*/
  /*}*/
  /*/deep/ .el-input__inner {*/
  /*  vertical-align: baseline;*/
  /*}*/
}
</style>
