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

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

  export default {
    name: "DayReportListPrint",
    title: '日报表打印',
    props: {
      countmodel:{
        type:Object,
        default:{}
      }
    },
    data() {
      return {
        modelval: [],
        fields: {},
        thead: '',
        tfoot: '',
        model:{},
        sumsmodel:{},
        orgname: '',
        depname: '0'
      }
    },
    methods: {
      stamp() {
         this.model = this.countmodel.model,
          this.sumsmodel = this.countmodel.sumsmodel,
          this.orgname = this.countmodel.orgname,
          this.depname = this.countmodel.depname
        // 验证，必须选择用气性质
        //默认选择要打印的列
        this.modelval = [
          "f_userinfo_code",
          "f_user_name",
          "f_pregas",
          "f_collection",
          "f_operate_date",
          "f_stair1price",
          "f_stair1amount",
          "f_stair2price",
          "f_stair2amount",
          "f_stair3price",
          "f_stair3amount",
          "f_payment",
          "f_pay_id",
          "f_terminal_num"
        ];
        this.fields = {
          "f_userinfo_code": "客户编号",
          "f_user_name": "客户姓名",
          "f_pregas":"气量",
          "f_collection":"金额",
          "f_operate_date": "写卡时间",
          "f_stair1amount": "1气量",
          "f_stair1price": "1单价",
          "f_stair2amount": "2气量",
          "f_stair2price": "2单价",
          "f_stair3amount": "3气量",
          "f_stair3price": "3单价",
          "f_payment": "付款方式",
          "f_pay_id":"交易号",
          "f_terminal_num":"服务机"
        };
        this.put();
        this.print();
      },
      put() {
        this.thead = `
        <tr>
          <th class="biaoti" colspan=${this.modelval.length} style="font-size: 6mm">
              ${this.orgname}${this.depname ? this.depname : ''}悦生活日统计报表
          </th>
        </tr>
        <tr>
          <th class="biaoti" colspan=${this.modelval.length}>统计日期：${this.model.model.f_startDate} 00:00:00 ~ ${this.model.model.f_endDate} 23:59:59</th></tr>
        <tr>
        <tr>
          <th class="biaoti" colspan=${this.modelval.length}>制单人：${this.$login.f.name} 打印时间: ${this.$login.toStandardTimeString()}</th></tr>
        <tr style="text-align: center;font-family: 微软雅黑">
          <th colspan=${this.modelval.length}>
            <span>业务人次: ${this.sumsmodel.f_sum_count}</span>
            <span>业务气量: ${this.sumsmodel.f_shouru_gas}</span>
            <span>业务金额: ${this.sumsmodel.f_shouru_fee}</span>
          </th>
        </tr>
        </tr>`

        this.thead += '<tr>';
        for (let key of this.modelval) {
          if (this.fields[key] == '客户姓名' || this.fields[key] == '付款方式' ) {
            this.thead += '<th class="khxm">' + this.fields[key] + '</th>'
          } else 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;white-space: nowrap;text-align: center;width: 180mm;border: 0.2mm solid black;}' +
          'table td {border-collapse: collapse;text-align: center;border: 0.2mm solid black} ' +
          '.biaoti {border-color: transparent} ' +
          '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: 12mm}</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 if(field === 'f_pay_id' && this.model.rows[j][field] ){
                  tHtml += '<td class="name">' +
                    this.model.rows[j][field].substring(0, 10)
                    + '</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)增加表格项
        if (isHeng == 2) {//横向打印
          lodop.ADD_PRINT_TABLE(0, 0, "264mm", 'BottomMargin:' + 30, tHtml);//增加表格项
        } else {
          lodop.ADD_PRINT_TABLE(0, 0, "200mm", 'BottomMargin:' + 30, tHtml);//增加表格项
        }
        lodop.SET_PRINT_STYLEA(0, "Horient", 2);//打印项在纸张中水平居中
        // lodop.SET_PRINT_STYLEA(0,"Vorient",2);//打印项在纸张中垂直居中
        // lodop.SET_PRINT_MODE('PRINT_PAGE_PERCENT', 'Full-width')

        // 设置页脚
        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_PRINT_MODE('PRINT_PAGE_PERCENT', 'Full-width')
        // 设置打印方式 （横向|纵向）
        // lodop.SET_SHOW_MODE("LANDSCAPE_DEFROTATED", 1);
        // 打印维护
        // lodop.PRINT_SETUP()
        // 使用打印预览是为了方便调整打印格式。例如：横向打印
        lodop.PREVIEW()
      },
    }


  }
</script>

<style scoped>

</style>
