<template>
  <div class="cp-container" v-if="panelVisible">
    <div class="cp-title">
      <span class="back" @click="back"></span>
      <span class="title" v-if="isTeacher">问答作业</span>
      <span class="title" v-else>问答作业-{{ sStudent.student }}</span>
      <span class="btn" v-if="type == 1" @click="addPz(type)">提交</span>
    </div>
    <div class="cp-box">
      <div class="cp-quest">提问：{{ answerQ }}</div>
      <div class="cp-conent" v-if="type == 1">
        <van-field v-model="message" rows="20" autosize type="textarea" placeholder="请输入..." />
      </div>
    </div>
  </div>
</template>

<script>
import { addCourseWorks, addCourseWorksTeacher } from '@/api/course'
import { mapGetters } from 'vuex'

export default {
  props: {
    panelVisible: {
      type: Boolean
    },
    courseid: {
      type: String,
      default: ''
    },
    courseType: {
      type: Number,
      default: 0
    },
    taskCount: {
      type: Number,
      default: 0
    },
    toolindex: {
      type: Number
    },
    isTeacher: {
      type: Boolean
    },
    answerQ: {
      type: String
    },
    sStudent: {
      type: Object
    }
  },
  components: {},
  data() {
    return {
      type: 1,
      message: '',
      imgList: []
    }
  },
  computed: {
    ...mapGetters(['userinfo'])
  },
  methods: {
    back() {
      this.$emit('setPanelVisible', false)
    },
    setType(type) {
      this.type = type
    },
    getImage(imgList) {
      this.imgList = imgList
      this.$forceUpdate()
    },
    addPz() {
      const fun = [addCourseWorks, addCourseWorksTeacher]
      const answerList = [
        {
          answerTitle: this.answerQ.replaceAll(/%/g, '%25'),
          answer: this.message.replaceAll(/%/g, '%25')
        }
      ]
      let params = []
      if (this.isTeacher) {
        params = [
          {
            uid: this.userinfo.userid,
            cid: this.courseid,
            stage: this.courseType,
            task: this.taskCount,
            tool: this.toolindex,
            content: JSON.stringify(answerList),
            type: 3
          }
        ]
      } else {
        params = [
          {
            uid: this.sStudent.userid,
            cid: this.courseid,
            stage: this.courseType,
            task: this.taskCount,
            tool: this.toolindex,
            content: JSON.stringify(answerList),
            type: 3,
            ateacher: this.userinfo.userid
          }
        ]
      }
      fun[this.isTeacher ? 0 : 1](params)
        .then(res => {
          this.$toast({
            message: '上传成功',
            type: 'success'
          })
          this.$emit('setPanelVisible', false)
        })
        .catch(err => {
          console.error(err)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.cp-container {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  background: #fff;
  z-index: 2;
  .cp-title {
    height: 1.5rem;
    width: 100%;
    border-bottom: 1px solid #cecece;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    position: relative;
    .back {
      width: 0.3rem;
      height: 0.3rem;
      border-top: 2px solid #000000;
      border-left: 2px solid #000000;
      position: absolute;
      transform: rotate(-45deg) translateY(-50%);
      top: 47%;
      left: 0.8rem;
      cursor: pointer;
    }
    .title {
      font-size: 16px;
    }
    .btn {
      font-size: 14px;
      transform: translateY(-50%);
      top: 50%;
      right: 0.8rem;
      position: absolute;
      color: #2274ff;
      cursor: pointer;
    }
  }
  .cp-box {
    width: 100%;
    height: calc(100% - 1.5rem);
    overflow: auto;
    .type-nav {
      height: 60px;
      width: 100%;
      display: flex;
      align-items: center;
      padding: 10px 0.3rem;
      box-sizing: border-box;

      .type-nav-box {
        height: 100%;
        opacity: 0.5;
        transition: all 0.5s;

        + .type-nav-box {
          margin-left: 0.3rem;
        }

        &.active {
          opacity: 1;
        }
        > img {
          height: 100%;
        }
      }
    }
    .cp-quest {
      width: 100%;
      padding: 10px 15px;
      font-size: 18px;
      word-break: break-all;
      box-sizing: border-box;
    }
    .cp-conent {
      // margin-top: 10px;
      height: auto;
      overflow: auto;
      font-size: 14px;
      width: 100%;
      /deep/ .van-field__control {
        // height: auto !important;
      }
    }
    .cp-audio {
      height: calc(100% - 60px);
      position: relative;
      width: 100%;
    }
  }
}
</style>
