<template>
  <section class="communityOverview">
    <ai-title title="小区总览"/>
    <div class="units" v-if="Object.keys(buildingUnits).length>0">
      <div class="building" v-for="(value,name) in buildingUnits" :key="name">
        <div class="unit" v-for="(op,j) in value" :key="j" @click="sta.selectedBuilding(op,j+1)"
             :class="{selected:sta.unitNumber==j+1 && sta.currentBuilding.buildingNumber==name}">
          <b>{{ name}}栋</b>
          <div>{{ j+1 }}单元</div>
        </div>
      </div>
    </div>
    <AiEmpty v-else>暂无楼栋信息,请进入【楼栋管理】添加</AiEmpty>
  </section>
</template>

<script>
export default {
  name: "communityOverview",
  inject: ['root', 'sta'],
  computed: {
    buildingUnits() {
      let obj = {}
      this.units.map(e => {
        for(let i=0;i<e.unitNumber;i++){
          obj[e.buildingNumber]?.push(e) || (obj[e.buildingNumber] = [e])
        }
      })
      return obj;
    }
  },
  data() {
    return {
      units: []
    }
  },
  methods: {
    getUnits(communityId) {
      this.root.instance.post("/app/appcommunitybuildinginfo/list", null, {
        params: {
          communityId,
          size: 999
        }
      }).then(res => {
        if (res?.data) {
          this.units = res.data.records
        }
      })
    }
  },
  created() {
    this.getUnits(this.root.isFormDv ? this.root.info.communityId : this.$route.query.communityId)
  }
}
</script>

<style lang="scss" scoped>
.communityOverview {
  max-width: 400px;

  .units {
    display: flex;
    flex-wrap: wrap;
    padding: 0 20px 20px;
    align-items: flex-start;

    .building {
      display: flex;
      align-items: center;
      height: auto;
      flex-wrap: wrap; 

      .unit {
        margin-right: 10px;
        margin-bottom: 10px;

        &:nth-of-type(5) {
          margin-right: 0;
        }
      }
    }

    .unit {
      width: 64px;
      height: 56px;
      background: #F8FBFF;
      border-radius: 2px 0 0 2px;
      border: 1px solid #829CCF;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      font-size: 12px;
      color: #999;
      cursor: pointer;

      b {
        color: #424242;
      }

      &.selected {
        color: #fff;
        background: #2266FF;

        b {
          color: #fff;
        }
      }
    }
  }

  .ai-empty {
    height: 240px;
  }
}
</style>
