<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="考试ID">
              <el-input
                class="form_search_width"
                size="small"
                v-model="form.examRecordDataId"
                placeholder="考试ID"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="需要审核">
              <el-select
                class="form_search_width"
                size="small"
                v-model="form.isWarn"
                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="6">
            <el-form-item label="陌生人脸">
              <el-select
                class="form_search_width"
                size="small"
                v-model="form.hasStranger"
                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="6">
            <el-form-item label="人脸比对">
              <el-input
                class="form_search_width_50"
                size="small"
                v-model="form.faceSuccessPercentLower"
                placeholder="下限"
              ></el-input>
              <span>-</span>
              <el-input
                class="form_search_width_50"
                size="small"
                v-model="form.faceSuccessPercentUpper"
                placeholder="上限"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="真实性">
              <el-row>
                <el-input
                  class="form_search_width_50"
                  size="small"
                  v-model="form.livenessSuccessPercentLower"
                  placeholder="下限"
                ></el-input>
                <span>-</span>
                <el-input
                  class="form_search_width_50"
                  size="small"
                  v-model="form.livenessSuccessPercentUpper"
                  placeholder="上限"
                ></el-input>
              </el-row>
            </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.PANEING_BATCHAUDIT">
          <div class="block-seperator"></div>
          <span>操作：</span>
          <el-dropdown class="button_left">
            <el-button size="small" type="primary">
              批量审核 <i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="success"
                  :disabled="batchAuditBtnDisabled"
                  @click="batchAudit('pass')"
                >
                  <i class="el-icon-success"></i> 通 &nbsp;&nbsp;过
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="danger"
                  :disabled="batchAuditBtnDisabled"
                  @click="batchAudit('nopass')"
                >
                  <i class="el-icon-error"></i> 不通过
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </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
            resizable
            stripe
          >
            <el-table-column
              type="selection"
              width="55"
              :selectable="selectable"
            ></el-table-column>
            <el-table-column label="考试ID">
              <template slot-scope="scope">
                <el-button
                  v-show="currentPagePrivileges.SNAPSHOT_DETAILS"
                  @click="gotoCaptureDetail(scope.row.dataId)"
                  type="text"
                  >{{ scope.row.dataId }}</el-button
                >
                <span v-show="!currentPagePrivileges.SNAPSHOT_DETAILS">{{
                  scope.row.dataId
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              sortable
              label="姓名"
              prop="studentName"
            ></el-table-column>
            <el-table-column
              sortable
              label="身份证号"
              prop="identityNumber"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="学号"
              prop="studentCode"
            ></el-table-column>
            <el-table-column
              sortable
              label="学习中心"
              prop="orgName"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="课程"
              prop="courseName"
              width="150"
            ></el-table-column>
            <el-table-column
              sortable
              label="课程层次"
              prop="courseLevel"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="校验次数"
              prop="faceTotalCount"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="成功次数"
              prop="faceSuccessCount"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="陌生人记录"
              prop="faceStrangerCount"
              width="120"
            ></el-table-column>
            <el-table-column
              sortable
              label="人脸识别成功率(%)"
              prop="faceSuccessPercent"
              width="180"
            ></el-table-column>
            <el-table-column
              sortable
              label="人脸真实性比率(%)"
              prop="baiduFaceLivenessSuccessPercent"
              width="180"
            ></el-table-column>
            <el-table-column
              :context="_self"
              label="操作"
              v-if="currentPagePrivileges.PENDING_OPERATE"
              fixed="right"
              width="120"
            >
              <div slot-scope="scope">
                <el-row class="operateRow">
                  <el-col :span="24">
                    <el-button
                      v-if="scope.row.isWarn"
                      size="mini"
                      type="success"
                      icon="el-icon-success"
                      @click="auditPass(scope.row.dataId)"
                      >通&nbsp;&nbsp;&nbsp;&nbsp;过</el-button
                    >
                  </el-col>
                </el-row>
                <el-row class="operateRow">
                  <el-col :span="24">
                    <el-button
                      v-if="scope.row.isWarn"
                      size="mini"
                      type="danger"
                      icon="el-icon-error"
                      @click="openAuditDialog(scope.row.dataId)"
                      >不通过</el-button
                    >
                  </el-col>
                </el-row>
              </div>
            </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="dialogFormVisible"
        @closed="auditDialogClosed"
      >
        <el-form :model="auditForm" ref="auditForm">
          <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="doAudit">确 定</el-button>
            <el-button @click="dialogFormVisible = false">取 消</el-button>
          </div>
        </el-form>
      </el-dialog>
    </el-main>
  </el-container>
</template>
<script>
import { mapState } from "vuex";
import pagePrivilege from "../mixin/pagePrivilege.js";
import commonFormVue from "../component/commonForm.vue";
import { DISCIPLINE_TYPE_LIST } from "../constants/constants";
import _ from "lodash";
export default {
  mixins: [pagePrivilege],
  components: { commonFormVue },
  data() {
    return {
      total: 0,
      tableLoading: false,
      exportLoading: false,
      dialogFormVisible: false,
      showAllCondition: 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
      },
      auditForm: {
        examRecordDataId: null,
        disciplineType: "",
        disciplineDetail: "",
        isPass: null
      },
      getExamCondition: {
        params: {
          name: "",
          examTypes: "ONLINE",
          propertyKeys: "IS_FACE_ENABLE"
        },
        filterCondition: "IS_FACE_ENABLE"
      },
      tableData: [],
      multipleSelection: [],
      batchAuditBtnDisabled: true,
      currentPagePrivileges: {
        PANEING_BATCHAUDIT: false,
        PENDING_OPERATE: false,
        SNAPSHOT_DETAILS: false
      },
      disciplineTypeList: DISCIPLINE_TYPE_LIST
    };
  },
  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,
        livenessSuccessPercentLower: null,
        livenessSuccessPercentUpper: null,
        identityNumber: null,
        orgId: null,
        studentCode: null,
        studentName: null,
        isWarn: null,
        pageNo: 1,
        pageSize: 10
      };
    },
    showMoreCondition() {
      this.showAllCondition = true;
    },
    showSimpleCondition() {
      this.$notify({
        title: "提示",
        message: "高级查询条件值已重置",
        type: "info",
        duration: 2000
      });
      this.resetForm();
      this.showAllCondition = false;
    },
    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/waiting/audit/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/awaitingAudit?" + new URLSearchParams(params)
          });
        });
    },
    selectable(row) {
      return row.isWarn;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    /**
     * pagesize改变时触发
     */
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.search();
    },
    /**
     * 当前页改变时触发
     */
    handleCurrentChange(val) {
      this.form.pageNo = val;
      this.search();
    },
    batchAudit(operate) {
      var isPass = operate == "pass" ? true : false;
      this.$confirm("确定执行?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          var examRecordDataIds = [];
          for (var i = 0; i < this.multipleSelection.length; i++) {
            examRecordDataIds.push(this.multipleSelection[i].dataId);
          }
          var param = new URLSearchParams({
            examRecordDataIds: examRecordDataIds,
            isPass: isPass
          });
          this.$http
            .post("/api/ecs_oe_admin/exam/audit/batch/audit", param)
            .then(() => {
              this.$notify({
                title: "成功",
                message: "操作成功",
                type: "success",
                duration: 5000
              });
              this.search();
            });
        })
        .catch(() => {});
    },
    openAuditDialog(examRecordDataId) {
      this.dialogFormVisible = true;
      this.auditForm = {
        examRecordDataId: examRecordDataId,
        disciplineType: "",
        disciplineDetail: "",
        isPass: false
      };
    },
    doAudit() {
      this.$refs["auditForm"].validate(valid => {
        if (valid) {
          var param = new URLSearchParams(this.auditForm);
          this.$http
            .post("/api/ecs_oe_admin/exam/audit/single/audit", param)
            .then(() => {
              this.$notify({
                title: "成功",
                message: "操作成功",
                type: "success",
                duration: 5000
              });
              this.dialogFormVisible = false;
              this.search();
            });
        } else {
          return false;
        }
      });
    },
    auditDialogClosed() {
      this.$refs["auditForm"].resetFields();
    },
    /**
     * 审核通过
     */
    auditPass(examRecordDataId) {
      var auditParams = {
        examRecordDataId: examRecordDataId,
        isPass: true
      };
      var param = new URLSearchParams(auditParams);
      this.$http
        .post("/api/ecs_oe_admin/exam/audit/single/audit", param)
        .then(() => {
          this.search();
          this.$notify({
            title: "成功",
            message: "操作成功",
            type: "success",
            duration: 5000
          });
        });
    },
    gotoCaptureDetail(examRecordDataId) {
      let pendingOperate = !this.currentPagePrivileges.PENDING_OPERATE;
      this.$router.push({
        path: "/oe/captureDetail/" + examRecordDataId + "/awaitingAudit",
        // name: "captureDetail",
        query: { pendingOperate }
      });
    },
    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();
      }
    }
  },
  watch: {
    multipleSelection: function() {
      this.batchAuditBtnDisabled = !(this.multipleSelection.length > 0);
    }
  },
  created() {
    this.backFill();
  }
};
</script>
<style scoped src="../style/common.css"></style>
