<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" v-if="printShow == '1'">
          <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="130px">
                <el-col :span="24" class="one_2txt">
                  <el-col :span="8">
                    <el-form-item label="姓名">
                      <el-input v-model="searchFrom.name"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="身份证号">
                      <el-input v-model="searchFrom.card"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="打印公安机关名称">
                      <el-input v-model="searchFrom.print_policename"></el-input>
                    </el-form-item>
                  </el-col>
                </el-col>
                <el-col :span="24" class="one_2txt">
                  <el-col :span="8">
                    <el-form-item label="出生日期">
                      <el-date-picker
                        v-model="searchFrom['start@birth']"
                        placeholder="请选择"
                        value-format="yyyy-MM-dd"
                        format="yyyy-MM-dd"
                        type="date"
                        style="width: 100%"
                      >
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="至">
                      <el-date-picker
                        v-model="searchFrom['end@birth']"
                        placeholder="请选择"
                        value-format="yyyy-MM-dd"
                        format="yyyy-MM-dd"
                        type="date"
                        style="width: 100%"
                      >
                      </el-date-picker>
                    </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="8">
                    <el-form-item label="发证日期">
                      <el-date-picker
                        v-model="searchFrom['start@cert_date']"
                        placeholder="请选择"
                        value-format="yyyy-MM-dd"
                        format="yyyy-MM-dd"
                        type="date"
                        style="width: 100%"
                      >
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="至">
                      <el-date-picker
                        v-model="searchFrom['end@cert_date']"
                        placeholder="请选择"
                        value-format="yyyy-MM-dd"
                        format="yyyy-MM-dd"
                        type="date"
                        style="width: 100%"
                      >
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8" class="btn">
                    <el-button type="primary" size="mini" @click="morePrint()">批量打印</el-button>
                    <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"
              @print="toPrint"
              :select="true"
              :selected="selected"
              @handleSelect="handleSelect"
            >
            </data-table>
          </el-col>
        </el-col>
        <el-col :span="24" v-else-if="printShow == '2'">
          <el-col :span="24" class="thr">
            <el-button type="primary" size="mini" @click="setSize()">设置尺寸</el-button>
            <el-button type="primary" size="mini" @click="certPrint()">打印证书</el-button>
            <el-button type="primary" size="mini" @click="previewPrint()">预览打印</el-button>
            <el-button type="primary" size="mini" @click="back()">返回</el-button>
          </el-col>
          <el-col :span="24" class="four" ref="print">
            <list-1 :list="printList" :baoan_cert_size="baoan_cert_size"></list-1>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog class="twodialog" title="设置尺寸" :visible.sync="setShow" width="50%" :before-close="setClose" :close-on-click-modal="false">
      <size-1 :baoan_cert_size="baoan_cert_size" @close="setClose" @onSubmit="setSubmit"></size-1>
    </el-dialog>
  </div>
</template>

