<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-col :span="24" class="one_1"> 保安员考场考试情况登记查询 </el-col>
          <el-col :span="24" class="one_2">
            <el-form ref="searchFrom" :model="searchFrom" label-width="100px">
              <el-col :span="24" class="one_2txt">
                <el-col :span="8">
                  <el-form-item label="考试地点">
                    <el-input v-model="searchFrom.exam_addr"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="公安机关">
                    <el-input v-model="searchFrom.police_office"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="考试等级">
                    <el-select v-model="searchFrom.exam_grade" clearable placeholder="请选择">
                      <el-option v-for="(item, index) in exam_gradeList" :key="index" :label="item" :value="item"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :span="24" class="one_2txt">
                <el-col :span="24" class="btn">
                  <el-button type="primary" size="mini" @click="search()">查询</el-button>
                  <el-button type="primary" size="mini" @click="getTemplate">导入格式文件下载</el-button>
                </el-col>
              </el-col>
            </el-form>
          </el-col>
        </el-col>
        <el-col :span="24" class="two">
          <data-table @query="search" :fields="fields" :opera="opera" :data="list" :total="total" @check="check" @achieveExp="achieveExp" @conCheck="conCheck">
          </data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog class="showinfo" title="信息登记" :visible.sync="showinfo" width="70%" :before-close="close" :close-on-click-modal="false">
      <info-1 :info="info" :list="infoList" @saveBtn="saveBtn"></info-1>
    </el-dialog>
    <el-dialog class="showAchieve" title="成绩导入" :visible.sync="achieveShow" width="30%" :before-close="achClose" :close-on-click-modal="false">
      <achieve-1 :form="achieveForm" @achClose="achClose" @twoSubmit="twoSubmit"></achieve-1>
    </el-dialog>
  </div>
</template>

<script>
import info1 from './parts/info-1.vue';
import achieve1 from './parts/achieve-1.vue';
const { exam_grade } = require('@common/src/layout/deploy/dict');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: examination_room } = createNamespacedHelpers('examination_room');
const { mapActions: examination_examinee } = createNamespacedHelpers('examination_examinee');
const { mapActions: import_file } = createNamespacedHelpers('import_file');
export default {
  name: 'index',
  props: {},
  components: { info1, achieve1 },
  data: function () {
    return {
      // 查询
      searchFrom: {},
      // 列表
      fields: [
        { label: '考场编码', model: 'testsite_num' },
        { label: '考试日期', model: 'exam_date' },
        { label: '考试时间', model: 'exam_time' },
        { label: '考试等级', model: 'exam_grade' },
        { label: '考试地点', model: 'exam_addr' },
        { label: '计划参考人数', model: 'test_number' },
        { label: '编排时间', model: 'room_date' },
      ],
      opera: [
        { label: '登记', method: 'check', type: 'primary', display: (i) => i.status === '2' },
        { label: '成绩导入', method: 'achieveExp', type: 'primary', display: (i) => i.status === '2' },
        { label: '确认登记', method: 'conCheck', type: 'primary', display: (i) => i.status === '2' },
      ],
      list: [],
      total: 0,
      // 考试等级
      exam_gradeList: exam_grade,
      // 考场情况登记
      showinfo: false,
      info: {},
      infoList: [],
      // 考场考生成绩导入
      achieveShow: false,
      achieveForm: {},
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...examination_room(['query', 'update']),
    ...examination_examinee({ bmjfQuery: 'query', bmjfUpdate: 'update' }),
    ...import_file(['importExam']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchFrom);
      let res = await this.query({ skip, limit, ...condition, status: '2', ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 考试情况登记
    async check({ data }) {
      let res = await this.bmjfQuery({ room_id: data.id });
      if (this.$checkRes(res)) {
        this.$set(this, `infoList`, res.data);
        data.register_personal = this.user.name;
        this.$set(this, `info`, data);
        this.showinfo = true;
      }
    },
    // 保存考场考试登记信息
    async saveBtn({ data }) {
      let res = await this.update(data);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `操作成功` });
      } else {
        this.$message({ type: `success`, message: `${res.errmsg}` });
      }
    },
    async conCheck({ data }) {
      let res = await this.bmjfQuery({ room_id: data.id });
      if (this.$checkRes(res)) {
        for (const val of res.data) {
          // 计算成绩是否合格，60分分界点
          val.status = '3';
          let p1 = await this.bmjfUpdate(val);
        }
        data.status = '3';
        let p2 = await this.update(data);
        if (this.$checkRes(p2)) {
          this.$message({ type: `success`, message: `操作成功` });
          this.search();
        } else {
          this.$message({ type: `success`, message: `${res.errmsg}` });
        }
      }
    },
    // 关闭查看
    close() {
      this.showinfo = false;
    },
    // 成绩导入
    // 打开导入
    achieveExp() {
      this.achieveShow = true;
    },
    // 提交导入
    async twoSubmit({ data }) {
      let res = await this.importExam(data);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `操作成功` });
        this.achieveForm = { uri: '' };
        this.achClose();
      }
    },
    // 成绩导入关闭
    achClose() {
      this.achieveShow = false;
    },
    // 导入格式文件下载
    getTemplate() {
      window.open(`${process.env.VUE_APP_HOST}/${process.env.VUE_APP_ROUTER}/考试成绩导入文件模板.xlsx`, '_blank');
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    .one_1 {
      height: 40px;
      line-height: 40px;
      border-bottom: 1px solid #ccc;
      margin: 0 0 10px 0;
      font-size: 18px;
      font-weight: bold;
    }
    .one_2 {
      .one_2txt {
        border: 1px solid #ccc;
        /deep/.el-form-item {
          margin: 0;
        }
        /deep/.el-date-editor.el-input,
        .el-date-editor.el-input__inner {
          width: 100%;
        }
        /deep/.el-select {
          width: 100%;
        }
        .btn {
          text-align: right;
          padding: 5px 5px;
        }
      }
    }
  }
}
/deep/.el-dialog__body {
  padding: 10px;
}
</style>
