<template>
  <div class="news-detail" v-if="pageShow">
    <div class="detail-top" :class="[info.videoFile ? 'detail-top__active' : '']">
      <h2>{{ info.title }}</h2>
      <p><span>{{ info.categoryName }}</span>{{ $dateFormat(info.createTime) }}</p>
      <u-parse :html="info.content" style="margin-top: 20px;"/>
    </div>
    <div class="accessory-list" v-if="info.files.length">
      <p>
        <u-icon name="attach" color="#2979ff" size="28"></u-icon>
        附件
      </p>
      <div class="card-list">
        <div class="item" v-for="(item,index) in info.files" :key="index" @click="prevFile(item)">
          <img src="./components/files.png" alt="">
          <div>{{ item.fileName }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
  name: 'newsDetail',
  data() {
    return {
      isShowComment: false,
      info: {files: []},
      imageProp: {
        mode: 'widthFix',
        padding: 0,
        lazyLoad: false,
        domain: ''
      },
      bottom: 0,
      placeholder: '',
      pageShow: false,
      commnet: '',
      commentList: [],
      commnetId: '',
      areaId: '',
      focus: false,
      current: 0,
      isMore: false,
      commentsTotal: 0,
      fileList: []
    }
  },

  onLoad(params) {
    this.id = params.id
    this.$nextTick(() => {
      this.getDetail()
      this.getComments()
    })
  },

  methods: {
    getDetail() {
      this.$http.post('/app/apppublicityinfo/queryDetailById', null, {
        params: {
          id: this.id
        }
      }).then(res => {
        if (res?.data) {
          this.info = res.data
          this.$nextTick(() => {
            this.pageShow = true
          })
        }
      })
    },
    ...mapActions(['previewFile']),
    prevFile(e) {
      if ([".jpg", ".png", ".gif"].includes(e.postfix.toLowerCase())) {
        uni.previewImage({
          current: e.url,
          urls: [e.url]
        })
      } else {
        this.previewFile({...e})
      }
    },

    onConfirm() {
      if (!this.commnet) {
        return this.$toast('评论不能为空')
      }
      this.$http.post('/app/appnewscentercomment/addOrUpdateForWX', {
        circleId: this.id,
        content: this.commnet,
        type: this.commentId ? 2 : 1,
        commentId: this.commentId,
        name: uni.getStorageSync('userInfo').nickName,
        areaId: this.areaId
      }).then(res => {
        if (res.code === 0) {
          this.commnet = ''
          this.$toast(this.commentId ? '回复成功' : '评论成功')
          this.current = 0
          this.isMore = false
          this.getComments()
          this.commentId = ''
          this.placeholder = ''
        } else {
          this.commentId = ''
          this.placeholder = ''
          this.$toast(res.msg)
        }
        this.isShowComment = false
      }).catch(() => {
        this.commentId = ''
        this.placeholder = ''
      })
    },

    getComments() {
      this.$http.post('/app/appnewscentercomment/listForWX?size=5&circleId=' + this.id + '&current=' + (this.current + 1)).then(res => {
        if (res?.data) {
          this.commentsTotal = res.data.total

          if (this.current === 0) {
            this.commentList = []
          }
          if (!res.data.records.length) {
            this.isMore = true
            this.isLoading = false

            this.$nextTick(() => {
              this.pageShow = true
            })

            return false
          }

          const data = res.data.records.map(item => {
            item.isShow = '0'

            return item
          })
          this.commentList.push(...data)
          this.current = this.current + 1
        }
      })
    }
  },

  onReachBottom() {
    this.getComments()
  },

  onShareAppMessage() {
    return {
      title: this.info.title,
      imageUrl: this.info.coverFile.url,
      path: `/subPages/live/newsDetail?id=${this.id}&areaId=${this.$areaId}`
    }
  }
}
</script>

<style lang="scss">
.news-detail {
  padding-bottom: 120px;

  div {
    box-sizing: border-box;
  }

  .comments {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: 1111;

    .mask {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.3);
    }
  }

  .comments-wrapper {
    position: absolute;
    bottom: 0;
    z-index: 1;
    width: 100%;
    padding: 32px 32px 16px;
    background: #FFFFFF;

    .textarea {
      position: relative;
      width: 100%;
      margin-bottom: 24px;
      padding: 16px;
      background: #F7F7F7;
      border-radius: 8px;

      textarea {
        width: 100%;
        height: 94px;
        color: #666;
        font-size: 26px;
      }

      .textarea-bottom {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 16px;
        color: #999999;
        font-size: 26px;
      }
    }

    .comments-bottom {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding-left: 16px;

      span {
        color: #666666;
        font-size: 26px;
      }

      div {
        width: 144px;
        height: 64px;
        line-height: 64px;
        text-align: center;
        color: #fff;
        font-size: 28px;
        background: #135AB8;
        border-radius: 32px;
      }
    }
  }

  .icon-img {
    width: 32px;
    height: 32px;
    margin-right: 8px;
  }

  .video-item {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1;
    color: #666;
    font-size: 28px;

    i {
      padding-right: 4px;
    }
  }

  .detail-top__active {
    padding-top: 30px;

    h2 {
      margin-bottom: 28px;
    }
  }

  .video {
    width: 100%;
    height: 396px;

    video {
      display: block;
      width: 100%;
      height: 396px;
    }
  }
}

