<template>
  <div class="cb-container">
    <div class="title" v-if="pzList.length">评课笔记</div>
    <div class="pzListBox" v-if="pzList && pzList.length">
      <div class="pzList" v-for="(pz, pzIndex) in pzList" :key="pzIndex">
        <div class="pzNavTop">
          <div>批</div>
          <div>
            {{ pz.username && pz.username.length > 8 ? pz.username.substring(0, 8) + '...' : pz.username }}的批注
          </div>
          <!-- v-if="pz.userid == userid" -->
          <div class="pzDelete" @click="deletePz(pz.id)">
            删除
          </div>
        </div>
        <div class="pzContent cont" v-html="pz.content" v-if="pz.type == '1'"></div>
        <div class="pzContent" v-if="pz.type == '2'">
          <audio :src="pz.content" controls="controls" ref="audio">
            Your browser does not support the audio element.
          </audio>
        </div>
        <div class="pzContent" v-if="pz.type == '3'">
          <img :src="pz.content" style="width: 90%; margin: 0 auto; display: block" @click="previewImg(pz.content)" />
        </div>
        <div class="time">
          {{ pz.time }}
        </div>
      </div>
    </div>
    <div class="noPz" v-else>
      <img src="@/assets/images/course/noPz.png" alt="" />
    </div>

    <div class="commentInput" @click="panelVisible = true">
      <div class="input">点击发一条评课内容</div>
    </div>
    <commentPanel
      :panelVisible.sync="panelVisible"
      :courseid="courseid"
      :courseType="courseType"
      :taskCount="taskCount"
    ></commentPanel>
  </div>
</template>

<script>
import commentPanel from './commentPanel.vue'
import { getCoursePz, deletePz } from '@/api/course'
export default {
  props: {
    courseid: {
      type: String,
      default: ''
    },
    courseType: {
      type: Number,
      default: 0
    },
    taskCount: {
      type: Number,
      default: 0
    }
  },
  components: {
    commentPanel
  },
  data() {
    return {
      pzList: [],
      panelVisible: false,
      userid: ''
    }
  },
  watch: {
    courseType(newValue, oldValue) {
      this.getData()
    },
    taskCount(newValue, oldValue) {
      this.getData()
    },
    panelVisible(newValue, oldValue) {
      this.getData()
    }
  },
  methods: {
    deletePz(id) {
      this.$dialog({
        message: '确定删除此批注吗？',
        showCancelButton: true,
        beforeClose: (action, done) => {
          if (action === 'confirm') {
            const params = [
              {
                id: id
              }
            ]
            deletePz(params)
              .then(res => {
                this.$toast({
                  message: '删除成功',
                  type: 'success'
                })
                this.getData()
                done()
              })
              .catch(err => {
                console.error(err)
                done()
              })
          } else {
            // 拦截取消操作
            done()
          }
        }
      })
    },
    getData() {
      const params = {
        cid: this.courseid,
        s: this.courseType,
        t: this.taskCount
      }
      getCoursePz(params)
        .then(res => {
          this.pzList = res[0]
        })
        .catch(err => {
          console.log(err)
        })
    }
  },
  mounted() {
    this.getData()
  }
}
</script>

<style lang="scss" scoped>
.cb-container {
  width: 100%;
  padding-bottom: 1.5rem;
  .commentInput {
    background: #fff;
    box-shadow: 0 -1px 3px 1px #dedede82;
    height: 1.5rem;
    width: 100%;
    position: fixed;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    .input {
      width: 95%;
      height: 70%;
      border-radius: 5px;
      background: rgb(243, 243, 243);
      display: flex;
      align-items: center;
      box-sizing: border-box;
      padding: 0 0.4rem;
      font-size: 14px;
      color: #585858;
    }
  }
  .title {
    font-size: 14px;
    width: 95%;
    margin: 10px auto 0;
  }
  .pzBox,
  .noPzBox {
    height: calc(100% - 60px);
    /* background: #ededed; */
    background: #fff;
    /* border-radius: 0 0 15px 15px; */
  }

  .noPzBox {
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: center;
  }

  .pzList {
    background: #f7f7f7;
    width: 95%;
    margin: 0 auto 10px;
    border-radius: 5px;
  }

  .pzNavTop {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    padding: 10px 10px 0 10px;
    align-items: center;
  }

  .pzDelete {
    cursor: pointer;
    margin-left: auto;
    font-size: 14px;
    color: rgb(135, 135, 135);
  }

  .pzNavTop > div:nth-child(1) {
    background: #3760af;
    width: 30px;
    height: 30px;
    color: #fff;
    text-align: center;
    line-height: 30px;
    border-radius: 50%;
    font-size: 14px;
  }

  .pzNavTop > div:nth-child(2) {
    font-size: 16px;
    color: #959595;
    margin-left: 5px;
  }

  /deep/ .pzContent {
    padding: 10px;
    word-break: break-word;
    font-size: 14px;
    > p {
      margin: 0;
    }
  }

  .pzContent audio {
    width: 100%;
    height: 1.2rem;
  }

  .pzContent audio::-webkit-media-controls-panel {
    background: #fff;
  }

  .pzListBox {
    padding-top: 15px;
    height: calc(100% - 60px);
    overflow: auto;
  }

  .noPz {
    width: 100%;
    height: 5rem;
    margin: 0 auto 0;
    display: flex;
    align-items: center;
    justify-content: center;

    > img {
      width: 3rem;
    }
  }
  .pzList .time {
    text-align: right;
    box-sizing: border-box;
    padding: 0 10px 10px 0px;
    color: #949494;
    font-size: 14px;
  }
}
</style>
