<script>
import { getKnowledgeTest } from "@/api/user";

export default {
  name: "examSelect",
  data() {
    return {
      examTab: this.$t('examination.examTab'),
      examIndex: 0,
      // 获取考试数据参数
      form: {
        PageIndex: 1,
        PageSize: 8,
        state: "",
        userId: "",
      },
      // 考试场次
      testList: [],
      total: 0, //总数
    };
  },
  methods: {
    examClick(item, index) {
      // 状态切换页码初始化
      this.form.PageIndex = 1;
      this.examIndex = index;
      this.form.state = item.state;
      //   切换状态重新发送请求
      this.getTestList();
    },
    getTestList() {
      getKnowledgeTest(this.form).then((res) => {
        this.testList = res.data.data;
        this.total = parseInt(res.data.message);
      });
    },
    // 进入考试
    enterExam(item) {
      if (item.batch_state.trim() === "进行中") {
        this.$router.push(
          `/examination?id=${item.batch_id}&totalScore=${item.total_score}&minute=${item.ks_minute}&passScore=${item.pass_score}`
        );
        let data = {
          user_id: this.form.userId,
          batch_id: item.batch_id,
          list_item_answer: [],
        };
        let testMsg = {
          testTitle: item.subject_name, //考试名称
          endTime: item.end_time, //考试结束时间
        };
        // 存储考试数据
        sessionStorage.setItem("testMsg", JSON.stringify(testMsg));
        sessionStorage.setItem("params", JSON.stringify(data));
      }
    },
    currentChange(current) {
      this.form.PageIndex = current;
      this.getTestList();
    },
    getCountdown(time) {
      let endTime = new Date(time).getTime();
      let startTime = new Date().getTime();
      let remainder = endTime - startTime;
      let d = parseInt(remainder / 1000 / 60 / 60 / 24); //算出天数
      let h = parseInt((remainder / 1000 / 60 / 60) % 24); //算出小时数
      let m = parseInt((remainder / 1000 / 60) % 60); //算出分钟数
      let s = parseInt((remainder / 1000) % 60); //算出秒数
      h = d * 24 + h;
      if (h < 10) {
        h = "0" + h;
      }
      if (m < 10) {
        m = "0" + m;
      }
      if (s < 10) {
        s = "0" + s;
      }

      return h + "小时" + m + "分" + s + "秒后开始";
      // return h+':'+m+':'+s
    },
  },
  mounted() {
    // 用户id赋值
    this.form.userId = window.location.href.split("?")[1];
    this.getTestList();
  },
  computed: {},
};
</script>

<template>
  <div class="test-box">
    <div class="test-box-title">考试场次选择</div>
    <ul class="test-box-status">
      <li
        v-for="(item, index) in examTab"
        :class="examIndex === index ? 'select' : ''"
        @click="examClick(item, index)"
      >
        {{ item.name }}
      </li>
    </ul>
    <div class="test-box-site">
      <div v-for="item in testList" class="test-box-site-item">
        <div class="test-box-site-item-name">
          {{ item.subject_name }}
        </div>
        <div class="test-box-site-item-start">
          <span>开始时间：{{ item.start_time }}</span>
          <span class="end" v-if="item.batch_state.trim() === '已结束'"></span>
          <span class="started" v-else-if="item.batch_state.trim() === '进行中'"></span>
          <span class="not-started" v-else></span>
        </div>
        <div class="test-box-site-item-end">
          <span>结束时间：{{ item.end_time }}</span>
        </div>
        <div class="test-box-site-item-time">
          <span>考试时长：{{ item.ks_minute }}分钟</span>
        </div>
        <div class="test-box-site-item-score">
          <span>满分：{{ item.total_score }}分</span>
        </div>
        <div class="test-box-site-item-btn">
          <div class="time" v-if="item.batch_state.trim() === '未开始'">
            {{ getCountdown(item.start_time) }}
          </div>
          <span
            v-if="item.state === 0"
            :class="item.batch_state.trim() === '进行中' ? 'starting' : ''"
            @click="enterExam(item)"
          >
            进入考场
          </span>
          <span v-else> 已参加 </span>
        </div>
      </div>
    </div>
    <div class="test-no-data" v-if="!testList.length"></div>
    <div class="test-box-pagination">
      <span>共{{ total }}条</span>
      <el-pagination
        @current-change="currentChange"
        :current-page="form.PageIndex"
        layout="prev, pager, next"
        :page-size="8"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<style lang="less" scoped>
