<template>
  <div class="keep-group">
    <div class="flex-r-c">
      <div class="title-group flex-r-c center left">
        <h5>{{ title }}</h5>
      </div>
    </div>
    <div class="hot-keep-group flex-r-c wrap">
      <div class="card-item flex-r-c" v-for="item in options" :key="item.id" @click="goContent(item)">
        <div class="mini-img-group">
          <img :src="item.img" alt="" @error="handleError">
          <i :class="'el-icon-jy-' + item.type" />
        </div>
        <div class="flex-c-c">
          <div class="flex">
            <div class="title-text van-multi-ellipsis--l2">{{ item.title }}</div>
          </div>
          <div class="money-group center left">
            {{ item.docTags }}
          </div>
          <div class="flex-c-c info-text">
            <span v-if="item.contribution">贡献者: {{ item.contribution }}</span>
            <div class="info-text-group flex-r-c center left">
              <span v-if="item.viewTimes">{{ SetViewUnit(item.viewTimes) }}浏览</span>
              <span v-if="item.page">共{{ item.page }}页</span>
              <span v-if="item.size">{{ item.size | sizeFormatter }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SelectedRecommend',
  props: {
    title: {
      type: String,
      default: '精选推荐'
    },
    options: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {}
  },
  mounted () {
    this.setItemwidth()
  },
  methods: {
    handleError (img) {
      img.target.src = require('../assets/images/error.png')
    },
    SetViewUnit (num) {
      let newNum = 0
      if (!num) return 0
      if (num > 10000) {
        newNum = (num / 10000).toFixed(1) + 'w'
      } else {
        newNum = num
      }
      return newNum
    },
    goContent (item) {
      this.$emit('click', item)
    },
    setItemwidth () { // 适配嵌入工作台内
      const bodyWidth = document.body.clientWidth
      if (window.goTemplateData && window.goTemplateData.inIframe && bodyWidth > 1248) {
        const element = document.getElementsByClassName('card-item')[0]
        const itemwidth = element.offsetWidth + 104 // 104是margin
        const num = Math.floor(bodyWidth / itemwidth)
        const newwidth = (Math.floor((bodyWidth - (itemwidth * num)) / num)) + itemwidth
        var elements = document.querySelectorAll('.card-item')
        elements.forEach(function (ele) {
          ele.style.width = newwidth + 'px' // 设置新的宽度
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.keep-group {
  margin-top: 38px;
  padding-bottom: 50px;
  @include diy-icon('iconJianYu', 24, 24);

  @include diy-icon('pdf', 24);
  @include diy-icon('word', 24);
  @include diy-icon('excel', 24);
  @include diy-icon('ppt', 24);

  .title-group {
    margin-bottom: 16px;

    h5 {
      color: #1D1D1D;
      font-family: Microsoft YaHei;
      font-size: 18px;
      font-weight: 400;
      line-height: 28px;
      letter-spacing: 0px;
      text-align: left;
      margin-right: 9px;
    }
  }

  .card-item {
    cursor: pointer;
    justify-content: flex-start;
    padding: 8px 0 12px;
    box-sizing: border-box;

    &:hover {
      .title-text {
        color: #2ABED1;
      }
    }

    .money-group {
      display: block;
      margin-top: 18px;
      width: 180px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      font-size: 13px;
      line-height: 22px;
      color: #999999;
    }

    .mini-img-group {
      flex-shrink: 0;
      position: relative;
      border-radius: 4px;
      border: 1px solid #ececec;
      width: 80px;
      height: 112px;
      margin-right: 16px;
      overflow: hidden;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;

      img {
        width: 100%;
      }

      i {
        position: absolute;
        right: 0;
        bottom: 0;
      }
    }

    .info-text-group {
      span {
        display: inline-block;

        &:last-child {
          &::after {
            content: unset;
          }
        }

        &::after {
          content: "|";
          padding: 0 4px;
          color: #ececec;
        }
      }
    }

    .info-text {
      font-family: Microsoft YaHei;
      font-style: normal;
      font-weight: normal;
      font-size: 13px;
      line-height: 24px;
      color: #999999;
    }

    .red-text {
      color: #FF3A20;
      font-family: Microsoft YaHei;
      font-style: normal;
      font-weight: normal;
      font-size: 18px;
      line-height: 28px;
    }

    .title-text {
      width: 183px;
      font-family: Microsoft YaHei;
      font-style: normal;
      font-weight: normal;
      font-size: 16px;
      line-height: 24px;
      color: #1D1D1D;
      word-wrap: break-word;
    }
  }

  .van-ellipsis {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .van-multi-ellipsis--l2 {
    display: -webkit-box;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;

  }

  .hot-keep-group {
    .card-item {
      width: 279px;
      overflow: hidden;
    }
  }
}

.in-app {

  // 工作台内适配
  .hot-keep-group {
    justify-content: flex-start !important;

    @media screen and (max-width: 1248px) {
        .card-item {
          margin-right: 41px;
        }
    }

    @media screen and (min-width: 1248px) {
      .card-item {
        margin-right: 104px;
      }
    }
  }
}
</style>
