<template>
  <div style="padding: 20px" class=".answer-details-container">
    <!-- 头部 -->
    <el-card>
      <div class="header">
        <div class="header_tit">
          <span>答题情况</span>
          <span class="more" @click="onShowJuYiFanSan" v-if="knowledgeId"
            >举一反三</span
          >
        </div>
        <!-- 基本信息 -->
        <table class="basic_info">
          <tr>
            <th>科目</th>
            <th>正确</th>
            <th>错误</th>
            <th>正确率</th>
            <th>用时</th>
            <th>日期</th>
          </tr>
          <tbody>
            <td>{{ topInfo.coursename || "" }}</td>
            <td>{{ topInfo.rights || 0 }}</td>
            <td>{{ topInfo.wrongs || 0 }}</td>
            <td>{{ topInfo.rate || 0 }}</td>
            <td>{{ topInfo.seconds || 0 }} 秒</td>
            <td>{{ topInfo.day || "0000-00-00" }}</td>
          </tbody>
        </table>
      </div>
    </el-card>
    <!-- 做题详情 -->
    <el-card class="info" id="question_list">
      <el-table
        ref="expandstable"
        :data="items"
        style="width: 100%"
        align="center"
        @expand-change="expandChange"
      >
        <el-table-column type="index" label="序号" width="150px">
        </el-table-column>
        <el-table-column
          label="章节"
          width="500"
        ><template>
          {{topInfo.chapterName}}
        </template></el-table-column>
        <el-table-column prop="detail.answer" label="你的答案">
        </el-table-column>
        <el-table-column prop="detail.correct" label="正确答案">
        </el-table-column>
        <el-table-column label="对错">
          <template slot-scope="scope">
            <img
              v-if="scope.row.detail.answer == scope.row.detail.correct"
              src="@/assets/images/icon_right.png"
            />
            <img v-else src="@/assets/images/icon_wrong.png" />
          </template>
        </el-table-column>
        <el-table-column type="expand" label="操作">
          <template slot-scope="scope">
            <!-- 题目解析 -->
            <div class="que_info">
              <div class="collect">
                <img src="" alt="" />
              </div>
              <div class="info_right">
                <div class="que_tit">
                  <div>【题文】</div>
                  <div class="center" v-html="scope.row.title"></div>
                  <div class="fix">
                    <span>正确答案</span>
                    <span>{{ scope.row.answer1 }}</span>
                  </div>
                </div>
                <div class="option-list">
                  <div class="option-item">
                    <span class="option-symbol">A</span>
                    <span v-html="scope.row.optionA"></span>
                  </div>
                  <div class="option-item">
                    <span class="option-symbol">B</span>
                    <span v-html="scope.row.optionB"></span>
                  </div>
                  <div class="option-item">
                    <span class="option-symbol">C</span>
                    <span v-html="scope.row.optionC"></span>
                  </div>
                  <div class="option-item">
                    <span class="option-symbol">D</span>
                    <span v-html="scope.row.optionD"></span>
                  </div>
                </div>
                <div class="que_options"></div>
                <div class="prase">
                  <div class="jiexi">解析</div>
                  <div v-html="scope.row.parse"></div>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <template slot="empty">
          <div class="empty-box">
            <img src="@/assets/images/icon_data.png" />
            <span>没有信息</span>
          </div>
        </template>
      </el-table>
    </el-card>
    <div class="btn">
      <span @click="backPractice">{{returnText}}</span>
    </div>
    <!-- 弹出警告 -->
    <el-dialog title="举一反三" :visible.sync="isShowJuYiFanSan" width="80%">
      <div class="paper_questions" id="question_simulate">
        <div class="que_item" v-for="(item, index) in queList" :key="item.id">
          <!-- 题目 -->
          <div class="que_content" style="display: flex">
            <div>{{ index + 1 }}.</div>
            <span v-html="item.title"></span>
          </div>
          <!-- 选项 -->
          <div class="options">
            <el-radio-group
              text-color="#ccc"
              v-model="item['group']"
              @change="onOptionClick(item)"
            >
              <div class="option" v-if="item.optionA">
                <el-radio label="A">
                  <span class="option-symbol">A</span>
                  <span v-html="item.optionA"></span>
                </el-radio>
              </div>
              <div class="option" v-if="item.optionB">
                <el-radio label="B">
                  <span class="option-symbol">B</span>
                  <span v-html="item.optionB"></span>
                </el-radio>
              </div>
              <div class="option" v-if="item.optionC">
                <el-radio label="C">
                  <span class="option-symbol">C</span>
                  <span v-html="item.optionC"></span>
                </el-radio>
              </div>
              <div class="option" v-if="item.optionD">
                <el-radio label="D">
                  <span class="option-symbol">D</span>
                  <span v-html="item.optionD"></span>
                </el-radio>
              </div>
            </el-radio-group>
          </div>
          <!-- :class="item['group'][0]==item.answer1?'success':'error'" -->
          <div class="is-correct" v-if="item['group'].length > 0">
            <div class="correct-text" :class="item['group'][0]==item.answer1?'success':'error'">正确答案:{{ item.answer1 }}</div>
            <div class="prase">
              <div class="jiexi">解析</div>
              <div v-html="item.parse"></div>
            </div>
          </div>
        </div>
        <el-button @click="onShowJuYiFanSan">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  diagnosticRecordsDetail,
  getQuestionsByIds,
  drawInferences,
} from "@/api/webApi/webQue.js";
export default {
  data() {
    return {
      returnText: '返回练习',
      topInfo: {},
      subjectName: "",
      wrongNum: 0,
      seconds: 0,
      isShowJuYiFanSan: false,
      date: "",
      correctPercent: "",
      correctNum: 0,
      items: [],
      chapterName: "",
      batchno: 0,
      knowledgeId: 0,
      recordsDetail: {},
      queList: [],
      answerForm: [],
    };
  },

  created() {
    let query = this.$route.query;
    let info = query.info;
    if (query.hasOwnProperty("knowledgeId")) {
      this.knowledgeId = query.knowledgeId;
    }
    if (query.hasOwnProperty("batchno")) {
      this.batchno = query.batchno;
    }
    if (query.hasOwnProperty("type") && query.type == 1) {
      this.formatData(JSON.parse(decodeURIComponent(info)));
      return;
    } else {
      this.topInfo = JSON.parse(decodeURIComponent(info));
      if (this.topInfo.hasOwnProperty("rate")) {
        this.topInfo["rate"] = this.topInfo["rate"] + "%";
      }
      console.log("topInfo", this.topInfo);
      this.httpDiagnosticRecordsDetail();
    }
  },
  methods: {
    onShowJuYiFanSan() {
      this.isShowJuYiFanSan = !this.isShowJuYiFanSan;
      if (this.isShowJuYiFanSan) {
        this.httpDrawInferences();
        this.$nextTick(_=>this.mxGlobal.MathQueue('question_simulate'))
      }
    },
    httpDiagnosticRecordsDetail() {
      diagnosticRecordsDetail({
        batchno: this.batchno,
      }).then((res) => {
        this.getQuestionIds(res.rows);
      });
    },
    httpDrawInferences() {
      drawInferences({
        knowledgeId: this.knowledgeId,
      }).then((res) => {
        this.queList = this.formatQueData(res.data);
        console.log(res);
      });
    },
    formatQueData(data) {
      let len = data.length;
      for (let i = 0; i < len; i++) {
        data[i]["group"] = [];
      }
      return data;
    },
    onOptionClick(res, row) {
      console.log(res);
      console.log(row);
    },
    formatData(data) {
      data["coursename"] = data["subjectName"];
      data["rights"] = data["correctNum"];
      data["wrongs"] = data["wrongNum"];
      data["rate"] = data["correctPercent"];
      data["day"] = data["date"];
      let list = data.items;
      let len = list.length;
      let qList = [];
      for (let i = 0; i < len; i++) {
        let data = list[i]["question"];
        data["detail"] = list[i]["question"];
        data["detail"]["answer"] = list[i]["yourAnswer"];
        data["detail"]["correct"] = list[i]["correctAnswer"];
        qList.push(data);
      }
      this.items = qList;
      this.topInfo = data;
    },
    getQuestionIds(data) {
      let len = data.length;
      let ids = [];
      for (let i = 0; i < len; i++) {
        let id = data[i]["questionid"];
        ids.push(id);
        this.recordsDetail[id] = data[i];
      }
      getQuestionsByIds({
        ids: ids.toString(),
      }).then((res) => {
        this.formatQusestion(res.data);
      });
    },
    formatQusestion(questionList) {
      let len = questionList.length;
      for (let i = 0; i < len; i++) {
        let qid = questionList[i]["id"];
        if (this.recordsDetail[qid]) {
          questionList[i]["detail"] = this.recordsDetail[qid];
        }
      }
      console.log(questionList);
      this.items = questionList;
      this.$nextTick(_=>this.mxGlobal.MathQueue('question_list'))
    },
    expandChange(item, rows) {
      this.$nextTick(_=>this.mxGlobal.MathQueue('question_list'))
    },
    expandsHandle(row) {
      this.$refs.expandstable.toggleRowExpansion(row, true);
      this.$nextTick(_=>this.mxGlobal.MathQueue('question_list'))
    },
    backPractice() {
      this.$router.push({
        path: "/question-center/smartExercise",
      });
    },
  },
};
</script>
<style scoped>
@import "~@/views/questioncenter/practice-answer-detail.scss";
</style>
