<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.FloorSequenceID">
        <p class="saga-floorName">{{floor.FloorLocalName}}<i class="el-icon-right"></i></p>
        <div class="saga-spaceList">
          <div class="saga-group" v-for="(group, groupIndex) in floor.group" :key="floor.FloorSequenceID+'-'+groupIndex" :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"
            @click="handleClickCheck(spaceItem)"
            >
            {{spaceItem.RoomLocalName}}
            </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 () { //获取数据并格式化数据
      //根据isAI判断请求参数是否包含AI推介数据（暂未完成）
      this.params.AiSwitch = this.isAI
      console.log(this.params)
      this.load = true;
      // 查询竖井关联的空间垂直交通关系
      shaftVerticalSpace(this.params, (res) => {
        let data = res.Content;
        // let data = [
        //   {
        //     FloorSequenceID: 5,
        //     FloorLocalName: 'F5',
        //     ZoneSpaceList: [
        //       {
        //         RoomID: 'f5-001',
        //         IsAI: false,
        //         RoomLocalName: '业务空间f5-001',
        //         SpaceIdList: ['f4-002']
        //       }
        //     ]
        //   },
        //   {
        //     FloorSequenceID: 4,
        //     FloorLocalName: 'F4',
        //     ZoneSpaceList: [{
        //       RoomID: 'f4-001',
        //       IsAI: true,
        //       RoomLocalName: '业务空间f4-001',
        //       SpaceIdList: ['f3-002']
        //     }, {
        //       RoomID: 'f4-002',
        //       IsAI: true,
        //       RoomLocalName: '业务空间f4-002',
        //       SpaceIdList: ['f5-001']
        //     }]
        //   },
        //   {
        //     FloorSequenceID: 3,
        //     FloorLocalName: 'F3',
        //     ZoneSpaceList: [{
        //       RoomID: 'f3-001',
        //       IsAI: false,
        //       RoomLocalName: '业务空间f3-001',
        //       SpaceIdList: ['f2-002']
        //     }, {
        //       RoomID: 'f3-002',
        //       IsAI: true,
        //       RoomLocalName: '业务空间f3-002',
        //       SpaceIdList: ['f2-002','f4-001']
        //     }]
        //   },
        //   {
        //     FloorSequenceID: 2,
        //     FloorLocalName: 'F2',
        //     ZoneSpaceList: [{
        //       RoomID: 'f2-001',
        //       IsAI: false,
        //       RoomLocalName: '业务空间f2-001',
        //       SpaceIdList: ['f1-001']
        //     }, {
        //       RoomID: 'f2-002',
        //       IsAI: true,
        //       RoomLocalName: '业务空间f2-002',
        //       SpaceIdList: ['f3-001','f3-002']
        //     }, {
        //       RoomID: 'f2-003',
        //       IsAI: false,
        //       RoomLocalName: '业务空间f2-003',
        //       SpaceIdList: []
        //     }]
        //   },
        //   {
        //     FloorSequenceID: 1,
        //     FloorLocalName: 'F1',
        //     ZoneSpaceList: [{
        //       RoomID: 'f1-001',
        //       IsAI: false,
        //       RoomLocalName: '业务空间f1-001',
        //       SpaceIdList: ['f2-001']
        //     }, {
        //       RoomID: 'f1-002',
        //       IsAI: false,
        //       RoomLocalName: '业务空间f1-002',
        //       SpaceIdList: []
        //     }, {
        //       RoomID: 'f1-003',
        //       IsAI: false,
        //       RoomLocalName: '业务空间f1-003',
        //       SpaceIdList: []
        //     }]
        //   }
        // ];
        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.RoomID]) {  // 该业务空间在映射列表中
                groupMap[idMap[space.RoomID]].push(space)
                space.SpaceIdList.forEach(RoomID =>{
                  if (!idMap[RoomID]) {
                    idMap[RoomID] = idMap[space.RoomID];
                  }
                })
              } else { // 该业务空间不在映射列表中
                max++;
                idMap[space.RoomID] = 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.RoomID == item.RoomID })) {
                  space.checked = false;
                  floor.group[groupIndex].spaceList.push(space);
                  max++;
                }
              })
            }
            if (max > this.groupWidth[groupIndex]) {
              this.groupWidth[groupIndex] = max;
            }
          })
        });
        this.elevationData = data
        console.log(this.elevationData)
        this.load = false;
      })
    },
    handleClickCheck (sapceItem) { //点选推介的业务空间
      console.log(this.isEdit)
      if (sapceItem.IsAI && this.isEdit && !this.onlyRead) { //是AI推荐并且编辑模式下
        sapceItem.checked = sapceItem.checked? false: true;
        if (sapceItem.checked) {
          this.checkedList.push(sapceItem)
        } else {
          let index = this.checkedList.findIndex(item =>{ return item.RoomID == sapceItem.RoomID })
          if (index != -1) { // 找到对应的数据
            this.checkedList.splice(index,1)
          }
        }
      }
    },
    savaEdit () {// 接口保存数据和业务空间的关系
      let params = {
        ShaftId: this.params.ShaftId,
        SpaceIdList: this.checkedList.map(item => {
          return item.RoomID
        })
      }
      console.log(params);
      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(val,ccc) {
        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;
      height: 100%;
      box-sizing: border-box;
      flex-direction: column;
      justify-content: flex-end;
      padding: 0 10px 60px 0;
      .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: 50px;
        text-align: right;
      }
      .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: 10px;
          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>
