<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="0" value="正常"></el-option>
                        <el-option label="1" value="注销"></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="morePrint()">正本批量打印</el-button>
                    <el-button type="primary" size="mini" @click="copymorePrint()">副本批量打印</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"
              @view="toView"
              @print="toPrint"
              @copyprint="toCopyprint"
              :select="true"
              :selected="selected"
              @handleSelect="handleSelect"
              @complete="tocomplete"
            >
            </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_cert_size="company_cert_size"></list-1>
            </el-col>
          </el-col>
        </el-col>
        <el-col :span="24" v-else-if="printShow == '3'">
          <el-col :span="24" class="thr">
            <el-button type="primary" size="mini" @click="copysetSize()">设置尺寸</el-button>
            <el-button type="primary" size="mini" @click="copycertPrint()">打印证书</el-button>
            <el-button type="primary" size="mini" @click="copypreviewPrint()">预览打印</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-2 :list="printList" :copy_company_cert_size="copy_company_cert_size"></list-2>
            </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_cert_size="company_cert_size" @close="setClose" @onSubmit="setSubmit"></size-1>
    </el-dialog>
    <el-dialog class="twodialog" title="副本证书设置尺寸" :visible.sync="copysetShow" width="50%" :before-close="copysetClose" :close-on-click-modal="false">
      <size-2 :copy_company_cert_size="copy_company_cert_size" @close="copysetClose" @onSubmit="copysetSubmit"></size-2>
    </el-dialog>
  </div>
</template>

<script>
import info1 from './parts/info-1.vue';
import list1 from './parts/list-1.vue';
import size1 from './parts/size-1.vue';
import list2 from './parts/list-2.vue';
import size2 from './parts/copy-size.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: company_licence } = createNamespacedHelpers('company_licence');
const { mapActions: company_licence_grant } = createNamespacedHelpers('company_licence_grant');
const moment = require('moment');
export default {
  name: 'index',
  props: {},
  components: {
    info1,
    list1,
    size1,
    list2,
    size2,
  },
  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' },
        { label: '打印', method: 'print' },
        { label: '副本打印', method: 'copyprint' },
        { label: '完成', method: 'complete' },
      ],
      list: [],
      total: 0,
      // 详情
      show: false,
      info: {},
      // 打印
      // 打印所选原数据列表
      selected: [],
      // 打印尺寸处理数据集
      printList: [],
      // 打印设置尺寸
      company_cert_size: {},
      setShow: false,
      // 副本设置尺寸
      copy_company_cert_size: {},
      copysetShow: false,
    };
  },
  created() {
    // 默认尺寸
    this.searchSize();
    this.searchcopySize();
    this.search();
  },
  methods: {
    ...company_licence(['query', 'update']),
    ...company_licence_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';
    },
    // 证书打印
    async certPrint() {
      this.$print(this.$refs.print);
    },
    // 预览证书打印
    previewPrint() {
      this.$print(this.$refs.print);
    },
    // 设置尺寸
    setSize() {
      this.setShow = true;
    },
    // 提交设置的尺寸
    setSubmit({ data }) {
      localStorage.setItem('company_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('company_cert_size'));
      if (data) {
        this.$set(this, `company_cert_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_cert_size', JSON.stringify(data));
        this.$set(this, `company_cert_size`, data);
      }
    },
    // 副本打印
    // 单个副本打印
    toCopyprint({ data }) {
      this.selected.push(data);
      this.copymorePrint();
    },
    // 副本打印跳转
    copymorePrint() {
      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 = '3';
    },
    // 副本证书打印
    copycertPrint() {
      this.$print(this.$refs.print);
    },
    // 预览打印
    copypreviewPrint() {
      this.$print(this.$refs.print);
    },
    // 设置尺寸
    copysetSize() {
      this.copysetShow = true;
    },
    // 提交设置
    copysetSubmit(data) {
      localStorage.setItem('copy_company_cert_size', JSON.stringify(data));
      this.searchcopySize();
      this.copysetClose();
    },
    // 关闭设置副本尺寸
    copysetClose() {
      this.copysetShow = false;
    },
    // 查詢默认副本尺寸
    searchcopySize() {
      let data = JSON.parse(localStorage.getItem('copy_company_cert_size'));
      if (data) {
        this.$set(this, `copy_company_cert_size`, data);
      } else {
        let data = {
          p0: { width: 1040, height: 715 },
          p1: { top: 160, right: 0, bottom: 0, left: 315 },
          p2: { top: 247, right: 0, bottom: 0, left: 176 },
          p3: { top: 290, right: 0, bottom: 0, left: 176 },
          p4: { top: 380, right: 0, bottom: 0, left: 176 },
          p5: { top: 430, right: 0, bottom: 0, left: 176 },
          p6: { top: 517, right: 0, bottom: 0, left: 176 },
          p7: { top: 555, right: 0, bottom: 0, left: 176 },
          p8: { top: 653, right: 0, bottom: 0, left: 284 },
        };
        localStorage.setItem('copy_company_cert_size', JSON.stringify(data));
        this.$set(this, `copy_company_cert_size`, data);
      }
    },

    // 打印完成
    tocomplete({ data }) {
      this.$confirm(`您确认提交完成吗！`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          // 修改证表信息
          this.updateLicence(data);
          // 创建发放表数据
          this.createGrant(data);
          this.search();
        })
        .catch(() => {});
    },
    // 修改证表信息
    async updateLicence(data) {
      data.status = '1';
      data.print_personal = this.user.department_name;
      data.print_date = moment(new Date()).format('yyyy-MM-DD');
      let res = await this.update(data);
      if (this.$checkRes(res));
    },
    // 创建发放表数据
    async createGrant(data) {
      let p1 = {
        company_id: data.company_id,
        company_licence_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 res = await this.create(p1);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `操作成功` });
      } else {
        this.$message({ type: `fail`, message: `${res.errmsg}` });
      }
    },
  },
  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;
        }
      }
    }
  }
  .thr {
    margin: 0 0 10px 0;
  }
}
/deep/.el-dialog__body {
  padding: 10px;
}
.twodialog {
  /deep/.el-dialog__body {
    height: 500px;
    overflow-y: auto;
  }
}
</style>
