<!--
  revit业务空间
 -->
<template>
  <div id="businessSpace">
    <!-- 空间属于多个建筑楼层 -->
    <roomInFloorDialog ref="roomInFloorDialog" @relaSuc="relaSuc"></roomInFloorDialog>
    <!--------- 页面头部   -------------->
    <div class="saga-title" v-show="!needCalculate">
      <div class="saga-build-mess">
        <floorCascader @change="changeCascader" ref="floorCascader"></floorCascader>
        <input type="hidden" v-model="zoneItemID">
      </div>
      <div class="saga-build-tab">
        <el-radio-group v-model="isMyTab" @change="changeRadio" style="width: 136px;">
          <el-radio-button label="1">平面图</el-radio-button>
          <el-radio-button label="2" class="space-own-radio" style="width: 68px;">列表</el-radio-button>
        </el-radio-group>
      </div>
      <div class="saga-build-tips" v-if="mutiCount>0" @click="getTips">有{{mutiCount}}个空间同时属于多个建筑楼层，点击处理</div>
    </div>
    <div class="saga-business-sapce-main" v-show="!needCalculate">
      <el-row class="spaceTypes">
        <div class="types">
          <el-tabs v-model="activeName" type='card' @tab-click="tabClick">
            <template v-for="(item,index) in tabsList">
              <el-tab-pane :name="item.Code" :key="index" :label="item.Name"></el-tab-pane>
            </template>
          </el-tabs>
        </div>
      </el-row>
      <div v-show="isMyTab == 1" style="width:100%;height:100%">
        <graphy @getSp="checkSpace" @dimension="checkSpace" @businessDetails="getBuinessDetails" ref="graphy" @copyID="copyID"></graphy>
      </div>
      <div v-show="isMyTab == 2">
        <handsontable-main @lookEqu="lookEqu" ref="handsontable" :zoneCode="activeName" :id="`handsontable${activeName}`"></handsontable-main>
      </div>
    </div>
    <div v-show="needCalculate" style="display:flex;align-items:center;justify-content:center;height:100%;background-color:#fff;">
      <div class="center" style="flex:1">
        <i class="iconfont icon-jianzhu" style="font-size:50px;"></i>
        <p style="margin:10px 0;">设备空间关系已产生变化，请请前往关系总览重新计算</p>
        <el-button @click="jumpToAllRelation">立即跳转</el-button>
      </div>
    </div>
  </div>
