<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.name" placeholder="请输入姓名"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="身份证号">
                    <el-input v-model="searchFrom.card" placeholder="请输入身份证号"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="联系电话">
                    <el-input v-model="searchFrom.phone" placeholder="请输入联系电话"></el-input>
                  </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-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" @pass="toPass" @nopass="toNopass"> </data-table>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: examination_examinee } = createNamespacedHelpers('examination_examinee');
const { mapActions: security_guard_base } = createNamespacedHelpers('security_guard_base');
const { mapActions: certificates_base } = createNamespacedHelpers('certificates_base');
const { mapActions: sendTemplate } = createNamespacedHelpers('sendTemplate');
const moment = require('moment');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      // 查询
      searchFrom: {},
      // 列表
      fields: [
        { label: '姓名', model: 'name' },
        { label: '身份证号', model: 'card' },
        { label: '考试等级', model: 'exam_grade' },
        { label: '考试日期', model: 'exam_date' },
        { label: '准考证号', model: 'exam_num' },
        { label: '考试类型', model: 'exam_type' },
        { label: '准考证打印日期', model: 'exam_type' },
        { label: '考试成绩', model: 'exam_achieve' },
        { label: '联系电话', model: 'phone' },
        { label: '公安机关', model: 'police_office' },
      ],
      opera: [
        { label: '合格', method: 'pass', type: 'primary', display: (i) => i.status === '3' && i.exam_achieve >= 60 },
        { label: '不合格', method: 'nopass', type: 'primary', display: (i) => i.status === '3' && i.exam_achieve < 60 },
      ],
      list: [],
      total: 0,
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...examination_examinee(['query', 'update']),
    ...security_guard_base({ baseFetch: 'fetch', baseUpdate: 'update' }),
    ...certificates_base(['create']),
    ...sendTemplate(['send']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchFrom);
      let res = await this.query({ skip, limit, ...condition, ...this.searchFrom, status: '3', ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 合格考生
    async toPass({ data }) {
      // 修改考生信息，4：考试合格的学生
      data.status = '4';
      let p0 = await this.update(data);
      if (this.$checkRes(p0)) {
        // 修改个人信息表，状态为7：待制证
        this.updateBase(data);
        // 发送消息，合格可预约领取保安员证
        this.sendTem(data);
      }
    },
    // 修改个人信息表，状态为7：待制证；并创建证件表
    async updateBase(data) {
      let p1 = await this.baseFetch({ id: data.security_guard_id });
      if (this.$checkRes(p1)) {
        p1.data.status = '7';
        let p2 = await this.baseUpdate(p1.data);
        if (this.$checkRes(p2)) {
          // 创建证件表
          this.createCert(data, p1.data);
        } else {
          this.$message({ type: `success`, message: `${p2.errmsg}` });
        }
      }
    },
    // 创建制证表
    async createCert(data, base) {
      let arr = {
        name: data.name,
        gender: data.gender,
        card: data.card,
        phone: data.phone,
        exam_grade: data.exam_grade,
        baoan_num: data.exam_num,
        security_guard_id: data.security_guard_id,
        birth: base.birth,
        house_onaddress: base.house_onaddress,
        is_cert: '0',
        is_receive: '1',
        cert_status: '0',
      };
      let p3 = await this.create(arr);
      if (this.$checkRes(p3)) {
        this.$message({ type: `success`, message: `操作成功` });
        this.search();
      } else {
        this.$message({ type: `success`, message: `${res.errmsg}` });
      }
    },
    // 不合格考生
    async toNopass({ data }) {
      data.status = '-4';
      let p0 = await this.update(data);
      if (this.$checkRes(p0)) {
        this.$message({ type: `success`, message: `操作成功` });
        // 发送消息，不合格，可以补考
        this.sendTem(data);
        this.search();
      } else {
        this.$message({ type: `success`, message: `${p0.errmsg}` });
      }
    },
    // 发送消息
    async sendTem(data) {
      let res = await this.baseFetch({ id: data.security_guard_id });
      if (this.$checkRes(res)) {
        let p1 = {
          openid: res.data.gopenid,
          unionid: res.data.unionid,
          templateId: process.env.VUE_APP_templateId,
          url: '',
          topColor: '',
          data: {
            first: { value: '保安员报名考试结果' },
            keyword1: { value: '考试成绩' },
            keyword2: { value: data.status == '4' ? '考试合格' : '考试不合格' },
            keyword3: { value: moment(new Date()).format('YYYY-MM-DD') },
            remark: {
              value: data.status == '4' ? '保安员考试成绩合格。您可登录系统查看详细考试信息' : '保安员考试成绩不合格，您可联系相关人员，进行安排补考！',
            },
          },
        };
        let p2 = await this.send(p1);
        if (p2.errcode == '0' && p2.wxOpenid) {
          let p3 = await this.baseUpdate({ id: this.basic_id, gopenid: p2.wxOpenid });
          if (this.$checkRes(p3));
        }
      }
    },
  },
  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;
        }
      }
    }
  }
}
</style>
