<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="170px">
                <el-col :span="24" class="one_2txt">
                  <el-col :span="8">
                    <el-form-item label="企业名称">
                      <el-input v-model="searchFrom.company_name"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="管辖单位">
                      <el-input v-model="searchFrom.tube_company"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="备案证明编号">
                      <el-input v-model="searchFrom.licence_num"></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-select v-model="searchFrom.cert_status" clearable placeholder="请选择">
                        <el-option label="正常" value="0"></el-option>
                        <el-option label="注销" value="1"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <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>
                <el-col :span="24" class="one_2txt">
                  <el-col :span="8">
                    <el-form-item label="法人姓名">
                      <el-input v-model="searchFrom.legal_personal"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="法人代表证件号码">
                      <el-input v-model="searchFrom.legal_card"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8" class="btn">
                    <el-button type="primary" size="mini" @click="search()">查询</el-button>
                    <el-button type="primary" size="mini" @click="morePrint()">批量打印</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"
              @view="toView"
              @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">
            <el-col :span="24" class="print" ref="print">
              <list-1 :list="printList" :company_keep_size="company_keep_size"></list-1>
            </el-col>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog class="dialog" title="信息详情" :visible.sync="show" width="60%" :before-close="close" :close-on-click-modal="false">
      <info-1 :info="info"></info-1>
    </el-dialog>
    <el-dialog class="twodialog" title="备案证明设置尺寸" :visible.sync="setShow" width="50%" :before-close="setClose" :close-on-click-modal="false">
      <size-1 :company_keep_size="company_keep_size" @close="setClose" @onSubmit="setSubmit"></size-1>
    </el-dialog>
  </div>
</template>

<script>
import info1 from './parts/info-1.vue';
import size1 from './parts/size-1.vue';
import list1 from './parts/list-1.vue';

import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: company_keep } = createNamespacedHelpers('company_keep');
const { mapActions: company_keep_grant } = createNamespacedHelpers('company_keep_grant');
const moment = require('moment');
export default {
  name: 'index',
  props: {},
  components: {
    info1,
    size1,
    list1,
  },
  data: function () {
    return {
      // 显示页面
      printShow: '1',
      // 查询
      searchFrom: {},
      // 列表
      fields: [
        { label: '企业名称', model: 'company' },
        { label: '企业类别', model: 'purpose' },
        { label: '法人姓名', model: 'legal_personal' },
        { label: '法人代表身份证号', model: 'legal_card' },
        { label: '备案证明编号', model: 'licence_num' },
        { label: '管辖单位', model: 'tube_company' },
        { label: '保安备案证明发证日期', model: 'cert_date' },
        { label: '打印日期', model: 'print_date' },
        { label: '证件状态', model: 'cert_status', format: (i) => (i == '0' ? '正常' : '注销') },
      ],
      opera: [
        { label: '详情', method: 'view', type: 'info' },
        { label: '打印', method: 'print' },
      ],
      list: [],
      total: 0,
      // 详情
      show: false,
      info: {},
      // 打印
      // 打印所选原数据列表
      selected: [],
      // 打印尺寸处理数据集
      printList: [],
      // 打印设置尺寸
      company_keep_size: {},
      setShow: false,
    };
  },
  created() {
    // 默认尺寸
    this.searchSize();
    this.search();
  },
  methods: {
    ...company_keep(['query', 'update']),
    ...company_keep_grant(['create']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchFrom);
      let res = await this.query({ skip, limit, status: '0', ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 详情
    toView({ data }) {
      this.$set(this, `info`, data);
      this.show = true;
    },
    // 关闭详情
    close() {
      this.show = false;
    },
    // 打印
    // 批量选择打印证书数据
    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.year = moment(val.cert_date).format('yyyy');
        val.month = moment(val.cert_date).format('MM');
        val.day = moment(val.cert_date).format('DD');
      }
      this.$set(this, `printList`, data);
      this.printShow = '2';
    },
    // 预览证书打印
    previewPrint() {
      this.$print(this.$refs.print);
    },
    async certPrint() {
      this.$confirm(`证书一旦打印不可更改,请谨慎操作！`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          let selected = this.selected;
          for (const data of selected) {
            // 修改证表信息
            this.updateKeep(data);
            // 创建发放表数据
            this.createGrant(data);
            this.$print(this.$refs.print);
          }
        })
        .catch(() => {});
    },
    // 修改证表数据
    async updateKeep(data) {
      data.status = '1';
      data.print_personal = this.user.department_name;
      data.print_date = moment(new Date()).format('yyyy-MM-DD');
      let p1 = await this.update(data);
      if (this.$checkRes(p1));
    },
    // 创建发放表数据
    async createGrant(data) {
      let p1 = {
        company_id: data.company_id,
        company_keep_id: data.id,
        company: data.company,
        tube_company: data.tube_company,
        purpose: data.purpose,
        legal_personal: data.legal_personal,
        legal_card: data.legal_card,
        addres: data.addres,
        baoan_sevicetype: data.baoan_sevicetype,
        register_money: data.register_money,
        approve_date: data.approve_date,
        licence_num: data.licence_num,
        approval_num: data.approval_num,
        cert_date: data.cert_date,
        effective_date: data.effective_date,
        cert_status: data.cert_status,
        print_personal: this.user.department_name,
        print_date: moment(new Date()).format('yyyy-MM-DD'),
      };
      let p2 = await this.create(p1);
      if (this.$checkRes(p2)) {
        this.$message({ type: `success`, message: `打印成功` });
      } else {
        this.$message({ type: `error`, message: `${p2.errmsg}` });
      }
    },
    // 设置尺寸
    setSize() {
      this.setShow = true;
    },
    // 提交设置的尺寸
    setSubmit({ data }) {
      localStorage.setItem('company_keep_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('company_keep_size'));
      if (data) {
        this.$set(this, `company_keep_size`, data);
      } else {
        let data = {
          p0: { width: 1300, height: 894 },
          p1: { top: 180, right: 0, bottom: 0, left: 960 },
          p2: { top: 265, right: 0, bottom: 0, left: 328 },
          p3: { top: 325, right: 0, bottom: 0, left: 328 },
          p4: { top: 385, right: 0, bottom: 0, left: 328 },
          p5: { top: 445, right: 0, bottom: 0, left: 328 },
          p6: { top: 500, right: 0, bottom: 0, left: 328 },
          p7: { top: 560, right: 0, bottom: 0, left: 328 },
          p8: { top: 625, right: 0, bottom: 0, left: 165 },
          p9: { top: 675, right: 0, bottom: 0, left: 800 },
        };
        localStorage.setItem('company_keep_size', JSON.stringify(data));
        this.$set(this, `company_keep_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;
        }
      }
    }
  }
}
/deep/.el-dialog__body {
  padding: 10px;
}
</style>
