<template>
  <div :class="classes">
    <div class="rect-col">
      <div
        class="rect-col-item"
        ref="stdinfoContainer"
        :style="{ height: heights.stdinfo + 'px' }"
      >
        <slot name="stdinfo"></slot>
      </div>
      <div
        class="rect-col-item"
        ref="noticeContainer"
        :style="{ height: heights.notice + 'px' }"
      >
        <slot name="notice"></slot>
      </div>
    </div>
    <div class="rect-col">
      <div
        class="rect-col-item"
        ref="stdnoContainer"
        :style="{ height: heights.stdno + 'px' }"
      >
        <slot name="stdno"></slot>
      </div>
      <div
        :class="['rect-col-item', { 'rect-col-item-none': !$slots.dynamic }]"
        ref="dynamicContainer"
        :style="{ height: heights.dynamic + 'px' }"
      >
        <slot name="dynamic"></slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "card-head-body-auto-resize",
  data() {
    return {
      orgHeights: {
        stdinfo: 40,
        notice: 40,
        stdno: 40,
        dynamic: 40
      },
      heights: {
        stdinfo: 40,
        notice: 40,
        stdno: 40,
        dynamic: 40
      }
    };
  },
  computed: {
    classes() {
      return ["card-head-body-auto-resize", "col-item-auto-height"];
    }
  },
  mounted() {
    this.initStyles();
  },
  methods: {
    initStyles() {
      const containers = ["stdinfo", "notice", "stdno", "dynamic"];
      containers.forEach(container => {
        const dom =
          this.$refs[`${container}Container`] &&
          this.$refs[`${container}Container`].firstChild;
        this.orgHeights[container] = dom ? dom.offsetHeight : 0;
      });
      Object.keys(this.orgHeights).map(key => {
        this.heights[key] = this.orgHeights[key] + 2;
      });
      this.resizeRect();
    },
    resizeRect() {
      let col1 = this.orgHeights.stdinfo + this.orgHeights.notice;
      let col2 = this.orgHeights.stdno + this.orgHeights.dynamic;
      if (this.$slots.dynamic) {
        if (col1 > col2) {
          this.heights.stdno = col1 - col2 + this.orgHeights.stdno + 2;
          this.heights.dynamic = this.orgHeights.dynamic + 2;
        } else {
          const splitHeight = (col2 - col1) / 2;
          this.heights.stdinfo = splitHeight + this.orgHeights.stdinfo + 2;
          this.heights.notice = splitHeight + this.orgHeights.notice + 2;
        }
      } else {
        col1 += 14;
        col2 -= 2;
        if (col1 > col2) {
          this.heights.stdno = col1;
        } else {
          const splitHeight = (col2 - col1) / 2;
          this.heights.stdinfo = splitHeight + this.orgHeights.stdinfo + 2;
          this.heights.notice = splitHeight + this.orgHeights.notice + 2;
        }
      }
    }
  }
};
</script>
