<template>
  <div v-if="intro" class="course-intro-wrapper">
    <CommonTitle></CommonTitle>
    <div class="body">
      <p class="content" :class="{'ellipsis': isUp}">{{ intro }}</p>
      <div class="toggle-btn" v-show="showArrow" @click="isUp=!isUp">
        <span class="action">{{ isUp ? "详情" : "收起"}}</span>
        <img v-show="isUp" src="~@/images/down.png" alt="" />
        <img v-show="!isUp" src="~@/images/up.png" alt="" />
      </div>
      <div class="offset">{{ intro }}</div>
    </div>
  </div>
</template>
<script>
import CommonTitle from "@/components/course/common-title"
export default {
  components: {
    CommonTitle
  },
  props: {
    intro: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      isUp: true,
      showArrow: true,
    }
  },
  watch: {
    intro(val) {
      this.countH();
    }
  },
  mounted() {
    this.countH();
  },
  methods: {
    countH() {
      if (this.intro) {
        this.$nextTick(() => {
          const content = this.$el.querySelector('.content').getBoundingClientRect();
          const offset = this.$el.querySelector('.offset').getBoundingClientRect();
          if (offset.height < (content.height - 1)) {
            this.showArrow = false;
          }
        })
      }
    }
  },
}
</script>
<style lang="less" scoped>
.course-intro-wrapper {
  .body {
    position: relative;
    color: #676869;
    font-size: 14px;
    margin: 0 15px;
    .content {
      display: block;
      text-align: justify;
      line-height: 21px;
      &.ellipsis {
        height: 63px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        white-space: normal;
      }
    }
    .toggle-btn {
      // background: #fff;
      position: absolute;
      right: 0;
      bottom: -18px;
      display: flex;
      align-items: center;
      // padding-left: 30px;
      // background: linear-gradient(90deg, rgba(255, 255, 255, .9), rgba(255, 255, 255, 1));
      img {
        position: relative;
        top: 1px;
        margin-left: 6px;
        width: 12px;
        height: 7px;
      }
      .action {
        color: #449284;
      }
    }
  }
  .offset{
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
  }
}
</style>
