<template>
  <div class="preview_container">
    <el-card>
      <div class="paper_header">
        <div class="paper_title">{{ paperName }}</div>
        <div class="operation">
          <div class="shoucan">
            <div v-if="collectFlag" class="fx-row ai-center" @click="cancelCollect()">
              <img src="@/assets/images/icon_shoucang_s.png" alt=""/>
              <span class="sc_text">已收藏</span>
            </div>
            <div v-else @click="Collect()" class="fx-row ai-center">
              <img src="@/assets/images/icon_shoucang_n.png" alt=""/>
              <span class="sc_text">收藏</span>
            </div>
            <span class="btn" @click="paperDownLoad(paperId)">下载试卷</span>
          </div>
        </div>
      </div>
      <!-- 题目 -->
      <div class="paper_questions" id="question_list">
        <div class="que_item" v-for="(item, index) in opticList" :key="item.id">
          <div class="que_content" style="display: flex">
            <div>{{ index + 1 }}.</div>
            <span v-html="item.title"></span>
          </div>
          <div class="option-list">
            <span v-html="'A&nbsp'+item.optionA" v-if="item.optionA != ''"></span>
            <span v-html="'B&nbsp'+item.optionB" v-if="item.optionB != ''"></span>
            <span v-html="'C&nbsp'+item.optionC" v-if="item.optionC != ''"></span>
            <span v-html="'D&nbsp'+item.optionD" v-if="item.optionD != ''"></span>
          </div>
          <div class="subQueBox" v-if="item.issub == 1 && item.subquestions != null">
            <div class="subQueItem" v-for="(item,index) in item.subquestions" :key="index" v-html="item.title">

            </div>
          </div>
          <div class="que_footer">
            <div class="spans">
              <span class="id">ID: {{ item.id }}</span>
              <span>题型： {{ item.qtpye }}</span>
              <span>难度： 一般</span>
            </div>
            <div class="operation">
              <div class="shoucan">
                <div v-show="item.collect" @click="toCancelCollectQue(item)" style="display: flex;align-items: center;">
                  <img src="@/assets/images/icon_shoucang_s.png" alt=""/>
                  <span>已收藏</span>
                </div>
                <div v-show="!item.collect" @click="toCollectQue(item)" style="display: flex;align-items: center;">
                  <img src="@/assets/images/icon_shoucang_n.png" alt=""/>
                  <span>收藏</span>
                </div>
              </div>
              <div class="jiucuo mr20" @click="$refs.correct.open(item.id)">
                <img src="@/assets/images/icon_jiucuo.png" alt=""/>
                <span>纠错</span>
              </div>
              <div class="detail" @click="handleParseVisible(item)">
                <img src="@/assets/images/icon_chakan.png" alt=""/>
                <span>查看详情>></span>
              </div>
            </div>
          </div>
          <!-- NOTE: 改v-if否则全部公式渲染太费时间 -->
          <div :id="`question_parse_${item.id}`" v-if="item.updateTime" class="info">
            <div v-html="'【解答】'+item.answer2" class="mb20"></div>
            <div v-html="'【解析】'+item.parse"></div>
          </div>
        </div>
      </div>
    </el-card>
    <correct-question ref="correct"></correct-question>
  </div>
</template>
<script>
import {
  downloadRealPaper,
  isCollected,
  papersCancelCollect,
  papersCollect,
  preview,
  queCancelCollect,
  queCollect
} from '@/api/webApi/webQue.js'
import { mapGetters } from 'vuex'
import correctQuestion from '../../components/MxPaper/plus/correct-question-dialog.vue'