.no-more {
  margin-top: 20px;
  text-align: center;
  color: #999;

  image {
    width: 400px;
    height: 240px;
  }

  p {
    text-align: center;
    color: #999;
    font-size: 28px;
  }
}

.detail-top {
  padding: 10px 30px 26px;
  background: #fff;

  & > h2 {
    margin-bottom: 40px;
    color: #333333;
    font-size: 40px;
  }

  p {
    font-size: 24px;

    span {
      margin-right: 15px;
    }
  }

  .detail-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #333333;
    font-size: 24px;

    .right {
      display: flex;
      align-items: center;

      & > div {
        display: flex;
        align-items: center;

        &:first-child {
          margin-right: 60px;
        }
      }

      i {
        margin-right: 10px;
        font-size: 32px;
      }
    }
  }

  .detail-content {
    margin-top: 46px;

    image {
      display: block;
      width: 100%;
      height: 400px;
      margin: 0 auto;
    }
  }
}

.detail-comments {
  margin-top: 20px;
  padding: 0 30px 20px;
  background: #fff;

  & > h2 {
    padding-top: 16px;
    font-size: 30px;
    color: #333333;
  }

  .comments-list {
    .item {
      display: flex;
      margin-top: 35px;

      image {
        flex-shrink: 1;
        width: 50px;
        height: 50px;
        border-radius: 50%;
        margin-right: 22px;
      }

      .item-right {
        flex: 1;

        .item-right__top {
          display: flex;
          justify-content: space-between;
          height: 50px;

          .item-right__top--right {
            display: flex;
            align-items: center;
            padding-left: 20px;

            i {
              margin-right: 10px;
              color: #666;
              font-size: 28px;
            }

            span {
              color: #333333;
              font-size: 24px;
            }
          }
        }

        .item-right__content {
          line-height: 46px;
          color: #333333;
          font-size: 28px;
          text-align: justify;
        }

        .item-right__info {
          display: flex;
          align-items: center;
          margin: 10px 0;

          span {
            margin-right: 20px;
            color: #999999;
            font-size: 24px;
          }

          i {
            color: #333333;
            font-size: 24px;
          }
        }

        .item-replay {
          padding: 30px;
          background: #F0F0F0;

          .item-replay__item {
            display: flex;
            line-height: 1.3;
            margin-bottom: 10px;

            h3 {
              color: #333333;
              font-size: 26px;
              font-weight: 600;
            }

            span {
              font-size: 26px;
            }
          }

          .item-replay__more {
            color: #999999;
            font-size: 26px;
          }
        }
      }
    }
  }
}

.footer-bar {
  display: flex;
  position: fixed;
  align-items: center;
  justify-content: space-between;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100px;
  padding: 0 30px;
  box-sizing: border-box;
  border-top: 1px solid rgba(0, 0, 0, 0.2);
  background: #fff;

  input {
    width: 460px;
    height: 58px;
    background: #F0F0F0;
    border-radius: 30px;
    font-size: 26px;
    text-align: center;
  }

  .footer-bar__right {
    display: flex;
    align-items: center;

    i {
      font-size: 30px;
      color: #666;
    }

    .count {
      position: relative;
      text-align: right;

      span {
        position: absolute;
        top: -18px;
        right: -11px;
        z-index: 21;
        padding: 3px 7px;
        font-size: 20px;
        color: #fff;
        border-radius: 12px;
        background: #C41C19;
      }
    }

    .count, & > i {
      margin-right: 40px;
    }

    button {
      padding: 0 !important;
      font-size: 26px !important;
      color: #666 !important;
      line-height: 1 !important;
      background: transparent !important;

      &::after {
        border: none;
      }

      &:last-child {
        border-right: none;
      }
    }
  }
}

.accessory-list {
  margin-top: 16px;
  background: #FFF;
  padding: 0 32px 30px 32px;
  box-sizing: border-box;

  p {
    font-size: 32px;
    height: 112px;
    line-height: 112px;
  }

  .card-list {
    .item {
      padding: 16px;
      border: 1px solid #CCCCCC;
      border-radius: 8px;
      display: flex;
      margin-bottom: 32px;

      img {
        width: 96px;
        height: 96px;
      }

      div {
        align-self: center;
      }
    }
  }

}
</style>
