<template>
  <div class="course-exam">
    <Header></Header>
    <section class="section">
      <div class="container">
        <div class="section__header">
          <!-- <el-breadcrumb separator="/">
              <el-breadcrumb-item
                v-for="(item, index) in $route.matched"
                :key="index"
                :to="{ path: item.path }"
                >{{ item.name }}</el-breadcrumb-item
              >
            </el-breadcrumb> -->
        </div>
        <div class="section__body">
          <div class="explain-record">
            <div class="explain-record__body">
              <div class="left-box">
                <Header-tab-box
                  :num="questionOverNum(true)"
                  @prevQuestion="prevQuestion"
                  :allNum="questionList.length"
                  @nextQuestion="nextQuestion"
                ></Header-tab-box>
                <div class="left-box__body">
                  <template v-for="(question, questionIndex) in questionList">
                    <div
                      class="question"
                      v-if="question.type == 1 && current == questionIndex"
                      :key="questionIndex"
                    >
                      <div class="question__title">
                        {{ questionIndex + 1 }}、单选题
                      </div>
                      <div
                        class="question__desc"
                        v-html="question.content"
                      ></div>
                      <div class="question__content">
                        <div class="question-list" v-if="!question.ques">
                          <div
                            class="radio"
                            v-for="(item, index) in question.jsonStr"
                            :key="index"
                            @click="
                              radioSelect(
                                question,
                                questionIndex,
                                item.optionsId
                              )
                            "
                          >
                            <div>
                              {{ ast[index] }}. {{ item.content }}
                              <div v-if="item.imgUrl">
                                <img
                                  style="max-width: 100%"
                                  :src="$tools.splitImgHost(item.imgUrl)"
                                  alt=""
                                />
                              </div>
                            </div>
                          </div>
                        </div>
                        <div class="question-list" v-if="question.ques">
                          <div
                            class="radio"
                            :class="{
                              right:
                                bankType == 1 &&
                                (item.optionsId == question.ques ||
                                  item.optionsId == question.ans),
                              wrong:
                                bankType == 1 &&
                                item.optionsId == question.ques &&
                                question.ques != question.ans,
                              user_choose:
                                bankType == 2 && item.optionsId == question.ques
                            }"
                            v-for="(item, index) in question.jsonStr"
                            :key="index"
                            @click="
                              radioSelect(
                                question,
                                questionIndex,
                                item.optionsId
                              )
                            "
                          >
                            <div>
                              {{ ast[index] }}. {{ item.content }}
                              <div v-if="item.imgUrl">
                                <img
                                  style="max-width: 100%"
                                  :src="$tools.splitImgHost(item.imgUrl)"
                                  alt=""
                                />
                              </div>
                            </div>
                          </div>
                        </div>
                        <div
                          class="answer-list"
                          v-if="bankType == 1 && question.ques"
                        >
                          <div class="answer-list__left">
                            题目答案：{{ ast[question.ans - 1] }}
                          </div>
                          <div class="answer-list__left">
                            我的答案：{{ ast[question.ques - 1] }}
                          </div>
                        </div>
                        <div
                          class="explain-list"
                          v-if="bankType == 1 && question.ques"
                        >
                          <div class="explain-list__header">答案解析：</div>
                          <div
                            class="explain-list__body"
                            v-html="question.analysisContent"
                          ></div>
                        </div>
                      </div>
                    </div>

                    <div
                      class="question"
                      v-if="question.type == 2 && current == questionIndex"
                      :key="questionIndex"
                    >
                      <div class="question__title">
                        {{ questionIndex + 1 }}、多选题
                      </div>
                      <div
                        class="question__desc"
                        v-html="question.content"
                      ></div>
                      <div class="question__content">
                        <div class="question-list" v-if="!question.ques">
                          <el-checkbox
                            class="checkbox"
                            v-for="(item, index) in question.jsonStr"
                            :key="index"
                            :label="item.optionsId"
                            v-model="item.checked"
                          >
                            <div>
                              {{ ast[index] }}. {{ item.content }}
                              <div v-if="item.imgUrl">
                                <img
                                  style="max-width: 100%"
                                  :src="$tools.splitImgHost(item.imgUrl)"
                                  alt=""
                                />
                              </div>
                            </div>
                          </el-checkbox>
                        </div>
                        <div class="question-list" v-if="question.ques">
                          <el-checkbox
                            :disabled="bankType == 2 ? false : true"
                            class="checkbox"
                            :class="{
                              right:
                                bankType == 1 &&
                                (question.ques.indexOf(item.optionsId) != -1 ||
                                  question.ans.indexOf(item.optionsId) != -1),
                              wrong:
                                bankType == 1 &&
                                question.ques.indexOf(item.optionsId) != -1 &&
                                question.ans.indexOf(item.optionsId) == -1,
                              user_choose:
                                bankType == 2 &&
                                question.ques.indexOf(item.optionsId) != -1
                            }"
                            v-for="(item, index) in question.jsonStr"
                            :key="index"
                            :label="item.optionsId"
                            v-model="item.checked"
                          >
                            <div>
                              {{ ast[index] }}. {{ item.content }}
                              <div v-if="item.imgUrl">
                                <img
                                  style="max-width: 100%"
                                  :src="$tools.splitImgHost(item.imgUrl)"
                                  alt=""
                                />
                              </div>
                            </div>
                          </el-checkbox>
                        </div>
                        <div
                          class="answer-list"
                          v-if="bankType == 1 && question.ques"
                        >
                          <div class="answer-list__left">
                            题目答案：
                            <template v-for="ansItem in question.ans">{{
                              ast[ansItem - 1]
                            }}</template>
                          </div>
                          <div class="answer-list__left">
                            我的答案：
                            <template v-for="quesItem in question.ques">{{
                              ast[quesItem - 1]
                            }}</template>
                          </div>
                        </div>
                        <div
                          class="explain-list"
                          v-if="bankType == 1 && question.ques"
                        >
                          <div class="explain-list__header">答案解析：</div>
                          <div
                            class="explain-list__body"
                            v-html="question.analysisContent"
                          ></div>
                        </div>
                      </div>
                      <div class="question__btns">
                        <div
                          v-if="
                            bankType == 2 || (bankType == 1 && !question.ques)
                          "
                          class="submit"
                          @click="checkboxSubmit(question, questionIndex)"
                        >
                          确认答案
                        </div>
                      </div>
                    </div>

                    <div
                      class="question"
                      v-if="question.type == 3 && current == questionIndex"
                      :key="questionIndex"
                    >
                      <div class="question__title">
                        {{ questionIndex + 1 }}、判断题
                      </div>
                      <div
                        class="question__desc"
                        v-html="question.content"
                      ></div>
                      <div class="question__content">
                        <div class="question-list" v-if="!question.ques">
                          <div
                            class="radio"
                            v-for="(item, index) in judge"
                            :key="index"
                            @click="judgeSelect(question, questionIndex, index)"
                          >
                            <div>
                              {{ ast[index] }}. {{ item }}
                              <div v-if="item.imgUrl">
                                <img
                                  style="max-width: 100%"
                                  :src="$tools.splitImgHost(item.imgUrl)"
                                  alt=""
                                />
                              </div>
                            </div>
                          </div>
                        </div>
                        <div class="question-list" v-if="question.ques">
                          <div
                            class="radio"
                            :class="{
                              right:
                                bankType == 1 &&
                                (index == (question.ques == 1 ? 0 : 1) ||
                                  index != question.ans),
                              wrong:
                                bankType == 1 &&
                                index == (question.ques == 1 ? 0 : 1) &&
                                question.ques != question.ans,
                              user_choose:
                                bankType == 2 &&
                                index == (question.ques == 1 ? 0 : 1)
                            }"
                            v-for="(item, index) in judge"
                            :key="index"
                            @click="judgeSelect(question, questionIndex, index)"
                          >
                            <div>
                              {{ ast[index] }}. {{ item }}
                              <div v-if="item.imgUrl">
                                <img
                                  style="max-width: 100%"
                                  :src="$tools.splitImgHost(item.imgUrl)"
                                  alt=""
                                />
                              </div>
                            </div>
                          </div>
                        </div>
                        <div
                          class="answer-list"
                          v-if="bankType == 1 && question.ques"
                        >
                          <div class="answer-list__left">
                            题目答案：{{ ast[question.ans == 1 ? 0 : 1] }}
                          </div>
                          <div class="answer-list__left">
                            我的答案：{{ ast[question.ques == 1 ? 0 : 1] }}
                          </div>
                        </div>
                        <div
                          class="explain-list"
                          v-if="bankType == 1 && question.ques"
                        >
                          <div class="explain-list__header">答案解析：</div>
                          <div
                            class="explain-list__body"
                            v-html="question.analysisContent"
                          ></div>
                        </div>
                      </div>
                    </div>

                    <div
                      class="question"
                      v-if="question.type == 4 && current == questionIndex"
                      :key="questionIndex"
                    >
                      <div class="question__title">
                        {{ questionIndex + 1 }}、案例题
                      </div>
                      <div
                        class="question__desc"
                        v-html="question.content"
                      ></div>
                      <div class="question__content">
                        <el-tabs v-model="question.tabIndex">
                          <el-tab-pane
                            v-for="(json, jsonIndex) in question.jsonStr"
                            :label="'问题' + (jsonIndex + 1)"
                            :name="jsonIndex + ''"
                            :key="jsonIndex"
                          >
                            <div
                              class="question"
                              v-if="json.type == 1"
                              :key="questionIndex"
                            >
                              <div class="question__title">
                                {{ jsonIndex + 1 }}、单选题
                              </div>
                              <div
                                class="question__desc"
                                v-html="json.content"
                              ></div>
                              <div class="question__content">
                                <div
                                  class="question-list"
                                  v-if="!question.ques[jsonIndex]"
                                >
                                  <div
                                    class="radio"
                                    v-for="(item, index) in json.optionsList"
                                    :key="index"
                                    @click="
                                      radioSelectChild(
                                        questionIndex,
                                        jsonIndex,
                                        item.optionsId
                                      )
                                    "
                                  >
                                    <div>
                                      {{ ast[index] }}. {{ item.content }}
                                      <div v-if="item.imgUrl">
                                        <img
                                          style="max-width: 100%"
                                          :src="
                                            $tools.splitImgHost(item.imgUrl)
                                          "
                                          alt=""
                                        />
                                      </div>
                                    </div>
                                  </div>
                                </div>
                                <div
                                  class="question-list"
                                  v-if="question.ques[jsonIndex]"
                                >
                                  <div
                                    class="radio"
                                    :class="{
                                      right:
                                        bankType == 1 &&
                                        (item.optionsId ==
                                          question.ques[jsonIndex] ||
                                          item.optionsId ==
                                            question.ans[jsonIndex]),
                                      wrong:
                                        bankType == 1 &&
                                        item.optionsId ==
                                          question.ques[jsonIndex] &&
                                        question.ques[jsonIndex] !=
                                          question.ans[jsonIndex],
                                      user_choose:
                                        bankType == 2 &&
                                        item.optionsId ==
                                          question.ques[jsonIndex]
                                    }"
                                    v-for="(item, index) in json.optionsList"
                                    :key="index"
                                    @click="
                                      radioSelectChild(
                                        questionIndex,
                                        jsonIndex,
                                        item.optionsId
                                      )
                                    "
                                  >
                                    <div>
                                      {{ ast[index] }}. {{ item.content }}
                                      <div v-if="item.imgUrl">
                                        <img
                                          style="max-width: 100%"
                                          :src="
                                            $tools.splitImgHost(item.imgUrl)
                                          "
                                          alt=""
                                        />
                                      </div>
                                    </div>
                                  </div>
                                </div>
                                <div
                                  class="answer-list"
                                  v-if="
                                    bankType == 1 && question.ques[jsonIndex]
                                  "
                                >
                                  <div class="answer-list__left">
                                    题目答案：{{
                                      ast[question.ans[jsonIndex] - 1]
                                    }}
                                  </div>
                                  <div class="answer-list__left">
                                    我的答案：{{
                                      ast[question.ques[jsonIndex] - 1]
                                    }}
                                  </div>
                                </div>
                                <div
                                  class="explain-list"
                                  v-if="
                                    bankType == 1 && question.ques[jsonIndex]
                                  "
                                >
                                  <div class="explain-list__header">
                                    答案解析：
                                  </div>
                                  <div
                                    class="explain-list__body"
                                    v-html="json.analysisContent"
                                  ></div>
                                </div>
                              </div>
                              <div class="question__btns"></div>
                            </div>

                            <div
                              class="question"
                              v-if="json.type == 2"
                              :key="jsonIndex"
                            >
                              <div class="question__title">
                                {{ jsonIndex + 1 }}、多选题
                              </div>
                              <div
                                class="question__desc"
                                v-html="json.content"
                              ></div>
                              <div class="question__content">
                                <div
                                  class="question-list"
                                  v-if="!question.ques[jsonIndex]"
                                >
                                  <el-checkbox
                                    class="checkbox"
                                    v-for="(item, index) in json.optionsList"
                                    :key="index"
                                    :label="item.optionsId"
                                    v-model="item.checked"
                                  >
                                    <div>
                                      {{ ast[index] }}. {{ item.content }}
                                      <div v-if="item.imgUrl">
                                        <img
                                          style="max-width: 100%"
                                          :src="
                                            $tools.splitImgHost(item.imgUrl)
                                          "
                                          alt=""
                                        />
                                      </div>
                                    </div>
                                  </el-checkbox>
                                </div>
                                <div
                                  class="question-list"
                                  v-if="question.ques[jsonIndex]"
                                >
                                  <el-checkbox
                                    :disabled="bankType == 2 ? false : true"
                                    class="checkbox"
                                    :class="{
                                      right:
                                        bankType == 1 &&
                                        (question.ques[jsonIndex].indexOf(
                                          item.optionsId
                                        ) != -1 ||
                                          question.ans[jsonIndex].indexOf(
                                            item.optionsId
                                          ) != -1),
                                      wrong:
                                        bankType == 1 &&
                                        question.ques[jsonIndex].indexOf(
                                          item.optionsId
                                        ) != -1 &&
                                        question.ans[jsonIndex].indexOf(
                                          item.optionsId
                                        ) == -1,
                                      user_choose:
                                        bankType == 2 &&
                                        question.ques[jsonIndex].indexOf(
                                          item.optionsId
                                        ) != -1
                                    }"
                                    v-for="(item, index) in json.optionsList"
                                    :key="index"
                                    :label="item.optionsId"
                                    v-model="item.checked"
                                  >
                                    <div>
                                      {{ ast[index] }}. {{ item.content }}
                                      <div v-if="item.imgUrl">
                                        <img
                                          style="max-width: 100%"
                                          :src="
                                            $tools.splitImgHost(item.imgUrl)
                                          "
                                          alt=""
                                        />
                                      </div>
                                    </div>
                                  </el-checkbox>
                                </div>
                                <div
                                  class="answer-list"
                                  v-if="
                                    bankType == 1 && question.ques[jsonIndex]
                                  "
                                >
                                  <div class="answer-list__left">
                                    题目答案：
                                    <template
                                      v-for="ansItem in question.ans[jsonIndex]"
                                      >{{ ast[ansItem - 1] }}</template
                                    >
                                  </div>
                                  <div class="answer-list__left">
                                    我的答案：
                                    <template
                                      v-for="quesItem in question.ques[
                                        jsonIndex
                                      ]"
                                      >{{ ast[quesItem - 1] }}</template
                                    >
                                  </div>
                                </div>
                                <div
                                  class="explain-list"
                                  v-if="
                                    bankType == 1 && question.ques[jsonIndex]
                                  "
                                >
                                  <div class="explain-list__header">
                                    答案解析：
                                  </div>
                                  <div
                                    class="explain-list__body"
                                    v-html="json.analysisContent"
                                  ></div>
                                </div>
                              </div>
                              <div class="question__btns">
                                <div
                                  v-if="
                                    bankType == 2 ||
                                      (bankType == 1 &&
                                        !question.ques[jsonIndex])
                                  "
                                  class="submit"
                                  @click="
                                    checkboxSubmitChild(
                                      questionIndex,
                                      jsonIndex
                                    )
                                  "
                                >
                                  确认答案
                                </div>
                              </div>
                            </div>

                            <div
                              class="question"
                              v-if="json.type == 3"
                              :key="jsonIndex"
                            >
                              <div class="question__title">
                                {{ jsonIndex + 1 }}、判断题
                              </div>
                              <div
                                class="question__desc"
                                v-html="json.content"
                              ></div>
                              <div class="question__content">
                                <div
                                  class="question-list"
                                  v-if="!question.ques[jsonIndex]"
                                >
                                  <div
                                    class="radio"
                                    v-for="(item, index) in judge"
                                    :key="index"
                                    @click="
                                      judgeSelectChild(
                                        questionIndex,
                                        jsonIndex,
                                        index
                                      )
                                    "
                                  >
                                    <div>
                                      {{ ast[index] }}. {{ item }}
                                      <div v-if="item.imgUrl">
                                        <img
                                          style="max-width: 100%"
                                          :src="
                                            $tools.splitImgHost(item.imgUrl)
                                          "
                                          alt=""
                                        />
                                      </div>
                                    </div>
                                  </div>
                                </div>
                                <div
                                  class="question-list"
                                  v-if="question.ques[jsonIndex]"
                                >
                                  <div
                                    class="radio"
                                    :class="{
                                      right:
                                        bankType == 1 &&
                                        (index ==
                                          (question.ques[jsonIndex] == 1
                                            ? 0
                                            : 1) ||
                                          index != question.ans[jsonIndex]),
                                      wrong:
                                        bankType == 1 &&
                                        index ==
                                          (question.ques[jsonIndex] == 1
                                            ? 0
                                            : 1) &&
                                        question.ques[jsonIndex] !=
                                          question.ans[jsonIndex],
                                      user_choose:
                                        bankType == 2 &&
                                        index ==
                                          (question.ques[jsonIndex] == 1
                                            ? 0
                                            : 1)
                                    }"
                                    v-for="(item, index) in judge"
                                    :key="index"
                                    @click="
                                      judgeSelectChild(
                                        questionIndex,
                                        jsonIndex,
                                        index
                                      )
                                    "
                                  >
                                    <div>
                                      {{ ast[index] }}. {{ item }}
                                      <div v-if="item.imgUrl">
                                        <img
                                          style="max-width: 100%"
                                          :src="
                                            $tools.splitImgHost(item.imgUrl)
                                          "
                                          alt=""
                                        />
                                      </div>
                                    </div>
                                  </div>
                                </div>
                                <div
                                  class="answer-list"
                                  v-if="
                                    bankType == 1 && question.ques[jsonIndex]
                                  "
                                >
                                  <div class="answer-list__left">
                                    题目答案：{{
                                      ast[question.ans[jsonIndex] == 1 ? 0 : 1]
                                    }}
                                  </div>
                                  <div class="answer-list__left">
                                    我的答案：{{
                                      ast[question.ques[jsonIndex] == 1 ? 0 : 1]
                                    }}
                                  </div>
                                </div>
                                <div
                                  class="explain-list"
                                  v-if="
                                    bankType == 1 && question.ques[jsonIndex]
                                  "
                                >
                                  <div class="explain-list__header">
                                    答案解析：
                                  </div>
                                  <div
                                    class="explain-list__body"
                                    v-html="json.analysisContent"
                                  ></div>
                                </div>
                              </div>
                              <div class="question__btns"></div>
                            </div>

                            <div
                              class="question"
                              v-if="json.type == 5"
                              :key="jsonIndex"
                            >
                              <div class="question__title">
                                {{ jsonIndex + 1 }}、简答题
                              </div>
                              <div
                                class="question__desc"
                                v-html="json.content"
                              ></div>
                              <div class="question__content">
                                <div
                                  class="question-list textarea"
                                  v-if="
                                    bankType == 2 ||
                                      (bankType == 1 &&
                                        !(
                                          question.ques[jsonIndex] &&
                                          (question.ques[jsonIndex].imageList
                                            .length ||
                                            question.ques[jsonIndex].text)
                                        ))
                                  "
                                >
                                  <el-input
                                    type="textarea"
                                    rows="5"
                                    v-model="json.ansText.text"
                                    resize="none"
                                  ></el-input>
                                  <div class="upload clearfix">
                                    <div
                                      class="upload__imgs"
                                      v-for="(img, imgIndex) in json.ansText
                                        .imageList"
                                      :key="imgIndex"
                                    >
                                      <img
                                        style="max-width: 100%"
                                        :src="$tools.splitImgHost(img, true)"
                                        alt=""
                                      />
                                    </div>
                                    <div class="upload__btn">
                                      <i class="el-icon-plus icon"></i>
                                      <p>上传图片</p>
                                      <input
                                        @change="
                                          uploadImgChild(
                                            $event,
                                            questionIndex,
                                            jsonIndex
                                          )
                                        "
                                        type="file"
                                      />
                                    </div>
                                  </div>
                                </div>
                                <div
                                  class="explain-list"
                                  v-if="
                                    bankType == 1 &&
                                      question.ques[jsonIndex] &&
                                      (question.ques[jsonIndex].imageList
                                        .length ||
                                        question.ques[jsonIndex].text)
                                  "
                                >
                                  <div class="explain-list__header">
                                    我的答案:
                                  </div>
                                  <div class="explain-list__body">
                                    <div>
                                      {{ question.ques[jsonIndex].text }}
                                    </div>
                                    <div class="upload clearfix">
                                      <div
                                        class="upload__imgs"
                                        v-for="(img, imgIndex) in question.ques[
                                          jsonIndex
                                        ].imageList"
                                        :key="imgIndex"
                                      >
                                        <img
                                          style="max-width: 100%"
                                          :src="$tools.splitImgHost(img, true)"
                                          alt=""
                                        />
                                      </div>
                                    </div>
                                  </div>
                                  <div class="explain-list__header">
                                    答案解析：
                                  </div>
                                  <div
                                    class="explain-list__body"
                                    v-html="json.analysisContent"
                                  ></div>
                                </div>
                              </div>
                              <div class="question__btns">
                                <div
                                  v-if="
                                    bankType == 2 ||
                                      (bankType == 1 &&
                                        !(
                                          question.ques[jsonIndex] &&
                                          (question.ques[jsonIndex].imageList
                                            .length ||
                                            question.ques[jsonIndex].text)
                                        ))
                                  "
                                  class="submit"
                                  @click="
                                    ansSubmitChild(
                                      question,
                                      questionIndex,
                                      jsonIndex
                                    )
                                  "
                                >
                                  确认答案
                                </div>
                              </div>
                            </div>
                          </el-tab-pane>
                        </el-tabs>
                      </div>
                    </div>

                    <div
                      class="question"
                      v-if="question.type == 5 && current == questionIndex"
                      :key="questionIndex"
                    >
                      <div class="question__title">
                        {{ questionIndex + 1 }}、简答题
                      </div>
                      <div
                        class="question__desc"
                        v-html="question.content"
                      ></div>
                      <div class="question__content">
                        <div
                          class="question-list textarea"
                          v-if="
                            bankType == 2 ||
                              (bankType == 1 &&
                                !question.ques.imageList.length &&
                                !question.ques.text)
                          "
                        >
                          <el-input
                            type="textarea"
                            rows="5"
                            v-model="question.ansText.text"
                            resize="none"
                          ></el-input>
                          <div class="upload clearfix">
                            <div
                              class="upload__imgs"
                              v-for="(img, imgIndex) in question.ansText
                                .imageList"
                              :key="imgIndex"
                            >
                              <img
                                style="max-width: 100%"
                                :src="$tools.splitImgHost(img, true)"
                                alt=""
                              />
                            </div>
                            <div class="upload__btn">
                              <i class="el-icon-plus icon"></i>
                              <p>上传图片</p>
                              <input
                                @change="
                                  uploadImg($event, question, questionIndex)
                                "
                                type="file"
                              />
                            </div>
                          </div>
                        </div>
                        <div
                          class="explain-list"
                          v-if="
                            (bankType == 1 && question.ques.imageList.length) ||
                              question.ques.text
                          "
                        >
                          <div class="explain-list__header">我的答案:</div>
                          <div class="explain-list__body">
                            <div>{{ question.ques.text }}</div>
                            <div class="upload clearfix">
                              <div
                                class="upload__imgs"
                                v-for="(img, imgIndex) in question.ques
                                  .imageList"
                                :key="imgIndex"
                              >
                                <img
                                  style="max-width: 100%"
                                  :src="$tools.splitImgHost(img, true)"
                                  alt=""
                                />
                              </div>
                            </div>
                          </div>
                          <div class="explain-list__header">答案解析：</div>
                          <div
                            class="explain-list__body"
                            v-html="question.analysisContent"
                          ></div>
                        </div>
                      </div>
                      <div class="question__btns">
                        <div
                          v-if="
                            bankType == 2 ||
                              (bankType == 1 &&
                                !question.ques.imageList.length &&
                                !question.ques.text)
                          "
                          class="submit"
                          @click="ansSubmit(question, questionIndex)"
                        >
                          确认答案
                        </div>
                      </div>
                    </div>
                  </template>
                </div>
                <Collection-box
                  :param="param"
                  @sumit="getCollectInfo(current)"
                  :showT="!collectList[current]"
                ></Collection-box>
              </div>
              <div class="right-box">
                <div class="right-box__header">
                  <el-button
                    type="primary"
                    round
                    plain
                    size="small"
                    class="back-btn"
                    @click="$router.back(-1)"
                    >返回</el-button
                  >
                </div>

                <div class="right-box__footer">
                  <div class="submit" @click="submit">交卷</div>
                </div>
                <div class="right-box__header">
                  <div class="title">答题卡</div>
                  <div class="time" v-if="allTimes">
                    剩余时间 {{ countdown(lastTime) }}
                  </div>
                </div>
                <div class="right-box__body">
                  <div class="card">
                    <div class="card__note">
                      <div class="item">
                        <div class="box green"></div>
                        正确
                      </div>
                      <div class="item">
                        <div class="box red"></div>
                        错误
                      </div>
                      <div class="item">
                        <div class="box blue"></div>
                        已做未评改
                      </div>

                      <div class="item">
                        <div class="box yellow"></div>
                        少选
                      </div>
                      <div class="item">
                        <div class="box white"></div>
                        未做
                      </div>
                    </div>
                    <div class="card__content">
                      <ul class="list">
                        <li
                          class="item white"
                          v-for="(item, index) in questionList"
                          :key="index"
                          :class="{
                            green: bankType == 1 && isRight(item, index),
                            red: bankType == 1 && isWrong(item, index),
                            yellow: bankType == 1 && isPart(item, index),
                            blue: bankType == 1 && isOver(item, index),
                            check_ans: bankType == 2 && isCheck(item, index)
                          }"
                          @click="changeIndex(index)"
                        >
                          {{ index + 1 }}
                        </li>
                      </ul>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
    <ToolBar></ToolBar>
    <Footer></Footer>
    <takePicture
      ref="takePicture"
      @returnParameter="returnParameter"
    ></takePicture>
  </div>
