<template>
  <button class="button_export button_spacing" @click="stamp()">打印</button>
</template>

<script>
  import getLodop from '../../../../../plugins/LodopFuncs'
  import {PagedList} from "vue-client";

  export default {
    title: '月报表打印',
    props: {
      countmodel:{
        type:Object,
        default:{}
      }
    },
    data() {
      return {
        modelval: [],
        fields: {},
        thead: '',
        tfoot: '',
        model:{},
        sumsmodel:{},
        orgname: ''
      }
    },
    methods: {
      stamp() {
        this.model = this.countmodel.model,
          this.sumsmodel = this.countmodel.sumsmodel,
          this.orgname = this.countmodel.orgname,
        //默认选择要打印的列
        this.modelval = [
          "f_operate_date",
          "f_count",
          "f_pregas",
          "f_collection"
        ];
        this.fields = {
          "f_operate_date": "购买日期",
          "f_count": "人次",
          "f_pregas":"气量",
          "f_collection":"金额"
        };
        this.put();
        this.print();
      },
      put() {

        let statisticalMonth = ''

        if (this.model.model.f_begin_operate_date && this.model.model.f_end_operate_date) {
          statisticalMonth = this.model.model.f_end_operate_date.substring(0, 7)
        } else {
          statisticalMonth = this.model.model.f_operate_date
        }

        this.thead = `
        <tr>
          <th class="biaoti" colspan=${this.modelval.length} style="font-size: 6mm">
              ${this.orgname}悦生活月统计报表
          </th>
        </tr>
        <tr>
          <th class="biaoti" colspan=${this.modelval.length}>统计月份: ${statisticalMonth}</th></tr>
        <tr>
        <tr>
          <th class="biaoti" colspan=${this.modelval.length}>制单人：${this.$login.f.name} 打印时间: ${this.$login.toStandardTimeString()}</th></tr>

        </tr>`

        this.thead += '<tr>';
        for (let key of this.modelval) {
          if (this.fields[key] == '购买日期') {
            this.thead += '<th class="bz">' + this.fields[key] + '</th>'
          }  else {
            this.thead += '<th class="other">' + this.fields[key] + '</th>'
          }
        }
        this.thead += '</tr>'
      },
      async print() {
        // 开始打印
        let lodop = getLodop()
        lodop.PRINT_INIT('打印')

        // 是否横向打印
        let isHeng =  1
        lodop.SET_PRINT_PAGESIZE(1, 0, 0, "A4");

        // 定义样式
        let style = '<style>table {border-collapse: collapse;font-size:2.8mm}' +
          'table th {border-collapse: collapse;text-align: center;width: 180mm;border: 0.2mm solid black;}' +
          'table td {border-collapse: collapse;text-align: center;border: 0.2mm solid black} ' +
          'table span {width: 25mm;text-align:left;display:inline-block;}' +
          '.comments {text-overflow: ellipsis;overflow: hidden} ' +
          '.khxm {border-top: 0;width: 14.4mm;text-align: center;} ' +
          '.bz {border-top: 0;width: 21.6mm;text-align: center;} ' +
          '.name {border-top: 0;width: 14.4 mm} ' +
          '.other {border-top: 0;width: 10mm}</style>'


        let tHtml = style + '<table><thead>' + this.thead + '</thead><tbody>'
        for (var i = 1; i <= this.model.totalPage; i++) {
          await this.model.loadPage(i)
          for (let j = 0; j < this.model.rows.length; j++) {
            tHtml += '<tr>'
            for (let field of this.modelval) {
              if (field && this.model.rows[j]) {
                if(this.model.rows[j][field]==null){
                  this.model.rows[j][field]=''
                }
                if (field === 'f_operate_date'&& this.model.rows[j][field]) {
                  tHtml += '<td class="bz">' +
                    this.model.rows[j][field]
                    + '</td>'
                } else {
                  tHtml += '<td>' + this.model.rows[j][field] + '</td>'
                }
              }
            }
            tHtml += '</tr>'
          }
        }
        this.tfoot = ``

        // 如果有表尾，则添加表尾，如果内容翻页的话，表尾每一页都要打印
        tHtml += `</tbody><tfoot>${this.tfoot}</tfoot></table>`
        //ADD_PRINT_TABLE(intTop,intLeft,intWidth,intHeight,strHtml)增加表格项
        lodop.ADD_PRINT_TABLE(0, 0, "200mm", 'BottomMargin:' + 30, tHtml);//增加表格项
        lodop.SET_PRINT_STYLEA(0, "Horient", 2);//打印项在纸张中水平居中

        // 设置页脚
        lodop.ADD_PRINT_HTM("96%", "45%", 100, 50, `<font color='#0000ff' format='##'><span tdata='pageNO'>第##页</span>/<span tdata='pageCount'>共##页</span></font>`);//增加超文本项
        lodop.SET_PRINT_STYLEA(0, "ItemType", 1);
        lodop.SET_PRINT_STYLEA(0, "Horient", 2);

        // 设置打印方式 （横向|纵向）
        lodop.SET_SHOW_MODE("LANDSCAPE_DEFROTATED", 1);
        // 打印维护
        // 使用打印预览是为了方便调整打印格式。例如：横向打印
        lodop.PREVIEW()
      },
    }


  }
</script>

<style scoped>

</style>
