<template>
  <el-container>
    <el-header height="220">
      <el-row>
        <el-select
          v-model="queryInfo.subjectId"
          clearable
          placeholder="请选择科目"
          style="margin-left: 5px"
          @change="subjectChange"
        >
          <el-option
            v-for="item in allSubject"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          />
        </el-select>
      </el-row>
    </el-header>

    <el-main>
      <el-table
        :data="dataList"
        border
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          label="No"
          type="index"
        />
        <el-table-column
          prop="examName"
          label="测评名称"
          width="150"
        />
        <el-table-column
          label="测评码"
          width="90"
        >
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.needPasscode"
              size="mini"
              type="danger"
            >需要</el-tag>
            <el-tag
              v-else
              size="mini"
              type="success"
            >不需要</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="startTime"
          label="测评时间"
          width="150"
        />
        <el-table-column
          prop="duration"
          label="测评时长(分钟)"
          width="150"
        />
        <el-table-column
          prop="totalScore"
          label="试卷总分"
        />
        <el-table-column
          prop="myScore"
          label="我的分数"
        />
        <el-table-column
          fixed="right"
          label="操作"
          width="320"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.evalStatus === 1"
              size="mini"
              type="primary"
              @click="prepareExam(scope.$index, scope.row)"
            >去测评</el-button>
            <el-button
              v-else-if="scope.row.evalStatus === 2"
              size="mini"
              type="danger"
              @click="markPaper(scope.$index, scope.row)"
            >去批改</el-button>
            <el-button
              v-else-if="scope.row.evalStatus === 3"
              size="mini"
              type="warning"
              @click="waitMark(scope.$index, scope.row)"
            >待批改</el-button>
            <el-button
              v-else
              size="mini"
              type="success"
              @click="viewResult(scope.$index, scope.row)"
            >看结果</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :small="screenWidth <= 768"
        layout="prev, pager, next"
        :page-size="pageSize"
        :current-page="currentPage"
        :total="totalSize"
        @current-change="handleCurrentChange"
        @prev-click="handleCurrentChange"
        @next-click="handleCurrentChange"
      />
    </el-main>

    <el-dialog
      title="测评提示"
      :visible.sync="startExamDialog"
      center
      width="50%"
    >
      <el-alert
        title="点击`开始测评`后将自动进入测评，请诚信测评，测评过程中可能会对用户行为、用户视频进行截图采样，请知悉！"
        type="error"
      />
      <el-card style="margin-top: 25px">
        <span>测评名称：</span>{{ currentSelectedExam.examName }}
        <br>
        <span>测评时长：</span>{{ currentSelectedExam.duration + '分钟' }}
        <br>
        <span>试卷总分：</span>{{ currentSelectedExam.totalScore }}分
        <br>
        <span>开放类型：</span> {{ currentSelectedExam.needPasscode ? '需要密码' : '完全公开' }}
        <br>
      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="startExamDialog = false">返 回</el-button>
        <el-button type="primary" @click="startExam(currentSelectedExam.examId)">开始测评</el-button>
      </span>
    </el-dialog>
  </el-container>
</template>

<script>
import { getSubjectKV, getExamList } from '@/api/exam'

export default {
  name: 'ExamEvalList',
  data() {
    return {
      // 屏幕宽度, 为了控制分页条的大小
      screenWidth: document.body.clientWidth,
      currentPage: 1,
      pageSize: 20,
      totalSize: 0,
      dataList: [],
      // **********************************************************************
      queryInfo: {
        subjectId: null,
        pageNumber: 1,
        pageSize: 10
      },
      allSubject: [],
      // 开始测评的提示框
      startExamDialog: false,
      // 当前选中的测评的信息
      currentSelectedExam: {
        examId: 114511
      }
    }
  },
  created() {
    document.title = '我的试卷'
    this.getData(this.queryInfo)
    this.getSubjects()
    // this.getQuestionTypes()
  },
  methods: {
    handleCurrentChange(pageNumber) {
      this.currentPage = pageNumber
      this.queryInfo.pageNumber = this.currentPage
      this.queryInfo.pageSize = this.pageSize
      this.getData(this.queryInfo)
      // 回到顶部
      scrollTo(0, 0)
    },
    getData(queryInfo) {
      getExamList(queryInfo).then(resp => {
        if (resp.code === 0) {
          this.dataList = resp.data.list
          this.totalSize = resp.data.totalSize
        } else {
          this.$notify({
            title: '提示',
            message: resp.msg,
            type: 'warning',
            duration: 3000
          })
        }
      }).catch(error => {
        this.$notify({
          title: '提示',
          message: error.message,
          type: 'error',
          duration: 3000
        })
      })
    },
    getSubjects() {
      getSubjectKV().then((resp) => {
        if (resp.code === 0) {
          this.allSubject = resp.data
        }
      })
    },
    // 题库变化
    subjectChange(val) {
      this.queryInfo.subjectId = val
      this.queryInfo.pageNumber = this.currentPage
      this.queryInfo.pageSize = this.pageSize
      this.getData(this.queryInfo)
    },
    prepareExam(index, row) {
      if (!row.needPasscode) {
        this.startExamDialog = true
        this.currentSelectedExam = row
        return
      }

      this.$prompt('请提供测评码', 'Tips', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        if (value === 123) {
          this.$message.error('密码错误o(╥﹏╥)o')
          return
        }

        this.startExamDialog = true
        this.currentSelectedExam = row
      }).catch(() => {
      })
    },
    startExam(paperId) {
      const routeUrl = this.$router.resolve({
        path: '/exam/paper/detail',
        query: {
          paperId: paperId,
          viewType: 2,
          userId: ''
        }
      })
      window.open(routeUrl.href, '_blank')
    },
    waitMark(index, row) {
      this.$message.info('试卷待批改')
    },
    markPaper(index, row) {
      const paperId = row.examId
      const routeUrl = this.$router.resolve({
        path: '/exam/paper/detail',
        query: {
          paperId: paperId,
          viewType: 3,
          userId: 0
        }
      })
      window.open(routeUrl.href, '_blank')
    },
    viewResult(index, row) {
      const paperId = row.examId
      const routeUrl = this.$router.resolve({
        path: '/exam/paper/detail',
        query: {
          paperId: paperId,
          viewType: 4,
          userId: ''
        }
      })
      window.open(routeUrl.href, '_blank')
    }
  }
}
</script>

<style>
</style>