.test-no-data {
  background-image: url("../../assets/image/examSelect/not_data.png");
  width: 228px;
  height: 244px;
  background-size: 100% 100%;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}
::v-deep .el-pager li {
  background-image: none;
  background-color: transparent;
  font-size: 14px;
  font-weight: normal;
  width: 34px;
  height: 34px;
  line-height: 34px;
}
::v-deep .el-pagination .btn-next,
::v-deep .el-pagination .btn-prev {
  background-color: transparent;
  width: 34px;
  color: #ffffff;
  height: 34px;
  line-height: 34px;
}
::v-deep .el-pagination button:disabled {
  color: rgba(255, 255, 255, 0.3);
  background-color: transparent;
}
::v-deep .el-pager li:hover {
  color: #ffffff;
}
::v-deep .el-pager li.active {
  background-color: rgba(255, 255, 255, 0.12);
  color: #ffffff;
}
.test-box {
  width: 1773px;
  height: 858px;
  background-image: url("../../assets/image/examSelect/test-box.png");
  background-size: 100% 100%;
  position: absolute;
  padding: 0 68px;
  box-sizing: border-box;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  margin: auto;
  //
  &-pagination {
    position: absolute;
    display: flex;
    bottom: 40px;
    left: 50%;
    transform: translateX(-50%);
    align-items: center;
    span {
      color: #ffffff;
      font-size: 14px;
    }
  }
  &-title {
    position: absolute;
    top: 14px;
    left: 50%;
    font-size: 24px;
    margin-left: -72px;

    color: rgba(255, 255, 255, 1);
  }

  //  状态的tab栏

  &-status {
    margin-top: 42px;
    margin-bottom: 26px;
    display: flex;
    list-style: none;

    .select {
      background-image: url("../../assets/image/examSelect/select-status.png");
      color: rgba(255, 255, 255, 1);
    }

    li {
      background-image: url("../../assets/image/examSelect/default-status.png");
      color: rgba(255, 255, 255, 0.6);
      width: 136px;
      height: 40px;
      margin-right: 14px;
      text-align: center;
      line-height: 40px;
    }
  }

  //  考试场次选择
  &-site {
    height: 552px;
    &-item {
      float: left;
      width: 372px;
      margin-right: 49px;
      margin-bottom: 49px;
      height: 232px;
      box-sizing: border-box;
      padding: 0 18px;
      background-image: url("../../assets/image/examSelect/exam-plate.png");
      background-size: 100% 100%;
      //  理论考试名字
      &-name {
        font-size: 18px;
        margin-top: 8px !important;
      }

      div {
        color: rgba(255, 255, 255, 1);
        font-size: 16px;
        margin-top: 16px;
      }

      //开始时间
      &-start {
        display: flex;
        justify-content: space-between;
        margin-top: 23px !important;
        span:nth-child(2) {
          text-align: center;
          line-height: 22px;
          width: 62px;
          height: 22px;
          background-size: 100% 100%;
        }
        .end {
          background-image: url("../../assets/image/examSelect/end.png");
        }
        .not-started {
          background-image: url("../../assets/image/examSelect/not-started.png");
        }
        .started {
          background-image: url("../../assets/image/examSelect/started.png");
        }
      }

      //  进入考场
      &-btn {
        display: flex;
        justify-content: flex-end;
        position: relative;
        margin-top: 5px !important;
        .time {
          color: rgba(101, 140, 246, 1);
          font-size: 14px;
          position: absolute;
          top: -30px;
          right: 0;
          margin: 0;
        }
        span {
          background-image: url("../../assets/image/examSelect/enter-not.png");
          font-size: 14px;
          width: 101px;
          line-height: 28px;
          color: rgba(255, 255, 255, 0.5);
          text-align: center;
          background-size: 100% 100%;
        }
        .starting {
          background-image: url("../../assets/image/examSelect/enter.png");
          color: rgba(255, 255, 255, 1);
        }
      }
    }

    &-item:nth-child(4n) {
      margin-right: 0;
    }
  }
}
</style>