</template>
<script>
// import noModelDialog from "@/components/business_space/dialogs/noModelDialog";
// import influenceDialog from "@/components/business_space/dialogs/influenceDialog";
// import detailsDialog from "@/components/business_space/dialogs/detailsDialog";
// import notRelated from "@/components/business_space/dialogs/notRelated";
// import facilityDialog from "@/components/business_space/dialogs/facilityDialog";
// import graphy from "@/components/business_space/graphy/business";
import graphy from "@/components/business_space/newGraphy/graphy";
import roomInFloorDialog from "@/components/business_space/newAddDialogs/roomInFloorDialog";
import handsontableMain from "@/components/business_space/business/handsontable";
import {
  getDataDictionary,
  getAllbusiness,
  getSpaceFloor,
  buildingQuery, //数据中心-建筑查询
  queryDictionaryHead, //数据中心-字典头部信息查询接口
  getrelationTypeProject, //数据中心-当该项目中的"建筑体下的业务空间"“楼层下的业务空间”关系的状态为需更新时，业务空间台账功能不可用
  getSpaceBdFlCount, // 属于多建筑楼层的空间数据统计
} from "@/api/scan/request";
import tools from '@/utils/scan/tools'
import floorCascader from "@/components/ledger/lib/floorCascader";
import { mapGetters, mapActions } from "vuex";
export default {
  components: {
    graphy,
    handsontableMain,
    roomInFloorDialog,
    floorCascader,
  },
  computed: {
    ...mapGetters('layout', ['projectId', 'userId', 'secret'])
  },
  data() {
    return {
      floor: {
        influence: false, //受影响的业务空间弹窗
        details: false, //详情弹窗
        notRelated: false,
        facility: false
      },
      param: {
        ProjId: this.projectId, //项目id
        UserId: this.userId, //用户id
        secret: this.secret
      },
      buildMess: {
        name: "--",
        code: "",
        selectd: [],
        map: "",
        isChild: true,
        buildName: "--",
        buildCode: ""
      },
      isMyTab: 1,
      activeName: "0",
      tabsList: [],
      activeTabType: {
        "code": "GeneralZone",
        "name": "默认分区",
        "rel_type": "99"
      },
      options: [], //建筑楼层级联下拉
      props: { //自定义字段
        value: "BuildID",
        label: "BuildLocalName",
        children: "Floor"
      },
      floorToMap: {}, //楼层及其对应的模型图
      FloorMap: "", //当前选中的楼层对象
      zoneItemID: '',
      buildFloorSelectd: [''], //当前所选的建筑楼层
      childBackParam: {},
      needCalculate: false, // 关系是否需要计算
      mutiCount: 0
    };
  },
  created() {
    // this.init();
    this.getRelations()
    this.childBackParam = this.$route.params;
  },
  mounted() { },
  methods: {
    // 初始化
    init() {
      this.getTypes()
      this.getBuilding()
      this.getMutiCount()
    },
    // 获取项目下建筑
    getBuilding() {
      let pa = {
        Cascade: [{ name: 'floor', Orders: 'SequenceId desc' }],
        Orders: "BuildLocalName asc",
      }
      buildingQuery(pa, res => {
        this.options = res.Content.map(t => {
          if (t.Floor) {
            t.Floor = t.Floor.map(item => {
              item.BuildID = item.FloorID;
              item.BuildLocalName = item.FloorLocalName;
              this.floorToMap[item.FloorID] = item;
              return item;
            })
          } else {
            t.Floor = []
          }
          return t;
        })
        if (this.childBackParam.zone) {
          this.changeCascader(this.buildFloorSelectd);
          this.$refs.floorCascader.setValue(this.buildFloorSelectd)
        } else {
          this.$refs.floorCascader.setValue([''])
        }
      })
    },
    // 切换平面图和列表
    changeRadio(val) {
      this.changeGraphyOrTable(val)
    },
    // 获取tabs的列表
    getTypes() {
      let pa = {
        Filters: `parentId = 'Space'`
      }
      queryDictionaryHead(pa, res => {
        this.tabsList = res.Content.map(t => {
          if (t.Name == "元空间") {
            return undefined;
          }
          return t;
        }).filter(item => item);
        if (this.childBackParam.zone) {
          this.activeName = this.childBackParam.zone;
          this.buildFloorSelectd = this.childBackParam.buildFloorSelectd;
          this.isMyTab = this.childBackParam.isMyTab;
        } else {
          this.activeName = 'GeneralZone';
        }
      })
    },
    // 获取多建筑楼层统计
    getMutiCount() {
      getSpaceBdFlCount({}, res => {
        this.mutiCount = res.Count;
        if (this.mutiCount > 0) {
          this.getTips();
        }
      })
    },
    // 获取关系是否需要计算
    getRelations() {
      let pa = {
        Filters: `not ZoneType isNull;ComputationalState>1`
      }
      getrelationTypeProject(pa, res => {
        if (res.Content.length) {
          this.needCalculate = true;
        } else {
          this.needCalculate = false;
          this.init()
        }
      })
    },
    // 通过选中的tab-Code 获取选中的tab的所有数据
    getItemForType() {
      let data = {}
      this.tabsList.map(item => {
        if (item.Code == this.activeName) {
          data.code = item.Code;
          data.name = item.Name;
        }
      })
      return data
    },
    // 点击tab
    tabClick(val) {
      this.changeGraphyOrTable(this.isMyTab)
    },
    //修改所选建筑楼层
    changeCascader(val) {
      this.buildFloorSelectd = val;
      // 如果选择的是全部或者未明确
      if (val.indexOf('noKnow') > -1 || val.indexOf('all') > -1) {
        this.isMyTab = 2;
        this.FloorMap = {}
      } else {
        // 选择的是具体某个楼层
        if (!this.floorToMap[val[1]].StructureInfo || !this.floorToMap[val[1]].StructureInfo.FloorMap) {
          this.isMyTab = 2;
        }
        this.FloorMap = this.floorToMap[val[1]];
      }
      this.changeGraphyOrTable(this.isMyTab)
    },
    // 判断切换至图还是表
    changeGraphyOrTable(num) {
      this.activeTabType = this.getItemForType()
      if (!this.buildFloorSelectd[0]) {
        this.$message.warning('请选择建筑楼层')
        return
      };
      if (num == 2) {
        this.initMessage();
      } else {
        this.$refs.graphy.getData(this.buildFloorSelectd, this.FloorMap, this.activeTabType);
      }
    },
    // 触发获取表头-获取初始化信息点
    initMessage() {
      let params = {
        data: {
          Orders: "sort asc",
          PageNumber: 1,
          PageSize: 500
        },
        type: this.activeName
      }
      getDataDictionary(params, res => {
        this.$refs.handsontable.getHeader(res.Content, this.buildFloorSelectd);
      })
    },
    checkSpace(list, build, graphyId) {
      this.floor.notRelated = true;
      this.$refs.notRelated.getIdList(list, build, graphyId, this.activeTabType);
      this.$refs["graphy"].getHasSpace();
    },
    changeShow() {
      this.floor.dialogVisible = true;
      console.log(this.buildMess, 'buildMess')
    },
    //成功关联平面图
    createSuccess() {
      this.$refs["graphy"].getHasSpace();
      this.$refs["graphy"].clearDimension();
      this.floor.notRelated = false;
    },
    //成功删除业务空间
    delSuccess() {
      this.floor.details = false;
      this.$refs["graphy"].getHasSpace();
      this.$refs["graphy"].clearDimension();
    },
    //查看业务空间详情
    getBuinessDetails(list) {
      this.floor.details = true;
      this.$refs.details.getData(list, this.buildMess);
    },
    lookEqu(infos) {
      infos.id = infos.RoomID
      infos.name = infos.RoomLocalName
      this.floor.facility = true
      this.$refs.facility.getData(infos, this.buildMess)
    },
    copyID(val){
      this.$refs.handsontable.zoneItemID = val;
    },
    // 空间属于多个建筑楼层
    getTips() {
      this.$refs.roomInFloorDialog.showDialog();
    },
    // 跳转至关系总览
    jumpToAllRelation() {
      this.$router.push({ name: 'overView' })
    },
    relaSuc() {
      this.init();
    }
  },
  watch: {
    floor: {
      deep: true,
      handler: function () {
        if (this.floor.notRelated) {
          this.$refs["graphy"].clearDimension();
        }
      }
    },
    projectId() {
      this.init()
    }
  }
};
</script>