</template>

<script>
import takePicture from "@/components/takePicture/index.vue";
import myMixins from "@/mixin";
import Footer from "@/components/footer/index";
import Header from "@/components/header/index";
import ToolBar from "@/components/toolbar/index";
import CollectionBox from "@/components/common/CollectionBox.vue";
import HeaderTabBox from "../../components/exam/HeaderTabBox.vue";
import { mapMutations } from "vuex";
export default {
  name: "BankExplain",
  components: {
    Footer,
    Header,
    ToolBar,
    CollectionBox,
    HeaderTabBox,
    takePicture
  },
  mixins: [myMixins],
  data() {
    return {
      openPhotoStatus: 0,
      recordId: 0,
      tabIndex: "1",
      textarea: "",
      questionIndex: 0,
      checked: false,
      questionList: [],
      judge: ["正确", "错误"],
      ast: [
        "A",
        "B",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "I",
        "J",
        "K",
        "L",
        "M",
        "N",
        "O",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "U",
        "V",
        "W",
        "X",
        "Y",
        "Z"
      ],
      mustBack: false,
      needPhoto: false,
      lastTime: 0, //剩余考试时长
      allTimes: 0, //总考试时长
      lastCount: 0,
      examId: 0,
      goodsId: 0,
      courseId: 0,
      gradeId: 0,
      moduleId: 0,
      chapterId: 0,
      sectionId: 0,
      orderGoodsId: 0,
      current: 0,
      timer: null,
      type: 0, //type:1章卷，2节卷，3模块卷
      bankType: 0,
      compareFaceData: 0, // 拍照匹配相似度
      collectList: [],
      doMode: 1,
      simulateExamId: ""
    };
  },
  async mounted() {
    this.goodsId = Number(this.$route.params.goodsId);
    this.examId = Number(this.$route.query.examId);
    this.type = Number(this.$route.query.type);
    this.courseId = Number(this.$route.query.courseId);
    this.gradeId = Number(this.$route.query.gradeId);
    this.moduleId = Number(this.$route.query.moduleId);
    this.sectionId = Number(this.$route.query.sectionId);
    this.chapterId = Number(this.$route.query.chapterId);
    this.orderGoodsId = Number(this.$route.query.orderGoodsId);
    if (
      (this.type == 1 && (!this.chapterId || this.chapterId === 0)) ||
      (this.type == 3 && (!this.moduleId || this.moduleId === 0))
    ) {
      this.$alert("数据出现未知异常，请重进或向教务人员反馈", "提示", {
        confirmButtonText: "返回上一页",
        showClose: false,
        callback: action => {
          this.mustBack = true;
          this.$router.go(-1);
        }
      });
      return;
    }
    await this.getGoodsDetail();
    await this.bankExam();
    this.needPhoto && (await this.getExamLearnStatus());
    this.goodsQuestionList();
  },
  beforeDestroy() {
    clearInterval(this.timer);
    try {
      this.$msgbox.close();
    } catch (err) {}
  },
  methods: {
    ...mapMutations(["setExamResult"]),
    toFixed(num) {
      if (num) {
        let str = String(num).indexOf(".");

        if (str != -1) {
          return +num.toFixed(2);
        } else {
          return num;
        }
      } else {
        return 0;
      }
    },
    getGoodsDetail() {
      return new Promise(resolve => {
        this.$request.goodsDetail(this.goodsId).then(res => {
          if (this.type != 2) {
            if (res.data.goodsPhotoExamConfig) {
              let goodsPhotoExamConfig = JSON.parse(
                res.data.goodsPhotoExamConfig
              );
              if (goodsPhotoExamConfig.photograph > 0) {
                this.needPhoto = true;
              }
            }
            if (res.data.erJianErZao) {
              this.needPhoto = true;
            }
          }
          resolve();
        });
      });
    },
    /**
     * @param {Object} current
     * 获取收藏信息
     */
    getCollectInfo(current) {
      this.$request
        .getCollectInfo({
          examId: this.examId,
          questionId: this.questionList[current].questionId,
          goodsId: this.goodsId,
          orderGoodsId: this.orderGoodsId,
          doMode: this.doMode
        })
        .then(res => {
          this.$set(this.collectList, current, res.data);
        })
        .catch(err => {
          this.$set(this.collectList, current, false);
        });
    },
    //提示且允许返回
    mustBackPage(msg, text) {
      this.$alert(msg || "未知异常", "提示", {
        confirmButtonText: text || "返回上一页",
        showClose: false,
        callback: action => {
          this.mustBack = true;
          this.$router.go(-1);
        }
      });
      return;
    },
    /**
     * 请求题目列表
     */
    goodsQuestionList() {
      this.$request[
        this.doMode == 3 ? "goodsRandomQuestionList" : "goodsQuestionList"
      ]({
        examId: this.examId,
        goodsId: this.goodsId,
        orderGoodsId: this.orderGoodsId,
        from: this.doMode == 3 ? 2 : ""
      })
        .then(async ({ data }) => {
          if (this.doMode == 3) {
            this.simulateExamId = data.simulateExamId;
            data = data.questionList;
          }
          if (!data.length) {
            this.mustBackPage("该试卷暂无题目");
            return;
          }
          this.questionList = this.questionOptimizeFormat(data);
          this.lastCount = this.questionList.length;
          this.getCollectInfo(this.current);
          await this.examRecord();
        })
        .catch(err => {
          if (this.doMode == 3) {
            this.mustBackPage("你已做完所有题目", "确定,并且返回上一页");
          } else {
            this.mustBackPage(err.msg);
          }
        });
    },
    //拍照
    openPhoto() {
      this.$refs.takePicture.openPhoto();
    },

    //拍照成功回显 url
    async returnParameter(url) {
      let file = this.$tools.convertBase64UrlToBlob(url);
      const photoUrl = await this.$upload.upload(file, 0, {
        gradeId: this.gradeId,
        orderGoodsId: this.orderGoodsId
      });
      let compareFaceData = await this.faceRecognition(photoUrl);
      if (compareFaceData >= 80) {
        //提交拍照记录
        if (await this.postStudyRecord(0, photoUrl, compareFaceData)) {
          this.needPhoto = false;
          if (this.openPhotoStatus == 1) {
            this.examSubmit();
            return;
          }
          if (this.allTimes) {
            this.BeginTime(); //需要开启计时器
          }
        }
      } else {
        this.$message({
          type: "warning",
          message: "人脸匹配不通过，请重新拍照上传"
        });
        setTimeout(() => {
          this.openPhoto();
        }, 1500);
      }
    },
    //需要开启计时器
    BeginTime() {
      this.timer = setInterval(() => {
        if (this.lastTime <= 0) {
          clearInterval(this.timer);
          this.$confirm("考试时间已到，系统将自动交卷", "提示", {
            showConfirmButton: false,
            closeOnClickModal: false,
            showCancelButton: false,
            closeOnPressEscape: false,
            distinguishCancelAndClose: false,
            showClose: false
          });
          setTimeout(() => {
            this.examSubmit();
          }, 2000);

          return;
        }
        this.lastTime--;
      }, 1000);
    },
    //提交拍照记录
    postStudyRecord(status = 0, imgUrl, compareFaceData) {
      return new Promise(resolve => {
        let data = {
          photo: imgUrl,
          recordId: this.recordId
        };
        if (imgUrl) {
          data.similarity = compareFaceData; // 相似度
        }
        this.$request
          .studyExamPhotoRecord(data)
          .then(res => {
            resolve(true);
          })
          .catch(err => {
            this.$message({
              type: "warning",
              message: err.msg
            });
          });
      });
    },
    //人脸校验
    faceRecognition(url) {
      return new Promise(resolve => {
        this.$request
          .faceCertificationCompareFace({
            urlA: url,
            // imageA: url,
            orderGoodsId: this.orderGoodsId,
            gradeId: this.gradeId
          })
          .then(res => {
            resolve(res.data);
          })
          .catch(err => {
            if (err.toString().indexOf("timeout") != -1) {
              err = {
                msg: "拍照超时，请重新拍照"
              };
            }
            this.$message({
              type: "warning",
              message: err.msg
            });
            setTimeout(() => {
              this.openPhoto();
            }, 1500);
          });
      });
    },
    /**
     * 记录总题数,获取recordId
     */
    examRecord() {
      return new Promise(resolve => {
        let self = this;
        let questionList = 0;
        this.questionList.forEach((item, index) => {
          if (item.type == 1 || item.type == 2 || item.type == 3) {
            questionList++;
          }
        });
        this.$request
          .bankRecord({
            fromPlat: 2, //"来源平台 1小程序 2PC网站 3h5"
            courseId: this.courseId,
            chapterId: this.chapterId || 0,
            moduleId: this.moduleId || 0,
            sectionId: this.sectionId || 0,
            goodsId: this.goodsId,
            examId: this.examId,
            type: this.type,
            gradeId: this.gradeId,
            totalQuestionNum: questionList,
            allQuestionNum: this.questionList.length,
            orderGoodsId: this.orderGoodsId,
            doMode: this.doMode,
            simulateExamId: this.simulateExamId
          })
          .then(res => {
            this.recordId = res.data;
            //  节不需要拍照
            if (self.needPhoto) {
              this.openPhoto();
            } else {
              if (this.lastTime) {
                this.BeginTime(); //需要开启计时器
              }
              resolve();
            }
          })
          .catch(err => {
            if (err.code === 601) {
              this.mustBackPage(err.msg);
            } else {
              this.$message.error(err.msg || "未知错误");
            }
          });
      });
    },

    /**
     * 获取试卷类型：1练习,2考试
     */
    bankExam() {
      return new Promise(resolve => {
        this.$request.bankExam(this.examId).then(res => {
          this.bankType = res.data.doType; //1练习,2考试
          this.allTimes = (res.data.answerTime || 0) * 60;
          this.lastTime = (res.data.answerTime || 0) * 60;
          this.doMode = res.data.simulateStatus ? 3 : 1;
          resolve();
        });
      });
    },
    /**
     * @param {Object}
     * 单选点击确认
     */
    radioSelect(question, questionIndex, optionsId) {
      // if (this.questionList[questionIndex].ques) return;
      if (this.questionList[questionIndex].ques && this.bankType == 1) {
        return;
      }
      this.$set(this.questionList[questionIndex], "ques", optionsId);
    },

    /**
     * @param {Object}
     * 案例单选点击
     */
    radioSelectChild(questionIndex, jsonIndex, optionsId) {
      // if (this.questionList[questionIndex].ques[jsonIndex]) return;
      if (
        this.questionList[questionIndex].ques[jsonIndex] &&
        this.bankType == 1
      ) {
        return;
      }
      this.$set(this.questionList[questionIndex].ques, jsonIndex, optionsId);
    },
    /**
     * 多选点击确认
     */
    checkboxSubmit(question, questionIndex) {
      // if (this.questionList[questionIndex].ques) return;
      if (this.questionList[questionIndex].ques && this.bankType == 1) {
        return;
      }
      let arr = [];
      this.questionList[questionIndex].jsonStr.forEach(item => {
        if (item.checked) {
          arr.push(item.optionsId);
        }
      });
      if (!arr.length) {
        delete this.questionList[questionIndex].ques;
        this.$message({
          type: "warning",
          message: "请选择答案"
        });
        return;
      }
      this.$set(this.questionList[questionIndex], "ques", arr);
    },
    /**
     * @param {Object}
     * 案例多选确认
     */
    checkboxSubmitChild(questionIndex, ansIndex) {
      // if (this.questionList[questionIndex].ques[ansIndex]) return;
      if (
        this.questionList[questionIndex].ques[ansIndex] &&
        this.bankType == 1
      ) {
        return;
      }
      let arr = [];
      this.questionList[questionIndex].jsonStr[ansIndex].optionsList.forEach(
        item => {
          if (item.checked) {
            arr.push(item.optionsId);
          }
        }
      );

      if (!arr.length) {
        delete this.questionList[questionIndex].ques[ansIndex];
        this.$message({
          type: "warning",
          message: "请选择答案"
        });
        return;
      }

      this.$set(this.questionList[questionIndex].ques, ansIndex, arr);
    },
    /**
     * 判断点击确认
     */
    judgeSelect(question, questionIndex, index) {
      // if (question.ques) return;
      if (question.ques && this.bankType == 1) {
        return;
      }
      this.$set(
        this.questionList[questionIndex],
        "ques",
        index == 0 ? "1" : "0"
      );
    },
    judgeSelectChild(questionIndex, jsonIndex, index) {
      // console.log(this.questionList[questionIndex].ques[jsonIndex]);
      // if (this.questionList[questionIndex].ques[jsonIndex]) return;
      if (
        this.questionList[questionIndex].ques[jsonIndex] &&
        this.bankType == 1
      ) {
        return;
      }
      this.$set(
        this.questionList[questionIndex].ques,
        jsonIndex,
        index == 0 ? "1" : "0"
      );
    },

    /**
     * 上传图片
     */
    uploadImg(e, question, questionIndex) {
      var file = e.target.files[0];
      if (file.size > 2 * 1024 * 1024) {
        this.$message.warn("图片不得大于2000kb");
        return;
      }
      var type = e.target.value
        .toLowerCase()
        .split(".")
        .splice(-1);
      if (
        type[0] != "jpg" &&
        type[0] != "png" &&
        type[0] != "jpeg" &&
        type[0] != "gif"
      ) {
        this.$message.error("上传格式需为：.jpg/.png/.jpeg/gif");
        e.target.value = "";
        return;
      }

      this.$upload.upload(file, 0).then(res => {
        question.ansText.imageList.push(res);
      });
    },

    /**
     * 案例上传图片
     */
    uploadImgChild(e, questionIndex, jsonIndex) {
      var file = e.target.files[0];
      if (file.size > 2 * 1024 * 1024) {
        this.$message.warn("图片不得大于2000kb");
        return;
      }
      var type = e.target.value
        .toLowerCase()
        .split(".")
        .splice(-1);
      if (
        type[0] != "jpg" &&
        type[0] != "png" &&
        type[0] != "jpeg" &&
        type[0] != "gif"
      ) {
        this.$message.error("上传格式需为：.jpg/.png/.jpeg/gif");
        e.target.value = "";
        return;
      }

      this.$upload.upload(file, 0).then(res => {
        this.questionList[questionIndex].jsonStr[
          jsonIndex
        ].ansText.imageList.push(res);
      });
    },
    isOver(item, index) {
      if (this.questionList[index].ques) {
        if (item.type == 4) {
          //案例题
          let isOver = item.jsonStr.every((jsonItem, indexs) => {
            if (
              jsonItem.type == 1 ||
              jsonItem.type == 2 ||
              jsonItem.type == 3
            ) {
              if (item.ques[indexs]) {
                return true;
              } else {
                return false;
              }
            } else if (jsonItem.type == 5) {
              if (
                item.ques[indexs] &&
                (item.ques[indexs].text || item.ques[indexs].imageList.length)
              ) {
                return true;
              } else {
                return false;
              }
            }
          });

          if (isOver) {
            return true;
          } else {
            return false;
          }
        } else if (item.type == 5) {
          //简答题
          //每一项都相等
          if (item.ques && (item.ques.imageList.length || item.ques.text)) {
            return true;
          }
          //判断
        } else {
          return false;
        }
      } else {
        return false;
      }
    },
    ansSubmit(question, questionIndex) {
      if (!question.ansText.text && !question.ansText.imageList.length) {
        this.$message({
          type: "warning",
          message: "请输入内容或上传图片"
        });
        return;
      }

      question.ques.imageList = question.ansText.imageList;
      question.ques.text = question.ansText.text;
    },
    ansSubmitChild(question, questionIndex, jsonIndex) {
      if (
        !this.questionList[questionIndex].jsonStr[jsonIndex].ansText.text &&
        !this.questionList[questionIndex].jsonStr[jsonIndex].ansText.imageList
          .length
      ) {
        this.$message({
          type: "warning",
          message: "请输入内容或上传图片"
        });
        return;
      }

      this.$set(this.questionList[questionIndex].ques, jsonIndex, {
        imageList:
          this.questionList[questionIndex].jsonStr[jsonIndex].ansText
            .imageList || [],
        text:
          this.questionList[questionIndex].jsonStr[jsonIndex].ansText.text || ""
      });
    },

    changeIndex(index) {
      this.current = index;
      this.getCollectInfo(this.current);
    },
    nextQuestion() {
      if (this.current >= this.questionList.length - 1) {
        this.$message({
          type: "warning",
          message: "已经是最后一题了！"
        });
        return;
      }
      this.current++;
      this.getCollectInfo(this.current);
    },
    prevQuestion() {
      if (this.current == 0) {
        this.$message({
          type: "warning",
          message: "已经是第一题了！"
        });
        return;
      } else {
        this.current--;
        this.getCollectInfo(this.current);
      }
    },
    isCheck(item, index) {
      if (this.questionList[index].ques) {
        return true;
      }
    },
    isRight(item, index) {
      //单选
      if (this.questionList[index].ques) {
        if (item.type == 1) {
          return this.questionList[index].ques == this.questionList[index].ans;
          //多选
        } else if (item.type == 2) {
          //每一项都相等
          return this.questionList[index].ans.every((item, i) => {
            return item == this.questionList[index].ques[i];
          });
          //判断
        } else if (item.type == 3) {
          return this.questionList[index].ques == this.questionList[index].ans;
        } else {
          return false;
        }
      } else {
        return false;
      }
    },

    isWrong(item, index) {
      if (this.questionList[index].ques) {
        //单选
        if (item.type == 1) {
          return this.questionList[index].ques != this.questionList[index].ans;
          //多选
        } else if (item.type == 2) {
          //每一项都相等
          return this.questionList[index].ques.some((item, i) => {
            return this.questionList[index].ans.indexOf(item) == -1;
          });
          //判断
        } else if (item.type == 3) {
          return this.questionList[index].ques != this.questionList[index].ans;
        } else {
          return false;
        }
      } else {
        return false;
      }
    },
    isPart(item, index) {
      if (this.questionList[index].ques) {
        if (item.type == 2) {
          let isWrong = this.questionList[index].ques.some((item, i) => {
            return this.questionList[index].ans.indexOf(item) == -1;
          });

          let isRight = this.questionList[index].ans.every((item, i) => {
            return item == this.questionList[index].ques[i];
          });

          if (!isRight && !isWrong) {
            return true;
          }
        }
      } else {
        return false;
      }
    },
    right(bankIndex, ansIndex, option) {
      if (
        this.questionList[bankIndex].ques[ansIndex] &&
        this.questionList[bankIndex].ans[ansIndex]
      ) {
        if (
          this.questionList[bankIndex].ques[ansIndex].indexOf(
            option.optionsId
          ) != -1 ||
          this.questionList[bankIndex].ans[ansIndex].indexOf(
            option.optionsId
          ) != -1
        ) {
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    },

    wrong(bankIndex, ansIndex, option) {
      if (
        this.questionList[bankIndex].ques[ansIndex] &&
        this.questionList[bankIndex].ans[ansIndex]
      ) {
        if (
          this.questionList[bankIndex].ques[ansIndex].indexOf(
            option.optionsId
          ) != -1 &&
          this.questionList[bankIndex].ans[ansIndex].indexOf(
            option.optionsId
          ) == -1
        ) {
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    },

    /**
     * 获取已经回答的题目数
     * hasSpecail (是否包含简答和案例)
     */
    questionOverNum(hasSpecail) {
      let count = 0;
      this.questionList.forEach(item => {
        if (item.type == 1 || item.type == 2 || item.type == 3) {
          if (item.ques) {
            count++;
          }
        } else if (item.type == 4) {
          //案例题
          if (hasSpecail) {
            let isOver = item.jsonStr.every((jsonItem, index) => {
              if (
                jsonItem.type == 1 ||
                jsonItem.type == 2 ||
                jsonItem.type == 3
              ) {
                if (item.ques[index]) {
                  return true;
                } else {
                  return false;
                }
              } else if (jsonItem.type == 5) {
                if (
                  item.ques[index] &&
                  (item.ques[index].text || item.ques[index].imageList.length)
                ) {
                  return true;
                } else {
                  return false;
                }
              }
            });

            if (isOver) {
              count++;
            }
          }
        } else if (item.type == 5) {
          //简答题
          if (hasSpecail) {
            if (item.ques && (item.ques.text || item.ques.imageList.length)) {
              count++;
            }
          }
        }
      });

      return count;
    },

    collect(state, index) {
      if (!state) {
        this.$request
          .collectQuestion({
            examId: this.examId,
            questionId: this.questionList[index].questionId,
            goodsId: this.goodsId || "",
            orderGoodsId: this.orderGoodsId,
            doMode: this.doMode
          })
          .then(res => {
            this.$set(this.collectList, index, true);
            this.$message.success("收藏成功");
            this.getCollectInfo(index);
          });
      } else {
        this.$request
          .deleteCollectQuestion(this.collectList[index].collectQuestionId)
          .then(res => {
            this.$set(this.collectList, index, false);
            this.$message.success("取消收藏成功");
          });
      }
      return;
    },
    submit() {
      let ansCount = this.questionOverNum(true); //已答题数
      this.lastCount = this.questionList.length - ansCount; //统计未答完的题数
      //没有答完
      if (this.lastCount !== 0) {
        this.$confirm(
          `您还有${this.lastCount}道题未作答， 现在继续作答，还是下次继续？`,
          "提示",
          {
            confirmButtonText: "立即交卷",
            cancelButtonText: "继续做题",
            closeOnClickModal: false,
            closeOnPressEscape: false,
            distinguishCancelAndClose: false,
            showClose: false
          }
        )
          .then(_ => {
            this.examSubmit();
          })
          .catch(_ => {});
        return;
      }

      if (this.bankType == 2) {
        if (this.lastTime > 0) {
          let lastTime = this.countdown(this.lastTime);
          this.$confirm(`时间还剩余${lastTime},确定交卷吗?`, "提示", {
            confirmButtonText: "交卷",
            cancelButtonText: "继续答题",
            closeOnClickModal: false,
            closeOnPressEscape: false,
            distinguishCancelAndClose: false,
            showClose: false
          })
            .then(_ => {
              this.examSubmit();
            })
            .catch(_ => {});
          return;
        }
      }

      this.examSubmit();
    },
    /**
     * @param {Object} second倒计时过滤器
     */
    countdown(second) {
      if (second) {
        let h = parseInt((second / 60 / 60) % 24); //   计算小时
        let m = parseInt((second / 60) % 60); //   计算分数
        let s = parseInt(second % 60); //   计算当前秒数

        if (h < 10) h = "0" + h;
        if (m < 10) m = "0" + m;
        if (s < 10) s = "0" + s;

        return h + ":" + m + ":" + s;
      } else {
        return "";
      }
    },
    examSubmit() {
      let form = this.calculateScore(this.questionList);
      clearInterval(this.timer);
      //交卷 /bank/record/edit
      this.$request
        .bankRecordEdit({
          moduleId: this.moduleId,
          chapterId: this.chapterId,
          sectionId: this.sectionId,
          gradeId: this.gradeId,
          courseId: this.courseId,
          orderGoodsId: this.orderGoodsId,
          type: this.type, //题卷类型 1章卷 2节卷 3模块卷
          examId: this.examId,
          goodsId: this.goodsId,
          recordId: this.recordId,
          status: 1,
          examTime: parseInt(this.allTimes),
          doTime: parseInt(this.allTimes) - parseInt(this.lastTime),
          ...form
        })
        .then(res => {
          this.mustBack = true;
          clearInterval(this.timer);
          this.$message({
            type: "success",
            message: "交卷成功"
          });

          setTimeout(() => {
            let result = {
              rightQuestionNum: form.rightQuestionNum,
              doWrongQuestionNum: form.questionIds.length,
              score: form.performance,
              totalScore: form.totalScore,
              reportStatus: form.reportStatus,
              lessQuestionNum: form.lessQuestionNum
            };

            this.setExamResult(result);
            this.$router.replace({
              path: "/course-report",
              query: {
                nextStatus: this.$route.query.nextStatus,
                // orderGoodsId: this.orderGoodsId,
                // chapterId: this.chapterId,
                // moduleId: this.moduleId,
                examId: this.examId,
                recordId: this.recordId,
                type: this.type //type:1章卷，2节卷，3模块卷
              }
            });
          }, 1000);
        })
        .catch(err => {
          if (err.code === 559) {
            this.$message.error(err.msg);
            console.log("拍照不够触发");
            this.openPhotoStatus = 1;
            this.needPhoto = true;
            this.openPhoto();
          } else {
            this.$message.error(err.msg);
          }
          console.log(err, "err");
        });

      //错题集id提交（客观题）/exam/wwrong/record
      this.$request
        .examWrongRecord({
          moduleId: this.moduleId || 0,
          chapterId: this.chapterId || 0,
          sectionId: this.sectionId || 0,
          gradeId: this.gradeId,
          courseId: this.courseId,
          orderGoodsId: this.orderGoodsId,
          examId: this.examId,
          goodsId: this.goodsId,
          questionIds: form.questionIds,
          recordId: this.recordId,
          type: 2, // 视频课程的传2
          doMode: this.doMode,
          courseType: 2
        })
        .then(res => {})
        .catch(err => {});
    },
    getExamLearnStatus(section) {
      return new Promise(resolve => {
        this.$request
          .getExamLearnStatus({
            goodsId: this.goodsId,
            gradeId: this.gradeId,
            chapterId: this.chapterId,
            courseId: this.courseId,
            moduleId: this.moduleId,
            type: this.type,
            examId: this.examId,
            orderGoodsId: this.orderGoodsId
          })
          .then(res => {
            if (res.data) {
              //之前是否拍照过,拍过则不需要再拍
              this.needPhoto = false;
            }
            resolve();
          });
      });
    },
    /**
     * 返回统计回答正确题数
     */
    examRecordEdit() {
      if (this.needPhoto) {
        this.$message.error("未拍照");
        return;
      }
      clearInterval(this.postTimer);
      clearInterval(this.timer);
      let form = this.calculateScore(this.questionList);
      this.$request
        .bankRecordEdit({
          examId: this.examId,
          goodsId: this.goodsId,
          gradeId: this.gradeId,
          recordId: this.recordId,
          orderGoodsId: this.orderGoodsId,
          courseId: this.courseId,
          type: this.type,
          moduleId: this.moduleId,
          chapterId: this.chapterId,
          sectionId: this.sectionId,
          status: 0,
          ...form
        })
        .then(res => {});
    }
  },
  computed: {
    param() {
      return {
        examId: this.examId,
        questionId: this.questionList[this.current]
          ? this.questionList[this.current].questionId
          : undefined,
        goodsId: this.goodsId,
        orderGoodsId: this.orderGoodsId,
        collectQuestionId: this.collectList[this.current]
          ? this.collectList[this.current].collectQuestionId
          : undefined
      };
    }
  },
  beforeRouteLeave(to, from, next) {
    if (
      this.mustBack ||
      this.$refs.takePicture.photoBadStatus ||
      (to.path == "/home" && !this.$store.state.userInfo)
    ) {
      next();
    } else {
      next(false);
      //离开
      let ansCount = this.questionOverNum(true); //已答题数
      this.lastCount = this.questionList.length - ansCount; //统计未答完的题数
      if (this.bankType == 1) {
        //所有题目答完
        if (this.lastCount == 0) {
          // this.testOver = true;
          this.$nextTick(() => {
            this.$confirm("您还未交卷,确定结束做题吗？", "温馨提示", {
              confirmButtonText: "结束做题",
              cancelButtonText: "下次继续",
              type: "warning"
            })
              .then(() => {
                this.examSubmit();
              })
              .catch(() => {
                this.examRecordEdit();
                next();
              });
          });
          //未答完
        } else {
          this.$nextTick(() => {
            this.$confirm(
              `您还有${this.lastCount}道题未作答， 现在继续作答，还是下次继续？`,
              "温馨提示",
              {
                confirmButtonText: "继续作答",
                cancelButtonText: "下次继续",
                type: "warning"
              }
            )
              .then(() => {
                // confirmButton回调
              })
              .catch(() => {
                this.examRecordEdit();
                next();
              });
          });
          // this.isLastCount = true;
        }
      } else if (this.bankType == 2) {
        //所有题目答完
        if (this.lastCount == 0) {
          this.$nextTick(() => {
            this.$confirm(`您已完成所有题目，快去交卷吧！`, "温馨提示", {
              confirmButtonText: "立即交卷",
              cancelButtonText: "暂不交卷",
              type: "warning"
            })
              .then(() => {
                // confirmButton回调
                this.examSubmit();
              })
              .catch(() => {});
          });
          //未答完
        } else {
          this.$nextTick(() => {
            this.$confirm(
              `您当前正在测试，还剩${this.lastCount}道题未完成，离开视为交卷`,
              "温馨提示",
              {
                confirmButtonText: "继续离开",
                cancelButtonText: "暂不离开",
                type: "warning"
              }
            )
              .then(() => {
                this.examSubmit();
              })
              .catch(() => {});
          });
        }
      }
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.course-exam {
  .section {
    overflow: hidden;

    &__header {
      height: 20px;
      margin-top: 20px;
    }

    &__body {
      .explain-record {

        &__body {
          border: 1px solid #eee;

          .left-box {
            position: relative;
            float: left;
            width: 970px;
            min-height: 630px;
            border: 1px solid #eee;

            &__body {
              border-bottom: 1px solid #eee;

              .question {
                padding: 12px 0 0 12px;
                display: flex;
                flex-direction: column;
                height: 100%;

                &__title {
                  padding-left: 12px;
                  font-size: 16px;
                  font-family: Microsoft YaHei;
                  font-weight: bold;
                  color: #333333;
                  line-height: 24px;
                }

                &__desc {
                  padding-left: 12px;
                  margin-top: 20px;
                  font-size: 16px;
                  font-family: Microsoft YaHei;
                  font-weight: 400;
                  color: #666666;
                  line-height: 24px;
                  text-align: justify;

                  /deep/ img {
                    max-width: 100% !important;
                  }
                }

                &__content {
                  /deep/ .el-tabs__item {
                    padding: 0 20px !important;
                    height: 40px;
                    line-height: 40px;
                  }

                  .question__content {
                    height: auto;
                    overflow: auto;
                  }

                  .question-list {
                    padding: 24px 0 24px 24px;
                    border-bottom: 1px solid #eee;

                    .checkbox,
                    .radio {
                      cursor: pointer;
                      margin-right: 24px;
                      padding: 0 24px;
                      display: flex;
                      align-items: center;
                      margin-top: 2px;
                      min-height: 40px;
                      padding-top: 10px;
                      padding-bottom: 10px;
                      background: #f5f9ff;
                      border-radius: 8px;
                      box-sizing: border-box;

                      &.right {
                        background: #37c65b;
                      }

                      &.wrong {
                        background: #ff3a30;
                      }

                      &.user_choose {
                        background: #007aff;
                      }
                    }

                    &.textarea {
                      margin-right: 12px;

                      .upload {
                        margin-top: 10px;

                        &__imgs {
                          margin-right: 10px;
                          width: 80px;
                          height: 80px;
                          background: #ffffff;
                          border: 1px solid #eeeeee;
                          border-radius: 4px;
                          position: relative;
                          display: flex;
                          float: left;
                          align-items: center;
                          justify-content: center;

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

                        &__btn {
                          margin-right: 10px;
                          width: 80px;
                          height: 80px;
                          background: #ffffff;
                          border: 1px solid #eeeeee;
                          border-radius: 4px;
                          position: relative;
                          display: flex;
                          float: left;
                          align-items: center;
                          justify-content: center;
                          flex-direction: column;

                          .icon {
                            font-size: 20px;
                            color: #3f8dfd;
                          }

                          p {
                            font-size: 12px;
                            font-family: Microsoft YaHei;
                            font-weight: 400;
                            color: #999999;
                            line-height: 24px;
                          }

                          input {
                            position: absolute;
                            left: 0;
                            top: 0;
                            display: block;
                            width: 100%;
                            height: 100%;
                            opacity: 0;
                          }
                        }
                      }
                    }

                    /deep/
                      .el-checkbox__input.is-checked
                      + .el-checkbox__label {
                      color: #000 !important;
                    }

                    /deep/
                      .el-checkbox__input.is-disabled
                      + span.el-checkbox__label {
                      color: #000;
                    }

                    /deep/ .el-checkbox {
                      white-space: normal;
                    }
                  }

                  .answer-list {
                    height: 40px;
                    // border-top: 1px solid #eee;
                    border-bottom: 1px solid #eee;
                    // margin-top: 24px;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    padding: 0 24px;

                    &__left {
                      font-size: 16px;
                      font-family: Microsoft YaHei;
                      font-weight: 400;
                      color: #333333;
                      line-height: 24px;
                    }

                    &__right {
                      font-size: 16px;
                      font-family: Microsoft YaHei;
                      font-weight: 400;
                      color: #333333;
                      line-height: 24px;
                    }
                  }

                  .explain-list {
                    padding: 12px 24px;

                    &__header {
                      font-size: 16px;
                      font-family: Microsoft YaHei;
                      font-weight: bold;
                      color: #666666;
                      line-height: 24px;
                    }

                    &__body {
                      margin-top: 12px;
                      font-size: 16px;
                      font-family: Microsoft YaHei;
                      font-weight: 400;
                      color: #666666;
                      line-height: 24px;
                    }

                    .upload {
                      margin-top: 10px;

                      &__imgs {
                        margin-right: 10px;
                        width: 80px;
                        height: 80px;
                        background: #ffffff;
                        border: 1px solid #eeeeee;
                        border-radius: 4px;
                        position: relative;
                        display: flex;
                        float: left;
                        align-items: center;
                        justify-content: center;

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

                  .question {
                    padding-bottom: 58px;
                  }
                }

                &__btns {
                  position: relative;
                  height: 32px;

                  .submit {
                    cursor: pointer;
                    margin: 0 auto;
                    width: 140px;
                    height: 32px;
                    background: #3f8dfd;
                    box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.2);
                    border-radius: 16px;
                    text-align: center;
                    line-height: 32px;
                    color: #fff;
                    font-size: 16px;
                  }

                  .collect {
                    cursor: pointer;
                    position: absolute;
                    right: 0;
                    top: 5px;
                    font-size: 12px;
                    font-family: Microsoft YaHei;
                    font-weight: 400;
                    color: #3f8dfd;
                    line-height: 24px;
                  }
                }
              }
            }
          }

          .right-box {
            float: right;
            width: 299px;
            border-right: 1px solid #eee;

            &__header {
              height: 40px;
              line-height: 40px;
              border-bottom: 1px solid #eeeeee;
              padding: 0 10px;

              .back-btn {
                display: block;
                margin: 0 auto;
                width: 160px;
              }

              .title {
                float: left;
                font-size: 16px;
                font-family: Microsoft YaHei;
                font-weight: bold;
                color: #333333;
                float: left;
              }

              .time {
                font-size: 16px;
                font-family: Microsoft YaHei;
                font-weight: 400;
                color: #666666;
                float: right;
              }
            }

            &__body {
              border-bottom: 1px solid #eee;
              height: 510px;

              .card {
                &__note {
                  display: flex;
                  height: 64px;
                  align-items: center;
                  border-bottom: 1px solid #eee;
                  flex-wrap: wrap;

                  .item {
                    display: flex;
                    align-items: center;
                    margin-left: 10px;
                    width: 84px;
                    font-size: 12px;

                    .box {
                      margin-right: 5px;
                      width: 16px;
                      height: 16px;
                      border-radius: 4px;

                      &.white {
                        background: #ffffff;
                        border: 1px solid #eeeeee;
                      }

                      &.green {
                        background: #37c65b;
                      }

                      &.red {
                        background: #ff3a30;
                      }

                      &.yellow {
                        background: #ffc53d;
                      }

                      &.blue,
                      &.check_ans {
                        background: #3f8dfd;
                      }
                    }
                  }
                }

                &__content {
                  height: 440px;
                  overflow-y: scroll;

                  &::-webkit-scrollbar {
                    width: 6px;
                  }

                  &::-webkit-scrollbar-track {
                    background-color: #fff;
                    -webkit-border-radius: 2em;
                    -moz-border-radius: 2em;
                    border-radius: 2em;
                  }

                  &::-webkit-scrollbar-thumb {
                    background-color: #eeeeee;
                    -webkit-border-radius: 2em;
                    -moz-border-radius: 2em;
                    border-radius: 2em;
                  }

                  .list {
                    display: flex;
                    flex-wrap: wrap;

                    .item {
                      width: 40px;
                      height: 40px;
                      border-radius: 10px;
                      text-align: center;
                      line-height: 40px;
                      margin-left: 16px;
                      margin-top: 16px;
                      cursor: pointer;

                      &.white {
                        line-height: 38px;
                        color: #333333;
                        background: #ffffff;
                        border: 1px solid #eeeeee;
                      }

                      &.green {
                        color: #fff;
                        background: #37c65b;
                      }

                      &.red {
                        color: #fff;
                        background: #ff3a30;
                      }

                      &.blue,
                      &.check_ans {
                        color: #fff;
                        background: #3f8dfd;
                      }

                      &.yellow {
                        background: #ffc53d;
                      }

                      &.disabled {
                        cursor: not-allowed;
                        line-height: 38px;
                        color: #eeeeee;
                        background: #ffffff;
                        border: 1px solid #eeeeee;
                      }
                    }
                  }
                }
              }
            }

            &__footer {
              border-bottom: 1px solid #eee;
              height: 40px;
              display: flex;
              align-items: center;
              justify-content: center;

              .submit {
                cursor: pointer;
                width: 140px;
                height: 32px;
                background: #3f8dfd;
                box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.2);
                border-radius: 16px;
                line-height: 32px;
                text-align: center;
                color: #fff;
                font-size: 16px;
              }
            }
          }
        }
      }
    }
  }

  .take-photo {
    /deep/ .el-dialog__header {
      display: none;
    }

    /deep/ .el-dialog__body {
      padding: 0;
      overflow: unset;
    }

    &__close {
      cursor: pointer;
      position: absolute;
      right: 0;
      top: -28px;
      width: 24px;
      height: 24px;
      line-height: 24px;
      text-align: center;
      color: #eee;
      border: 1px solid #eee;
      border-radius: 50%;
    }

    &__header {
      height: 40px;
      border-bottom: 1px solid #eee;
      line-height: 40px;
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #333333;
      padding-left: 24px;
    }

    &__body {
      // height: 400px;
      padding: 40px 24px;

      .left-box {
        width: 336px;
        float: left;

        .title {
          font-size: 16px;
          font-family: Microsoft YaHei;
          font-weight: bold;
          color: #ff3b30;
          line-height: 24px;
        }

        .content {
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #333333;
          line-height: 28px;
          margin-top: 32px;
        }
      }

      .right-box {
        float: right;
        width: 400px;
        height: 400px;
        position: relative;
        overflow: hidden;

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

        .mask {
          width: 55%;
          height: 200px;
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          margin: 30px auto 0;
          box-shadow: 0 0 0 2000px rgba(0, 0, 0, 0.4);
        }
      }
    }

    &__footer {
      height: 90px;
      border-top: 1px solid #eee;
      text-align: center;

      .take {
        display: inline-block;
        width: 200px;
        height: 40px;
        padding: 0;
        border-radius: 20px;
        text-align: center;
        line-height: 40px;
        margin: 24px auto;
      }
    }
  }
}
</style>
