<template>
  <div class="bank-record">
    <div class="bank-record__header">
      <div class="title">做题记录</div>
      <div class="content">
        <el-select
          clearable
          @change="reGetExamRecordList"
          v-model="param.goodsId"
          placeholder="请选择"
        >
          <el-option
            clearable
            v-for="item in list"
            :key="item.goodsId"
            :label="item.goodsName"
            :value="item.goodsId"
          >
          </el-option>
        </el-select>
        <el-select
          @change="reGetExamRecordList"
          v-model="param.paperId"
          placeholder="请选择"
        >
          <el-option
            v-for="item in list1"
            :key="item.paperId"
            :label="item.paperName"
            :value="item.paperId"
          >
          </el-option>
        </el-select>
      </div>
    </div>

    <div class="bank-record__body">
      <div class="bank-item" v-for="(record, index) in recordList" :key="index">
        <div class="bank-item__header">
          <div class="note" v-if="record.paperName">{{ record.paperName }}</div>
          {{ record.examName }}
        </div>
        <div class="bank-item__body">
          <div class="list">
            <div class="exam-item">
              <div class="exam-item__body">
                <div class="text">
                  <span class="title">{{
                    $tools.timestampToTime(record.updateTime, false)
                  }}</span>
                  <span class="desc"
                    >总{{ record.totalQuestionNum }}题/对{{
                      record.rightQuestionNum
                    }}题</span
                  >
                </div>
                <div class="btns-wrap">
                  <div class="btns">
                    <el-button
                      type="primary"
                      class="btn"
                      @click="
                        go('/bank-exam-continue/' + record.goodsId, {
                          recordId: record.recordId,
                          examId: record.examId,
                          chapterId: record.chapterExamId,
                          moduleId: record.moduleExamId,
                          orderGoodsId: record.orderGoodsId,
                        })
                      "
                      v-if="record.status == 0 && record.historyExamJson"
                      >继续答题</el-button
                    >

                    <div class="btn-line">
                      <div
                        class="btn-line__in"
                        @click="doRepeat(record)"
                        v-if="record.status == 1"
                      >
                        <div>重新做题</div>
                      </div>
                      <div
                        class="btn-line__in"
                        @click="
                          go('/bank-exam-explain/' + record.goodsId, {
                            examId: record.examId,
                            chapterId: record.chapterExamId,
                            moduleId: record.moduleExamId,
                            orderGoodsId: record.orderGoodsId,
                          })
                        "
                        v-if="record.status == 1"
                      >
                        <div>解析</div>
                      </div>
                      <div
                        class="btn-line__in"
                        v-if="record.status == 1"
                        @click="
                          go('/bank-report/' + record.goodsId, {
                            chapterId: record.chapterExamId,
                            moduleId: record.moduleExamId,
                            examId: record.examId,
                            recordId: record.recordId,
                            orderGoodsId: record.orderGoodsId,
                          })
                        "
                      >
                        <div>做题报告</div>
                      </div>
                      <div
                        class="btn-line__in"
                        @click="
                          go('/person-center/record-list/' + record.goodsId, {
                            examId: record.examId,
                            chapterId: record.chapterExamId,
                            moduleId: record.moduleExamId,
                            recordId: record.recordId,
                          })
                        "
                      >
                        <div>更多记录</div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="bank-record__footer">
      <div class="pagination">
        <el-pagination
          @current-change="currentChange"
          background
          layout="prev, pager, next"
          :total="total"
          :pager-count="5"
          :page-size="param.pageSize"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "BankRecord",
  data() {
    return {
      list1: [],
      list: [],
      param: {
        pageNum: 1,
        pageSize: 10,
        goodsId: "",
        paperId: "",
      },
      recordList: [],
      total: 0,
    };
  },
  mounted() {
    this.listGoodsUserQuestion();
    this.examaperList();
    this.getExamRecordList();
  },
  methods: {
    reGetExamRecordList() {
      this.param.pageNum = 1;
      this.getExamRecordList();
    },
    examaperList() {
      this.$request.examaperList().then((res) => {
        this.list1 = res.rows;
        this.list1.unshift({
          paperId: "",
          paperName: "全部试卷类型",
        });
      });
    },

    listGoodsUserQuestion() {
      this.$request.listGoodsUserQuestion().then((res) => {
        this.list = res.rows;
        this.list.unshift({
          goodsId: "",
          goodsName: "全部题库记录",
        });
      });
    },
    go(path, query) {
      this.$router.push({
        path,
        query,
      });
    },
    currentChange(e) {
      this.param.pageNum = e;
      this.getExamRecordList();
    },
    getExamRecordList() {
      this.$request.examRecordGroupList(this.param).then((res) => {
        this.recordList = res.rows;
        this.total = res.total;
      });
    },

    async doRepeat(section) {
      let count = await this.examRecordCount(section.examId);
      let answerNum = await this.getExamDetail(section.examId);
      //超过答题次数
      if (answerNum > 0 && count >= answerNum) {
        this.$message({
          type: "warning",
          message: "该试卷只能答题" + answerNum + "次!",
        });
        return;
      }

      this.$confirm(`是否清空答案重做？`, "提示", {
        confirmButtonText: "重做",
        cancelButtonText: "查看上次",
        closeOnClickModal: false,
        closeOnPressEscape: false,
        distinguishCancelAndClose: false,
        showClose: false,
      })
        .then((_) => {
          this.$router.push({
            path: "/bank-exam/" + section.goodsId,
            query: {
              orderGoodsId: section.orderGoodsId,
              examId: section.examId,
              moduleId: section.moduleExamId || 0,
              chapterId: section.chapterExamId || 0,
            },
          });
        })
        .catch((_) => {
          this.$router.push({
            path: "/bank-exam-all-explain/" + section.recordId,
            query: {
              examId: section.examId,
              moduleId: section.moduleExamId || 0,
              chapterId: section.chapterExamId || 0,
              goodsId: section.goodsId,
              orderGoodsId: section.orderGoodsId,
            },
          });
        });
    },

    /**
     * 查询试卷历史做题次数
     */
    examRecordCount(examId) {
      return new Promise((resolve) => {
        this.$request
          .examRecordCount({
            examId: examId,
            goodsId: this.id,
          })
          .then((res) => {
            resolve(res.data);
          });
      });
    },
    /**
     * @param {Object} exam_id
     * 获取试卷可以做的次数
     */
    getExamDetail(exam_id) {
      return new Promise((resolve) => {
        this.$request.getExamDetail(exam_id).then((res) => {
          resolve(res.data.answerNum);
        });
      });
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.bank-record {
  &__header {
    .title {
      height: 56px;
      line-height: 56px;
    }

    .content {
      margin: 10px 0;
    }
    border-bottom: 1px solid #eee;
  }
  &__body {
    .bank-item {
      margin-top: 24px;

      &__header {
        height: 32px;
        line-height: 32px;
        .note {
          display: inline-block;
          margin-right: 15px;
          width: 80px;
          height: 32px;
          line-height: 32px;
          background: #3f8dfd;
          text-align: center;
          color: #fff;
          font-size: 16px;
          border-radius: 8px 0 8px 0;
        }
      }

      &__body {
        .list {
          .exam-item {
            height: 65px;
            background: #fafbfc;
            border-radius: 8px;
            overflow: hidden;
            line-height: 65px;
            border-top: 1px solid #eee;
            position: relative;

            &__body {
              .text {
                .title {
                  margin-left: 24px;
                  font-size: 16px;
                  font-family: Microsoft YaHei;
                  font-weight: 400;
                  color: #333333;
                }

                .desc {
                  margin-left: 24px;
                  color: #666;
                  font-size: 16px;
                }
              }

              .btns-wrap {
                display: table;
                position: absolute;
                right: 0;
                top: 0;
                height: 100%;
                margin-right: 20px;

                .btns {
                  display: table-cell;
                  vertical-align: middle;
                  text-align: center;

                  .btn {
                    cursor: pointer;
                    width: 122px;
                    height: 32px;
                    padding: 0;
                    border-radius: 16px;
                    display: inline-block;
                    text-align: center;
                    line-height: 32px;
                    vertical-align: middle;

                    /deep/ span {
                      font-size: 16px;
                    }
                  }

                  .btn-line {
                    margin: 0 10px;
                    overflow: hidden;
                    vertical-align: middle;
                    display: inline-block;
                    background: #ffffff;
                    border: 1px solid #3f8dfd;
                    border-radius: 30px;
                    font-size: 0;
                    line-height: 0;

                    &__in {
                      cursor: pointer;
                      display: inline-block;
                      padding: 7px 0;
                      color: #3f8dfd;

                      > div {
                        text-align: center;
                        font-size: 16px;
                        line-height: 16px;
                        width: 96px;
                        height: 16px;
                        border-right: 1px solid #3f8dfd;
                      }

                      &:nth-last-of-type(1) {
                        > div {
                          border: 0;
                        }
                      }

                      &:hover {
                        background: #3f8dfd;
                        color: #fff;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  &__footer {
    .pagination {
      padding: 30px 0;
      text-align: center;
    }
  }
}
</style>
