<template>
  <el-container>
    <el-main>
      <commonFormVue :form="form" :getExamCondition="getExamCondition">
        <el-col :span="8">
          <el-form-item label="完成状态">
            <el-select v-model="form.finished" clearable placeholder="全部">
              <el-option value="true" label="已完成"></el-option>
              <el-option value="false" label="未完成"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="采集人">
            <el-input v-model="form.infoCollector" placeholder="采集人">
            </el-input>
          </el-form-item>
        </el-col>
      </commonFormVue>
      <el-row>
        <el-col :span="24">
          <el-button @click="search" size="medium" type="primary"
            >查询</el-button
          >
          <commonExportVue
            v-show="currentPagePrivileges.EXAM_PARTICULARS_EXPORT"
            :form="form"
            :exportUrl="exportUrl"
            :exportFileName="exportFileName"
          ></commonExportVue>
        </el-col>
      </el-row>
      <el-row class="margin-top-10"
        ><el-col :span="24">
          <el-table
            v-loading="tableLoading"
            element-loading-text="数据加载中"
            ref="multipleTable"
            @selection-change="handleSelectionChange"
            :data="tableData"
            border
          >
            <el-table-column sortable label="学习中心" prop="orgName">
            </el-table-column>
            <el-table-column sortable label="姓名" prop="studentName">
            </el-table-column>
            <el-table-column sortable label="身份证号" prop="identityNumber">
            </el-table-column>
            <el-table-column sortable label="学号" prop="studentCode">
            </el-table-column>
            <el-table-column sortable label="课程" prop="courseName">
            </el-table-column>
            <el-table-column sortable label="课程层次" prop="courseLevel">
            </el-table-column>
            <el-table-column sortable label="专业" prop="specialtyName">
            </el-table-column>
            <el-table-column sortable label="已考次数" prop="normalExamTimes">
            </el-table-column>
            <el-table-column sortable label="学生电话" prop="phone">
            </el-table-column>
            <el-table-column sortable label="年级" prop="grade">
            </el-table-column>
            <el-table-column sortable label="采集人" prop="infoCollector">
            </el-table-column>
            <el-table-column sortable label="完成状态">
              <template slot-scope="scope">
                <span
                  v-show="scope.row.finished && scope.row.examType == 'ONLINE'"
                  >已完成</span
                >
                <span
                  v-show="!scope.row.finished && scope.row.examType == 'ONLINE'"
                  >未完成</span
                >
                <span
                  v-show="scope.row.finished && scope.row.examType == 'OFFLINE'"
                  >已抽题</span
                >
                <span
                  v-show="
                    !scope.row.finished && scope.row.examType == 'OFFLINE'
                  "
                  >未抽题</span
                >
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  v-show="scope.row.examType == 'OFFLINE'"
                  type="success"
                  icon="el-icon-success"
                  @click="previewPaper(scope.row.examStudentId);"
                  >查看考题</el-button
                >
                <el-button
                  size="mini"
                  v-show="scope.row.examType == 'OFFLINE'"
                  type="success"
                  icon="el-icon-success"
                  @click="exportPaper(scope.row.examStudentId);"
                  >下载考题</el-button
                >
                <el-button
                  size="mini"
                  v-show="scope.row.examType == 'OFFLINE'"
                  type="success"
                  icon="el-icon-success"
                  @click="openUploadAnswerDialog(scope.row.examStudentId);"
                  >上传作答</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <div class="block">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page.sync="form.pageNo"
              :page-sizes="[10, 30, 50, 100]"
              :page-size="form.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            >
            </el-pagination></div></el-col
      ></el-row>
      <div>
        <el-dialog title="上传作答" :visible.sync="uploadAnswerDialogVisible">
          <el-form>
            <el-form-item label="选择文件"
              ><input
                type="file"
                accept="application/pdf,application/zip"
                id="importFile"
                @change="uploadAnswerChange"
              />
              <div>温馨提示:仅支持pdf和zip文件,文件大小请不要超过30M!</div>
            </el-form-item>
            <div class="dialog-footer">
              <el-button @click="uploadAnswerDialogVisible = false;"
                >取 消</el-button
              >
              <el-button
                :disabled="!offlineAnswerFile"
                type="primary"
                @click="doUploadAnswer"
                >确 定</el-button
              >
            </div>
          </el-form>
        </el-dialog>
      </div>
    </el-main>
  </el-container>
