<template>
  <div>
    <div class="node-wrap" v-if="nodeConfig.laneLevel === 1">
      <div class="node-wrap-box">
        <div class="node-box-after">
          <div class="nodeBoxTop">
            <div :title="getAllName(nodeConfig.organizationName)">{{ spliceNameFun(nodeConfig.organizationName, 8) }}</div>
            <div :title="getAllName(nodeConfig.name)">{{ spliceNameFun(nodeConfig.name, 12) }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="branch-wrap">
      <div class="branch-box-wrap">
        <div class="branch-box">
          <div class="col-box" v-for="(item, index) in nodeConfig.children" :key="index">
            <div class="condition-node">
              <div class="condition-node-box">
                <div class="auto-judge">
                  <div v-if="getOnerSixLevelFlag(item)">
                    <div class="wgdzb">
                      <div :title="getAllName(item.organizationName)">{{ spliceNameFun(item.organizationName, 8) }}</div>
                      <div :title="getAllName(item.name)">{{ spliceNameFun(item.name, 12) }}</div>
                    </div>
                  </div>
                  <div v-if="item.level === 'building_chief'">
                    <div class="lydxz">
                      <div :title="getAllName(item.organizationName)">{{ spliceNameFun(item.organizationName, 8) }}</div>
                      <div :title="getAllName(item.name)">{{ spliceNameFun(item.name, 12) }}</div>
                    </div>
                  </div>
                  <div v-if="item.level === 'unit_chief'">
                    <div @click="gotoPersonnel(item)" class="dyzxh" :title="item.name">
                      {{ spliceNameFun(item.name, 3) }}
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <nodeWrapGrid v-if="item.children" :nodeConfig.sync="item"></nodeWrapGrid>
            <template v-if="index == 0">
              <div class="top-left-cover-line"></div>
            </template>
            <template v-if="index == nodeConfig.children.length - 1">
              <div class="top-right-cover-line"></div>
            </template>
          </div>
        </div>
      </div>
    </div>
    <nodeWrapGrid v-if="nodeConfig.childNode" :nodeConfig.sync="nodeConfig.childNode"></nodeWrapGrid>
  </div>
</template>
<script>
export default {
  props: ["nodeConfig"],
  data() {
    return {
      homeTypeArr: [],
      sixLevelArr: ["province", "city", "district", "street", "community", "grid_manager"],
    };
  },
  mounted() {},
  computed: {},
  watch: {},
  methods: {
    getAllName(item) {
      return item;
    },

    spliceNameFun(row, num) {
      if (row && row.length > num) {
        return row.substring(0, num) + "...";
      }
      return row;
    },

    getOnerSixLevelFlag(item) {
      return this.sixLevelArr.includes(item.level);
    },

    findParentParentId(nodeConfig, pid) {
      let parentParentId = "";
      if (nodeConfig.id === pid) {
        return nodeConfig.pid;
      } else {
        if (nodeConfig.children && nodeConfig.children.length > 0) {
          for (let i = 0; i < nodeConfig.children.length; i++) {
            parentParentId = this.findParentParentId(nodeConfig.children[i], pid);
            if (parentParentId) {
              return parentParentId;
            }
          }
        }
      }
    },
    gotoPersonnel(item) {
      const grandPid = this.findParentParentId(this.nodeConfig, item.pid);
      const name = item.name ? item.name : "恭喜发财";
      this.$router.push(`/organizational/microgrid/${item.id}/${grandPid}/${name}`);
    },
  },
};
</script>
<style scoped lang="scss">
@mixin spanLevel {
  font-size: 14px;
  color: #fff;
  word-break: break-all;
}
@mixin normalLevel {
  display: flex;
  align-items: center;
  justify-content: space-around;
}
.node-box-after {
  .nodeBoxTop {
    font-size: 14px;
    cursor: pointer;
    position: relative;
    color: #fff;
    display: inline-block;
    width: 168px;
    height: 164px;
    padding-top: 10px;
    text-align: center;
    background-image: url(~@/assets/images/home/firstLevel.png);
    background-size: 100%;
    div:nth-child(2) {
      cursor: pointer;
    }
  }
}
.auto-judge {
  .wgdzb {
    @include spanLevel;
    width: 132px;
    height: 96px;
    text-align: center;
    padding-top: 5px;
    background-image: url(~@/assets/images/home/zbbg.png);
    background-size: 100%;
  }
  .lydxz {
    @include spanLevel;
    // @include normalLevel;
    text-align: center;
    width: 216px;
    height: 50px;
    padding-top: 5px;
    background-image: url(~@/assets/images/home/lydxzTree.png);
    background-size: 100%;
  }
  .dyzxh {
    @include spanLevel;
    @include normalLevel;
    height: 118px;
    width: 30px;
    writing-mode: tb-rl;
    margin-top: -12px;
    background-image: url(~@/assets/images/home/dyzxhTree.png);
    background-size: 100%;
    cursor: pointer;
  }
  .lxjt {
    @include spanLevel;
    display: flex;
    align-items: center;
    justify-content: space-around;
    height: 118px;
    width: 30px;
    writing-mode: tb-rl;
    margin-top: -12px;
    background-image: url(~@/assets/images/home/dyzxhTree.png);
    background-size: 100%;
  }
}
</style>
