<template>
  <el-container>
    <el-main class="el-main-padding">
      <commonFormVue :form="form" :getExamCondition="getExamCondition">
        <el-row v-show="showAllCondition">
          <el-col :span="6">
            <el-form-item label="采集人">
              <el-input
                class="form_search_width"
                size="small"
                v-model="form.infoCollector"
                placeholder="采集人"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开考时间">
              <el-date-picker
                class="input"
                v-model="startExamDatetimeRange"
                type="datetimerange"
                start-placeholder="开始日期"
                range-separator="至"
                end-placeholder="结束日期"
                value-format="yyyy/MM/dd HH:mm:ss"
                :clearable="false"
                size="small"
                @change="changeStartExamDatetimeRange"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </commonFormVue>
      <el-col :span="24">
        <el-button
          @click="search('clickSelectBtn')"
          size="small"
          type="primary"
          icon="el-icon-search"
          >查询</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-more"
          v-if="!showAllCondition"
          @click="showMoreCondition"
          >高级查询</el-button
        >
        <el-button
          size="small"
          type="primary"
          v-if="showAllCondition"
          @click="showSimpleCondition"
          >简单查询</el-button
        >
        <el-button
          size="small"
          icon="el-icon-refresh"
          @click="resetForm"
          class="margin-bottom-10"
          >重置</el-button
        >
      </el-col>
      <el-row>
        <el-col v-show="currentPagePrivileges.EXAM_DETAIL_EXPORT">
          <div class="block-seperator"></div>
          <span>操作：</span>
          <commonExportVue
            :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 label="考试ID" width="120">
              <template slot-scope="scope">
                <el-button
                  v-show="
                    scope.row.examType == 'ONLINE' &&
                      currentPagePrivileges.SNAPSHOT_DETAILS
                  "
                  @click="gotoCaptureDetail(scope.row.dataId)"
                  type="text"
                  >{{ scope.row.dataId }}</el-button
                >
                <span
                  v-show="
                    scope.row.examType != 'ONLINE' ||
                      !currentPagePrivileges.SNAPSHOT_DETAILS
                  "
                  >{{ scope.row.dataId }}</span
                >
              </template>
            </el-table-column>
            <el-table-column
              sortable
              label="学习中心"
              prop="orgName"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="姓名"
              prop="studentName"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="身份证号"
              prop="identityNumber"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="学号"
              prop="studentCode"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="课程"
              prop="courseNameAndCode"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="课程层次"
              prop="courseLevel"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="年级"
              prop="grade"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="学生电话"
              prop="phone"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="采集人"
              prop="infoCollector"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="考试次数"
              prop="examOrder"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="开考时间"
              prop="paperStartTime"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="交卷时间"
              prop="paperSubmitTime"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              :sort-method="sortByExamTime"
              label="持续时间"
              prop="examTime"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="校验成功率(%)"
              prop="faceSuccessPercent"
              width="150"
            ></el-table-column>
            <el-table-column
              sortable
              label="续考次数"
              prop="continuedCount"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              :sort-method="sortByPaperTotalScore"
              label="卷面总分"
              prop="paperTotalScore"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="是否违纪"
              prop="isIllegality"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="是否提交"
              prop="isSubmit"
              width="120"
            ></el-table-column>
            <el-table-column fixed="right" label="详情" width="100">
              <template slot-scope="scope">
                <el-row
                  class="operateRow"
                  v-if="
                    scope.row.examType == 'ONLINE' &&
                      currentPagePrivileges.EXAM_QUERY_GETPAPER
                  "
                >
                  <el-col :span="24">
                    <el-button
                      size="mini"
                      type="primary"
                      icon="el-icon-view"
                      plain
                      @click="
                        examPaperDetail(scope.row.courseId, scope.row.dataId)
                      "
                      >调&nbsp;卷</el-button
                    >
                  </el-col>
                </el-row>
                <el-row
                  class="operateRow"
                  v-if="
                    scope.row.examType == 'ONLINE' &&
                      currentPagePrivileges.REDO_AUDIT
                  "
                >
                  <el-col :span="24">
                    <el-dropdown>
                      <el-button
                        size="mini"
                        icon="el-icon-arrow-down"
                        type="primary"
                        plain
                        >重审</el-button
                      >
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item>
                          <el-button
                            size="mini"
                            type="success"
                            @click="redoAudit(scope.row.dataId, 'pass')"
                            icon="el-icon-success"
                            >通&nbsp;&nbsp;过</el-button
                          >
                        </el-dropdown-item>
                        <el-dropdown-item>
                          <el-button
                            size="mini"
                            type="danger"
                            @click="redoAudit(scope.row.dataId, 'nopass')"
                            icon="el-icon-error"
                            >不通过</el-button
                          >
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
                <el-row
                  class="operateRow"
                  v-if="
                    scope.row.examType == 'OFFLINE' &&
                      scope.row.offlineFileUrl &&
                      currentPagePrivileges.EXAM_QUERY_GETPAPER
                  "
                >
                  <el-col :span="24">
                    <el-button
                      size="mini"
                      type="primary"
                      icon="el-icon-view"
                      plain
                      @click="downloadOfflineFile(scope.row.offlineFileUrl)"
                      >调卷</el-button
                    >
                  </el-col>
                </el-row>
              </template>
            </el-table-column>
          </el-table>
          <div class="block pull-right">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page.sync="form.pageNo"
              :page-sizes="[10, 20, 50, 100, 200, 300]"
              :page-size="form.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            ></el-pagination>
          </div>
        </el-col>
      </el-row>
      <el-dialog
        title="审核"
        :visible.sync="dialogAuditFormVisible"
        @closed="auditDialogClosed"
      >
        <el-form :model="auditForm" ref="redoAuditForm">
          <el-form-item
            label="违纪类型"
            prop="disciplineType"
            :rules="[
              { required: true, message: '请选择违纪类型', trigger: 'change' }
            ]"
          >
            <el-select v-model="auditForm.disciplineType" placeholder="请选择">
              <el-option
                v-for="item in disciplineTypeList"
                :key="item.name"
                :label="item.desc"
                :value="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="详情描述" style="margin-top:15px;">
            <el-input
              v-model="auditForm.disciplineDetail"
              type="textarea"
              :autosize="{ minRows: 6, maxRows: 10 }"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
          <div class="dialog-footer margin-top-10 text-center">
            <el-button type="primary" @click="doRedoAuditNoPass"
              >确 定</el-button
            >
            <el-button @click="dialogAuditFormVisible = false">取 消</el-button>
          </div>
        </el-form>
      </el-dialog>
    </el-main>
  </el-container>