</template>
<script>
import { mapState } from "vuex";
import commonFormVue from "../component/commonForm.vue";
import commonExportVue from "../component/commonExport.vue";
import pagePrivilege from "../mixin/pagePrivilege.js";
export default {
  components: { commonFormVue, commonExportVue },
  mixins: [pagePrivilege],
  data() {
    return {
      uploadAnswerDialogVisible: false,
      total: 0,
      tableLoading: false,
      form: {
        examRecordDataId: null,
        hasStranger: null,
        courseId: null,
        courseLevel: null,
        examId: null,
        examRecordId: null,
        faceSuccessPercentLower: null,
        faceSuccessPercentUpper: null,
        livenessSuccessPercentLower: null,
        livenessSuccessPercentUpper: null,
        identityNumber: null,
        orgId: null,
        studentCode: null,
        studentName: null,
        isWarn: null,
        pageNo: 1,
        pageSize: 10
      },

      getExamCondition: {
        params: {
          name: "",
          examTypes: "ONLINE#OFFLINE"
        },
        filterCondition: ""
      },
      tableData: [],
      exportUrl: "/api/ecs_oe_admin/exam/student/all/list/export",
      exportFileName: "考试进度详情",
      currentOfflineExamRecordDataId: "",
      offlineAnswerFile: "",
      currentPagePrivileges: {
        EXAM_PARTICULARS_EXPORT: false //导出
      }
    };
  },
  computed: {
    ...mapState({ user: state => state.user })
  },
  methods: {
    search() {
      if (!this.form.examId) {
        this.$notify({
          title: "警告",
          message: "请选择考试批次",
          type: "warning",
          duration: 1000
        });
        return false;
      }
      this.tableLoading = true;
      this.$http
        .post("/api/ecs_oe_admin/exam/student/all/list", this.form)
        .then(response => {
          if (response.data) {
            this.tableData = response.data.content;
            this.total = response.data.totalElements;
          } else {
            this.tableData = [];
          }
          this.tableLoading = false;
        });
    },
    selectable(row) {
      return row.isWarn;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    /**
     * pagesize改变时触发
     */
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.search();
    },
    /**
     * 当前页改变时触发
     */
    handleCurrentChange() {
      this.search();
    },
    previewPaper(examStudentId) {
      this.$http
        .get("/api/ecs_oe_admin/exam/record/select/byExamStudentId", {
          params: { examStudentId: examStudentId }
        })
        .then(function success(response) {
          if (response.data) {
            var examRecordList = response.data;
            if (examRecordList && examRecordList.length > 0) {
              window.open(
                "http://192.168.10.39:8868/#/preview_paper/" +
                  examRecordList[0].basePaperId
              );
            } else {
              alert("该考生未参加考试");
            }
          }
        });
    },
    exportPaper(examStudentId) {
      var currentUser = this.user;
      this.$http
        .get("/api/ecs_oe_admin/exam/record/select/byExamStudentId", {
          params: { examStudentId: examStudentId }
        })
        .then(function success(response) {
          if (response.data) {
            var examRecordList = response.data;
            if (examRecordList && examRecordList.length > 0) {
              var basePaperId = examRecordList[0].basePaperId;
              var rootOrgName = currentUser.rootOrgName;
              var token = currentUser.token;
              var key = currentUser.key;
              window.location.href =
                "http://192.168.10.39:8868" +
                "/api/ecs_ques/paper/export/" +
                basePaperId +
                "/PAPER/" +
                rootOrgName +
                "/" +
                basePaperId +
                "/offLine?$key=" +
                key +
                "&$token=" +
                token;
            } else {
              alert("该考生未参加考试");
            }
          }
        });
    },
    openUploadAnswerDialog(examStudentId) {
      this.uploadAnswerDialogVisible = true;
      this.currentOfflineExamStudentId = examStudentId;
    },
    uploadAnswerChange(event) {
      if (event.target.files.length > 0) {
        this.offlineAnswerFile = event.target.files[0];
      } else {
        this.offlineAnswerFile = "";
      }
    },
    doUploadAnswer() {
      this.$http
        .get("/api/ecs_oe_admin/exam/record/data/findByExamStudentId", {
          params: { examStudentId: this.currentOfflineExamStudentId }
        })
        .then(response => {
          debugger;
          var examRecordDataList = response.data;
          if (examRecordDataList.length == 0) {
            alert("改学生未参加考试");
            return;
          }
          let param = new FormData();
          param.append("file", this.offlineAnswerFile);
          var examRecordDataId = examRecordDataList[0].id;
          param.append("examRecordDataId", examRecordDataId);
          let config = {
            headers: { "Content-Type": "multipart/form-data" }
          };
          this.$http
            .post("/api/ecs_oe_student/offlineExam/submitPaper", param, config)
            .then(() => {
              debugger;
            })
            .catch(function(error) {
              console.log(error);
            });
        });
    }
  },
  created() {}
};
</script>
<style scoped>
.el-row {
  position: static !important;
}
.margin-top-10 {
  margin-top: 10px;
}
</style>