<script>
import list1 from '../bayzzz/parts/list-1.vue';
import size1 from '../bayzzz/parts/size-1.vue';
const { exam_grade } = require('@common/src/layout/deploy/dict');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: certificates_print_again } = createNamespacedHelpers('certificates_print_again');
const { mapActions: certificates_print_log } = createNamespacedHelpers('certificates_print_log');
const moment = require('moment');
export default {
  name: 'index',
  props: {},
  components: { list1, size1 },
  data: function () {
    return {
      // 页面显示
      printShow: '1',
      // 查询表单
      searchFrom: {},
      list: [],
      total: 0,
      fields: [
        { label: '姓名', model: 'name' },
        { label: '身份证号', model: 'card' },
        { label: '出生日期', model: 'birth' },
        { label: '联系电话', model: 'phone' },
        { label: '职业等级', model: 'exam_grade' },
        { label: '发证日期', model: 'cert_date' },
        { label: '保安员编号', model: 'baoan_num' },
        { label: '证件状态', model: 'cert_status' },
      ],
      opera: [{ label: '打印', method: 'print' }],
      exam_gradeList: exam_grade,
      // 打印
      // 打印所选原数据列表
      selected: [],
      // 打印尺寸处理数据集
      printList: [],
      // 打印设置尺寸
      baoan_cert_size: {},
      setShow: false,
    };
  },
  async created() {
    // 默认尺寸
    this.searchSize();
    await this.search();
  },
  methods: {
    ...certificates_print_again(['query', 'update']),
    ...certificates_print_log(['create']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchFrom);
      let res = await this.query({ skip, limit, status: '1', ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 打印
    // 批量选择打印证书数据
    handleSelect(data) {
      this.$set(this, `selected`, data);
    },
    // 单个选择打印证书数据
    toPrint({ data }) {
      this.selected.push(data);
      this.morePrint();
    },
    // 打印跳转
    morePrint() {
      let data = _.cloneDeep(this.selected);
      for (const val of data) {
        val.birth = moment(val.birth).format('yyyy  MM  DD');
        val.year = moment(val.cert_date).format('yyyy');
        val.month = moment(val.cert_date).format('MM');
      }
      this.$set(this, `printList`, data);
      this.printShow = '2';
    },
    // 证书打印
    async certPrint() {
      this.$confirm(`证书一旦打印不可更改,请谨慎操作！`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          let selected = this.selected;
          for (const data of selected) {
            data.status = '2';
            data.print_personal = this.user.department_name;
            data.print_policename = this.user.department_name;
            data.print_date = moment(new Date()).format('YYYY-MM-DD');
            let p1 = await this.update(data);
            if (this.$checkRes(p1)) {
            } else {
              this.$message({ type: `fail`, message: `${p1.errmsg}` });
            }
          }
          // 创建打印日志
          this.createJournal(selected);
          this.$print(this.$refs.print);
        })
        .catch(() => {});
    },
    // 预览证书打印
    previewPrint() {
      this.$print(this.$refs.print);
    },
    // 创建打印日志
    async createJournal(selected) {
      for (const data of selected) {
        let arr = {
          baoan_num: data.baoan_num,
          name: data.name,
          print_personal: data.print_personal,
          print_policename: data.print_policename,
          print_date: data.print_date,
          is_make: '是',
          make_apply: data.make_apply,
          make_applynum: data.make_applynum,
          make_applypolice: data.make_applypolice,
          make_applypersonal: data.make_applypersonal,
          make_applydate: data.make_applydate,
          make_examinenum: data.make_examinenum,
          make_examinename: data.make_examinename,
          make_examinepersonal: data.make_examinepersonal,
          make_examinedate: data.make_examinedate,
          make_examineresaon: data.make_examineresaon,
          check_xzjl: data.check_xzjl,
          check_xscf: data.check_xscf,
          check_certthr: data.check_certthr,
          check_certtwo: data.check_certtwo,
          check_fxpg: data.check_fxpg,
          check_zhpd: data.check_zhpd,
        };
        let p2 = await this.create(arr);
        if (this.$checkRes(p2)) {
          this.$message({ type: `success`, message: `操作成功` });
        } else {
          this.$message({ type: `success`, message: `${p2.errmsg}` });
        }
      }
    },
    // 设置尺寸
    setSize() {
      this.setShow = true;
    },
    // 提交设置的尺寸
    setSubmit({ data }) {
      localStorage.setItem('baoan_cert_size', JSON.stringify(data));
      this.searchSize();
      this.setClose();
    },
    // 关闭设置尺寸
    setClose() {
      this.setShow = false;
    },
    // 返回
    back() {
      this.printShow = '1';
      this.search();
    },
    // 打印尺寸默认值查询
    searchSize() {
      let data = JSON.parse(localStorage.getItem('baoan_cert_size'));
      if (data) {
        this.$set(this, `baoan_cert_size`, data);
      } else {
        let data = {
          p0: { width: 720, height: 485 },
          p1: { top: 90, right: 0, bottom: 0, left: 70 },
          p2: { top: 220, right: 0, bottom: 0, left: 110 },
          p3: { top: 300, right: 0, bottom: 0, left: 165 },
          p4: { top: 380, right: 0, bottom: 0, left: 185 },
          p5: { top: 200, right: 0, bottom: 0, left: 495 },
          p6: { top: 240, right: 0, bottom: 0, left: 495 },
          p7: { top: 280, right: 0, bottom: 0, left: 495 },
          p8: { top: 380, right: 0, bottom: 0, left: 495 },
        };
        localStorage.setItem('baoan_cert_size', JSON.stringify(data));
        this.$set(this, `baoan_cert_size`, data);
      }
    },
  },
  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>