<style lang="less">
#businessSpace {
  position: relative;
  display: flex;
  flex-direction: column;
  .space-own-radio {
    .el-radio-button__inner {
      padding: 9px 21px;
    }
  }
  .el-dialog__body {
    max-height: 600px;
    overflow-y: auto;
  }
  .saga-title {
    height: 54px;
    background-color: #fff;
    font-size: 14px;
    position: relative;
    line-height: 54px;
    margin-bottom: 10px;
    border: 1px solid #dfe6ec;
    .saga-build-mess {
      // position: absolute;
      // left: 10px;
      width: 500px;
      margin-left: 10px;
      i {
        padding-right: 5px;
      }
      p {
        float: left;
      }
      .saga-btn {
        float: left;
        height: 40px;
        line-height: 40px;
        padding: 0 10px;
      }
    }
    .saga-build-tab {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      .tab-main {
        float: left;
        width: 120px;
        padding: 0 5px;
        margin: 5px 0;
        border: 1px solid #ccc;
        background-color: #fff;
        height: 30px;
        box-sizing: border-box;
        text-align: center;
        cursor: pointer;
        overflow: hidden;
        i {
          font-size: 18px;
          padding-right: 10px;
          float: left;
          line-height: 30px;
          margin-left: 10px;
        }
        span {
          line-height: 30px;
          float: left;
        }
      }
      .tab-active {
        background-color: #409eff;
        color: #fff;
      }
    }
    .saga-build-tips {
      float: right;
      margin-right: 10px;
      font-size: 12px;
      color: #409eff;
      cursor: pointer;
    }
  }
  .saga-business-sapce-main {
    overflow: hidden;
    height: 100%;
    background: #fff;
    padding: 10px;
    /deep/ .el-tabs__item.is-top {
      border-top: 2px solid transparent;
      &.is-active {
        border-top: 2px solid #409eff;
      }
    }
  }
  .spaceTypes {
    .types {
      float: left;
      width: 100%;
      // width: calc(100% - 200px);
    }
    .groupAdd {
      float: left;
      width: 200px;
      text-align: right;
      height: 40px;
      border-bottom: 1px solid #e4e7ed;
    }
  }
}
</style>