export default {
  components: { correctQuestion },
  data() {
    return {
      collectFlag: false,
      paperId: 0,
      paperName: '',
      opticList: []
    }
  },
  computed: {
    ...mapGetters(['period'])
  },
  created() {
    this.paperId = this.$route.query.paperId
    this.paperName = this.$route.query.paperName
    isCollected({ paperId: this.paperId }).then((res) => {
      console.log(res)
      this.collectFlag = res.data
    })
    this.getOptics()
  },
  methods: {
    getOptics() {
      preview({ paperId: this.paperId }).then((res) => {
        this.opticList = res.data
        setTimeout(_ => this.mxGlobal.MathQueue('question_list'))
      })
    },
    async handleParseVisible(item) {
      item.updateTime = !item.updateTime
      const parseEl = `question_parse_${item.id}`
      this.$nextTick(_ => this.mxGlobal.MathQueue(parseEl))
    },
    toCollectQue(item) {
      queCollect({ questionId: item.id }).then((res) => {
        item.collect = !item.collect
        this.msgSuccess('收藏成功')
        console.log(res)
      })
    },
    toCancelCollectQue(item) {
      queCancelCollect({ questionId: item.id }).then((res) => {
        item.collect = !item.collect
        this.msgSuccess('取消收藏成功')
        console.log(res)
      })
    },
    cancelCollect() {
      papersCancelCollect({ paperId: this.paperId }).then((res) => {
        this.collectFlag = false
        this.msgSuccess('取消收藏成功')
      })
    },
    Collect() {
      papersCollect({ paperId: this.paperId }).then((res) => {
        this.collectFlag = true
        this.msgSuccess('收藏成功')
      })
    },
    paperDownLoad(paperId) {
      downloadRealPaper(paperId, this.period)
    }
  }
}
</script>
<style scoped>
.preview_container {
  padding: 20px;
}

.paper_header {
  overflow: hidden;
  margin-bottom: 33px;
}

.paper_title {
  height: 25px;
  font-size: 18px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #292929;
  line-height: 25px;
  float: left;
}

.operation {
  float: right;
}

.shoucan {
  display: flex;
  align-items: center;
  margin-right: 53px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
}

.shoucan img {
  margin-right: 6px;
  cursor: pointer;
}

.shoucan .sc_text {
  color: #9f9f9f;
  line-height: 20px;
  cursor: pointer;
  margin-right: 28px;
}

.shoucan .btn {
  padding: 6px 20px;
  cursor: pointer;
  background: #47c6a2;
  color: white;
  border-radius: 4px;
}

.que_item {
  border-radius: 1px;
  border: 1px solid #dedede;
  margin-bottom: 8px;
}

.que_content {
  padding: 12px 24px 0 33px;
  font-size: 14px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #4c4c4c;
  line-height: 27px;
  margin-bottom: 10px;
}

.options {
  padding-left: 36px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #4c4c4c;
  line-height: 27px;
  border-bottom: 1px solid #dedede;
}

.options .option {
  margin-bottom: 10px;
}

.que_footer {
  border-top: 1px solid #dedede;
  padding-left: 33px;
  overflow: hidden;
  padding-bottom: 23px;
  padding-top: 21px;
}

.que_footer .spans {
  float: left;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #979797;
  line-height: 20px;
}

.que_footer .spans > span {
  margin-right: 20px;
}

.operation {
  display: flex;
  align-items: center;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #47c6a2;
  line-height: 20px;
}

.operation > div {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.operation .shoucan {
  margin-right: 46px;
}

.operation .detail span {
  border-radius: 1px;
  border-bottom: 1px solid #47c6a2;
}

.operation .detail {
  margin-right: 32px;
}

.operation > div > img {
  margin-right: 10px;
}

.operation .addQue {
  padding: 7px;
  border-radius: 4px;
  border: 1px solid #979797;
  margin-right: 24px;
}

.info {
  padding: 12px 40px;
}

.option-list {
  display: flex;
  flex-direction: column;
  margin-bottom: 40px;
  font-size: 14px;
}

.option-list span {
  padding: 5px 40px;
  clear: both;
}

.subQueBox {
  padding: 5px 40px;
}

.subQueBox .subQueItem {
  margin-bottom: 10px;
  font-size: 14px;
}
</style>
