<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-button type="primary" size="mini" @click="back()">返回</el-button>
          <el-button type="primary" size="mini" @click="toPrint()">打印</el-button>
        </el-col>
        <el-col :span="24" class="two">
          <el-col :span="24" class="print" ref="print">
            <zkz-1 :info="info"></zkz-1>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import zkz1 from './parts/zkz-1.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: examination_examinee } = createNamespacedHelpers('examination_examinee');
const { mapActions: security_guard_collect } = createNamespacedHelpers('security_guard_collect');
const moment = require('moment');
export default {
  name: 'index',
  props: {},
  components: { zkz1 },
  data: function () {
    return {
      info: {},
    };
  },
  created() {
    if (this.id) this.search();
  },
  methods: {
    ...examination_examinee({ zkzFetch: 'fetch', zkzUpdate: 'update' }),
    ...security_guard_collect(['query']),
    async search() {
      if (this.type == 'zkz') {
        let res = await this.zkzFetch({ id: this.id });
        if (this.$checkRes(res)) {
          let arr = await this.query({ security_guard_id: res.data.security_guard_id, type: '人像' });
          if (this.$checkRes(arr)) {
            let head = arr.data[0];
            if (head) res.data.head = head.data;
            this.$set(this, `info`, res.data);
          }
        }
      }
    },
    // 打印
    async toPrint() {
      this.$confirm(`您确认提交打印准考证吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          let data = { id: this.info.id, exam_printdate: moment(new Date()).format('YYYY-MM-DD'), exam_printper: this.user.name };
          let res = await this.zkzUpdate(data);
          if (this.$checkRes(res)) {
            this.$print(this.$refs.print);
          } else {
            this.$message({ type: `success`, message: `${res.errmsg}` });
          }
        })
        .catch(() => {});
    },
    back() {
      history.go(-1);
    },
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
    type() {
      return this.$route.query.type;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    margin: 0 0 10px 0;
  }
  .two {
    .print {
      border: 1px solid #ff0000;
      width: 717px;
    }
  }
}
</style>
