<template>
  <div class="classDetail">
    <u-navbar title="在线课堂" title-color="#000" :background="{background: '#f4f6fa'}"></u-navbar>

    <div class="media">
      <img :src="data.pictureUrl" alt="" v-if="data.courseType == 0">
      <video :src="data.videoUrl" controls v-if="data.courseType == 1"/>
    </div>

    <div class="title">{{ data.title }}</div>
    <p class="study_num">{{ data.learnerNumber || 0 }}人已学习</p>

    <u-parse class="content" :html="data.content"></u-parse>

    <div class="btn_box">
      <div class="input_btn" @click="showSend = true">我来说两句...</div>
      <div class="comment" @click="showComment = true;getComment()">
        <img src="https://cdn.cunwuyun.cn/qujing/message.png" alt="">
        <div class="comm_num">
          <span>{{ data.msgCount || 0}}</span><span v-show="data.msgCount > 999">+</span>
        </div>
      </div>
    </div>

    <u-popup v-model="showComment" mode="bottom" border-radius="32">
			<h4 class="message_num">共{{ data.msgCount }}条评论</h4>
      <scroll-view scroll-y="true" @scrolltolower="scrollLower" class="comment_box" v-if="commentList.length">
        <div class="comment_card" v-for="item in commentList" :key="item.id">
          <div class="avatar">
            <img :src="item.avatar" alt="" v-if="item.avatar">
            <img src="https://cdn.cunwuyun.cn/shandong10086/avatar.png" alt="" v-else>
          </div>
          <div class="comment_info">
            <div class="avatar_info">
              <div class="avatar_name">{{ item.name }}</div>
              <div class="avatar_time">{{ item.commentTime }}</div>
            </div>
            <div class="comm_content">{{ item.content }}</div>
          </div>
        </div>
      </scroll-view>
      <AiEmpty :description="`暂无相关评论`" class="emptyWrap" v-else/>

      <div class="comm_input_btn" @click="showSend = true,showComment= false">
        <div>我来说两句...</div>
      </div>
		</u-popup>

    <u-popup v-model="showSend" mode="bottom" border-radius="32">
			<div class="send_box">
        <div class="text">
          <textarea
              @blur="height = 0"
              placeholder-style="color: #999;font-size: 16px;"
              v-model="content"
              :cursor-spacing="40"
              placeholder="我来说两句..."
              maxlength="100"
              @keyboardheightchange="keyboard">
          </textarea>
        </div>
        <div class="send_btn" @click="sendComment">发送</div>
      </div>
		</u-popup>
  </div>
</template>

<script>
export default {
  customNavigation: true,
  data() {
    return {
      showComment: false,
      showSend: false,
      content: '',
      height: 0,
      data: {},
      commentList: [],
      id: '',
      flag: false,
      current: 1,
    }
  },
  onLoad(o) {
    this.id = o.id
    this.getDetail()
  },
  methods: {
    getDetail() {
      this.$instance.post(`/app/appcourseinfo/queryDetailById?id=${this.id}`).then(res=> {
        if(res?.data) {
          this.data = res.data
        }
      }).catch(err=> this.$u.toast(err.msg))
    },
    // 评论集合
    getComment() {
      this.$instance.post(`/app/appcoursecomment/listByApplet`,null,{
        params: {
          current: this.current,
          size: 10,
          courseId: this.data.id,
        }
      }).then(res=> {
        if(res?.data) {
          this.commentList = this.current==1? res.data.records: [...this.commentList,...res.data.records]
        }
      })
    },
    // 新增评论
    sendComment() {
      if(this.flag) return

      if(!this.content) {
        return this.$u.toast('请输入内容')
      }

      this.flag = true
      this.$instance.post(`/app/appcoursecomment/add`,{
        courseId: this.data.id,
        content: this.content,
        type: '1'
      }).then(res=> {
        if(res.code==0) {
          this.flag = false
          this.showSend = false
          this.content = ''
          this.getComment()
          this.getDetail()
        }
      }).catch(err=> {
        this.flag = false
        this.showSend = false
        this.$u.toast(err.msg)
      })
    },
    stop() {
      this.$instance.post(`/app/appcourseinfo/stopLearnById`,null,{
        params: {
          id: this.data.id,
          recordId: this.data.recordId
        }
      }).then(res=> {
        if(res.code==0) {
          console.log(res.msg);
        }
      }).catch(err=> this.$u.toast(err.msg))
    },
    keyboard(e) {
      console.log(e.detail.height);
    },
    scrollLower() {
      this.current++
      this.getComment()
    },
  },
  onUnload() {
    this.stop()
  }
}
</script>