</template>
<script>
import { mapState } from "vuex";
import commonFormVue from "../component/commonForm.vue";
import commonExportVue from "../component/commonExport.vue";
import { DISCIPLINE_TYPE_LIST } from "../constants/constants";
import pagePrivilege from "../mixin/pagePrivilege.js";
import _ from "lodash";
export default {
  components: { commonFormVue, commonExportVue },
  mixins: [pagePrivilege],
  data() {
    return {
      disciplineTypeList: DISCIPLINE_TYPE_LIST,
      total: 0,
      tableLoading: false,
      showAllCondition: false,
      startExamDatetimeRange: [],
      form: {
        examRecordDataId: null,
        hasStranger: null,
        courseId: null,
        courseLevel: null,
        examId: null,
        examRecordId: null,
        faceSuccessPercentLower: null,
        faceSuccessPercentUpper: null,
        identityNumber: null,
        orgId: null,
        studentCode: null,
        studentName: null,
        isWarn: null,
        pageNo: 1,
        pageSize: 10,
        rootOrgId: null,
        startTime: null,
        endTime: null,
        infoCollector: null
      },

      getExamCondition: {
        params: {
          name: "",
          examTypes: "ONLINE#OFFLINE"
        },
        filterCondition: ""
      },
      tableData: [],
      exportUrl: "/api/ecs_oe_admin/exam/record/detail/list/export",
      exportFileName: "考试明细",
      currentPagePrivileges: {
        EXAM_DETAIL_EXPORT: false, //导出
        SNAPSHOT_DETAILS: false, //详情查看
        EXAM_QUERY_GETPAPER: false, //调卷
        REDO_AUDIT: false //重新审核
      },
      dialogAuditFormVisible: false,
      auditForm: {
        examRecordDataId: null,
        disciplineType: "",
        disciplineDetail: "",
        isPass: null
      }
    };
  },
  computed: {
    ...mapState({ user: state => state.user })
  },
  methods: {
    resetForm() {
      this.form = {
        examRecordDataId: null,
        hasStranger: null,
        courseId: null,
        courseLevel: null,
        examId: null,
        examRecordId: null,
        faceSuccessPercentLower: null,
        faceSuccessPercentUpper: null,
        identityNumber: null,
        orgId: null,
        studentCode: null,
        studentName: null,
        isWarn: null,
        pageNo: 1,
        pageSize: 10,
        rootOrgId: null,
        startTime: null,
        endTime: null,
        infoCollector: null
      };
      this.startExamDatetimeRange = [];
    },
    showMoreCondition() {
      this.showAllCondition = true;
    },
    showSimpleCondition() {
      this.$notify({
        title: "提示",
        message: "高级查询条件值已重置",
        type: "info",
        duration: 2000
      });
      this.resetForm();
      this.showAllCondition = false;
    },
    changeStartExamDatetimeRange(e) {
      if (e && e.length > 0) {
        this.form.startTime = e[0];
        this.form.endTime = e[1];
      } else {
        this.form.startTime = "";
        this.form.endTime = "";
      }
    },
    search(type) {
      if (!this.form.examId) {
        this.$notify({
          title: "警告",
          message: "请选择考试批次",
          type: "warning",
          duration: 2000
        });
        return false;
      }
      if (type && type == "clickSelectBtn") {
        this.form.pageNo = 1;
      }
      this.tableLoading = true;
      var params = JSON.parse(JSON.stringify(this.form));
      this.$http
        .post("/api/ecs_oe_admin/exam/record/detail/list", params)
        .then(response => {
          if (response.data) {
            this.tableData = response.data.content;
            this.total = response.data.totalElements;
            this.form.pageNo = response.data.number + 1;
          } else {
            this.tableData = [];
          }
          this.tableLoading = false;
          this.$router.push({
            path: "/oe/examDetail?" + new URLSearchParams(params)
          });
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    /**
     * pagesize改变时触发
     */
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.search();
    },
    /**
     * 当前页改变时触发
     */
    handleCurrentChange() {
      this.search();
    },
    examPaperDetail(courseId, examRecordDataId) {
      window.open(
        "/admin/oe/examPaperDetail/" + courseId + "/" + examRecordDataId
      );
    },
    gotoCaptureDetail(examRecordDataId) {
      this.$router.push({
        path: "/oe/captureDetail/" + examRecordDataId + "/examDetail"
      });
    },
    downloadOfflineFile(url) {
      window.open(url);
    },
    redoAudit(examRecordDataId, isPass) {
      if (isPass != "pass") {
        this.dialogAuditFormVisible = true;
        this.auditForm.examRecordDataId = examRecordDataId;
      } else {
        //审核通过
        var redoAuditInfo = {
          examRecordDataIds: [examRecordDataId],
          isPass: true,
          disciplineType: "",
          disciplineDetail: ""
        };
        this.$http
          .post("/api/ecs_oe_admin/exam/audit/redoAudit", redoAuditInfo)
          .then(() => {
            this.$notify({
              title: "成功",
              message: "操作成功",
              type: "success"
            });
            this.search();
          })
          .catch(res => {
            var errorMsg = "操作失败";
            if (res.response && res.response.data) {
              errorMsg = res.response.data.desc;
            }
            this.$notify({
              title: "提示",
              message: errorMsg,
              type: "error"
            });
          });
      }
    },
    doRedoAuditNoPass() {
      this.$refs["redoAuditForm"].validate(valid => {
        if (valid) {
          var redoAuditInfo = {
            examRecordDataIds: [this.auditForm.examRecordDataId],
            isPass: false,
            disciplineType: this.auditForm.disciplineType,
            disciplineDetail: this.auditForm.disciplineDetail
          };
          this.$http
            .post("/api/ecs_oe_admin/exam/audit/redoAudit", redoAuditInfo)
            .then(() => {
              this.$notify({
                title: "成功",
                message: "操作成功",
                type: "success"
              });
              this.doRedoAuditNoPassPostProcess();
              this.search();
            })
            .catch(res => {
              var errorMsg = "操作失败";
              if (res.response && res.response.data) {
                errorMsg = res.response.data.desc;
              }
              this.doRedoAuditNoPassPostProcess();
              this.$notify({
                title: "提示",
                message: errorMsg,
                type: "error"
              });
            });
        } else {
          return false;
        }
      });
    },
    doRedoAuditNoPassPostProcess() {
      this.auditForm = {
        examRecordDataId: null,
        disciplineType: "",
        disciplineDetail: "",
        isPass: null
      };
      this.$refs["redoAuditForm"].resetFields();
      this.dialogAuditFormVisible = false;
    },
    auditDialogClosed() {
      this.$refs["redoAuditForm"].resetFields();
    },
    backFill() {
      var formData = this.$route.query;
      if (formData && formData.examId) {
        for (var attr in formData) {
          var value = formData[attr];
          if (value && value != "null") {
            //是数字 且 长度小于15
            if (!isNaN(value) && value.toString().length < 15) {
              value = _.toNumber(value);
            }
            this.form[attr] = value;
          }
        }
        this.search();
      }
    },
    sortByPaperTotalScore(obj1, obj2) {
      let p1 = Number(obj1.paperTotalScore);
      let p2 = Number(obj2.paperTotalScore);
      return p1 - p2;
    },
    sortByExamTime(obj1, obj2) {
      let p1 = Number(obj1.usedExamTime);
      let p2 = Number(obj2.usedExamTime);
      if (isNaN(p1)) {
        p1 = 0;
      }
      if (isNaN(p2)) {
        p2 = 0;
      }
      return p1 - p2;
    }
  },
  created() {
    this.form.rootOrgId = this.user.rootOrgId;
    this.backFill();
  }
};
</script>
<style scope>
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: auto !important;
}
</style>
<style scoped src="../style/common.css"></style>
