<template>
  <div v-loading="load" class="saga-elevation">
    <div v-if="elevationData.length" class="saga-floor">
      <div class="saga-rf">
        <p class="saga-floorName">RF</p>
        <div class="saga-spaceList">
          <div class="saga-group" v-for="(winth, index) in groupWidth" :key="index" :style="`width: ${100*winth}px;`"></div>
        </div>
      </div>
      <div class="saga-floor-item" v-for="floor in elevationData" :key="floor.id">
        <p class="saga-floorName" :title="floor.localName?floor.localName:floor.name">
          {{floor.localName?floor.localName:floor.name}}
          <i class="el-icon-right"></i>
        </p>
        <div class="saga-spaceList">
          <div class="saga-group" v-for="(group, groupIndex) in floor.group" :style="`width: ${100*groupWidth[groupIndex]}px;`">
            <div class="saga-spaceItem" 
            :class="[spaceItem.IsAI?spaceItem.checked?'saga-spaceItem-checked':'saga-spaceItem-ai':'']" 
            v-for="spaceItem in group.spaceList" 
            :key="spaceItem.RoomID"
            :title="spaceItem.RoomLocalName?spaceItem.RoomLocalName:spaceItem.RoomName"
            @click="handleClickCheck(spaceItem)"
            >
            {{spaceItem.localName || spaceItem.name}}
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-else class="saga-center">
      <i class="icon-wushuju iconfont"></i>
      暂无数据
    </div>
  </div>
</template>

<script>
  import { shaftVerticalSpace, shaftZoneLink } from "@/api/scan/request"

  import {mapGetters} from 'vuex'
  export default {
  data () {
    return {
      elevationData: [],
      groupWidth: {},
      checkedList: [],
      load: false,
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId"])
  },
  props: {
    params: Object,
    onlyRead: Boolean,
    isEdit: Boolean,
    isAI: Boolean,
  },
  methods: {
    init () { //获取数据并格式化数据
      this.elevationData = [];
      //根据isAI判断请求参数是否包含AI推介数据（暂未完成）
      this.params.aiSwitch = this.isAI
      this.load = true;
      // 查询竖井关联的空间垂直交通关系
      shaftVerticalSpace(this.params, (res) => {
        let data = res.content || [];
        let max = 0, idMap = {}, groupMap = {}, copyData = JSON.parse(JSON.stringify(data));
        this.groupWidth = {};
        copyData.reverse().forEach(floor => {
          if (floor.zoneSpaceList && floor.zoneSpaceList.length) {
            floor.zoneSpaceList.forEach(space => {
              if (idMap[space.id]) {  // 该业务空间在映射列表中
                groupMap[idMap[space.id]].push(space)
                space.spaceIdList.forEach(RoomID =>{
                  if (!idMap[RoomID]) {
                    idMap[RoomID] = idMap[space.id];
                  }
                })
              } else { // 该业务空间不在映射列表中
                max++;
                idMap[space.id] = max;
                groupMap[max] = [];
                this.groupWidth[max] = 0;
                groupMap[max].push(space);
                space.spaceIdList.forEach(RoomID =>{
                  if (!idMap[RoomID]) {
                    idMap[RoomID] = max;
                  }
                })
              }
            })
          }
        })
        data.forEach((floor, index) =>{
          floor.group = {};
          Object.keys(groupMap).forEach(groupIndex =>{
            let max = 0;
            floor.group[groupIndex] = {
              spaceList: []
            };
            if (floor.zoneSpaceList && floor.zoneSpaceList.length) {
              floor.zoneSpaceList.forEach(space => {
                if (groupMap[groupIndex].find(item => { return space.id == item.id })) {
                  space.checked = false;
                  floor.group[groupIndex].spaceList.push(space);
                  max++;
                }
              })
            }
            if (max > this.groupWidth[groupIndex]) {
              this.groupWidth[groupIndex] = max;
            }
          })
        });
        this.elevationData = data
        this.load = false;
      })
    },
    handleClickCheck (sapceItem) { //点选推介的业务空间
      if (sapceItem.IsAI && this.isEdit && !this.onlyRead) { //是AI推荐并且编辑模式下
        sapceItem.checked = sapceItem.checked? false: true;
        if (sapceItem.checked) {
          this.checkedList.push(sapceItem);
          // 选中平面图的对应空间
          this.$emit('elevationChecked',sapceItem.RoomID);
        } else {
          let index = this.checkedList.findIndex(item =>{ return item.RoomID == sapceItem.RoomID })
          if (index != -1) { // 找到对应的数据
            this.checkedList.splice(index,1);
            // 取消选中平面图的对应空间
            this.$emit('elevationUncheck',sapceItem.RoomID);
          }
        }
      }
    },
    savaEdit () {// 接口保存数据和业务空间的关系
      let params = {
        type: this.params.objectType,
        data: {
          shaftId: this.params.shaftId,
          spaceIdList: this.checkedList.map(item => {
            return item.RoomID
          })
        }
      }
      shaftZoneLink(params, res =>{
        this.$message.success("保存成功");
        this.init();
      })
    }
  },
  watch: {
    projectId() {
      if (this.params.buildingId && this.params.objectType && this.params.shaftId) {
        this.init();
      }
      console.log(this.projectId)
    },
    // isAI() {
    //   if (this.params.BuildingId && this.params.ObjectType && this.params.ShaftId) {
    //     this.init();
    //   }
    // },
    params: {
      handler() {
        if (this.params.buildingId && this.params.objectType && this.params.shaftId) {
          this.init();
        }
      },
      immediate: true,
      deep: true,
    }
  }
}
</script>

<style lang="less">
  .saga-elevation {
    height: 100%;
    position: relative;
    .saga-center{
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-60%);
      text-align: center;
    }
    .saga-floor {
      display: flex;
      box-sizing: border-box;
      flex-direction: column;
      justify-content: flex-end;
      padding: 60px 10px 20px 0px;
      .saga-rf {
        display: flex;
        flex-direction: row;
        font-size: 12px;
        color: blue;
      }
      .saga-floor-item {
        display: flex;
        flex-direction: row;
        font-size: 12px;
        height: 60px;
        line-height: 60px;
      }
      .saga-floorName {
        min-width: 80px;
        text-align: right;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .saga-spaceList {
        display: flex;
        flex-direction: row;
        flex: 1;
        border-bottom: 2px solid #c1c6d2;
        box-sizing: border-box;
        .saga-group {
          display: flex;
          flex-direction: row;
          margin-left: 25px;
          box-sizing: border-box;
          .saga-spaceItem {
            width: 100px;
            margin-left: 1px;
            text-align: center;
            overflow: hidden;
            text-overflow:ellipsis;
            white-space: nowrap;
            background-color: #ffcc00;
            border: 1px solid #333333;
          }
          .saga-spaceItem-ai{
            cursor: pointer;
            box-sizing: border-box;
            background-color: #eaeae9;
            border: 1px dashed #333333;
          }
          .saga-spaceItem-checked {
            cursor: pointer;
            box-sizing: border-box;
            background-color: #1abc9c;
            border: 1px dashed #333333;
          }
        }
      }
    }
  }
</style>