<style lang="scss" scoped>
.classDetail {
  padding-bottom: 140px;
  box-sizing: border-box;
  .media {
    img {
      width: 100%;
      height: 420px;
    }

    video {
      width: 100%;
      height: 420px;
    }
  }

  .title {
    margin-top: 30px;
    font-weight: 500;
    font-size: 46px;
    color: #222222;
    padding: 0 32px;
    box-sizing: border-box;
  }

  .study_num {
    margin-top: 16px;
    font-weight: 400;
    font-size: 30px;
    color: #999999;
    padding: 0 32px;
    box-sizing: border-box;
  }

  .content {
    margin-top: 48px;
    padding: 0 32px;
    box-sizing: border-box;
  }

  .btn_box {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 128px;
    background: #fff;
    padding: 24px 32px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    
    .input_btn {
      width: 87%;
      height: 80px;
      line-height: 80px;
      padding-left: 32px;
      box-sizing: border-box;
      background: #F4F5FA;
      border-radius: 44px;
      font-weight: 400;
      font-size: 32px;
      color: #666666;
      margin-right: 20px;
    }

    .comment {
      position: relative;
      img {
        width: 48px;
        height: 48px;
      }

      .comm_num {
        position: absolute;
        left: 34px;
        top: -10px;
        background: #FFF;
        color: #E23C3C;
        font-weight: 600;
        font-size: 20px;
      }
    }
  }

  .message_num {
    width: 100%;
    height: 100px;
    line-height: 100px;
    text-align: center;
    font-weight: 400;
    font-size: 28rpx;
    color: #333333;
  }

  .comment_box {
    width: 100%;
    max-height: 60vh;
    .comment_card {
      display: flex;
      padding: 24px 32px;
      box-sizing: border-box;

      .avatar {
        width: 64px;
        height: 64px;
        border-radius: 50%;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .comment_info {
        margin-left: 16px;
        width: calc(100% - 80px);

        .avatar_info {
          display: flex;
          justify-content: space-between;

          .avatar_name {
            font-weight: 500;
            font-size: 28px;
            color: #333333;
          }
          .avatar_time {
            font-weight: 400;
            font-size: 26px;
            color: #999999;
          }
        }

        .comm_content {
          margin-top: 8px;
          font-weight: 400;
          font-size: 32rpx;
          color: #333333;
        }
      }
    }
  }
  .comm_input_btn {
    width: 100%;
    height: 128px;
    background: #fff;
    padding: 24px 32px;
    box-sizing: border-box;
    div {
      width: 100%;
      height: 80px;
      line-height: 80px;
      padding-left: 32px;
      box-sizing: border-box;
      background: #F4F5FA;
      border-radius: 44px;
      font-weight: 400;
      font-size: 32px;
      color: #666666;
    }
  }
  

  .send_box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 32px;
    box-sizing: border-box;
    .text {
      textarea { 
        background: #F4F5FA;
        border-radius: 16px;
        padding: 16px;
        box-sizing: border-box;
        height: 128px;
        width: 80vw;
      }
    }
    .send_btn {
      width: 80px;
      height: 80px;
      line-height: 80px;
      text-align: right;
      font-weight: 500;
      font-size: 34rpx;
      color: #2D7DFF;
    }
  }
}
</style>