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

<script>
  import getLodop from '../../../../plugins/LodopFuncs'

  export default {
    name: "DayReportListPrint",
    title: '缴费通知单打印',
    props: {
      countmodel:{
        type:Object,
        default:{}
      },
      modelval:[],
      fields: {}
    },
    data() {
      return {
        thead: '',
        tfoot: '',
        model:{},
        sumsmodel:{},
        orgname: '',
        depname: '0'
      }
    },
    methods: {
      stamp() {
        this.model = this.countmodel.model
        console.log(this.model)
        this.put()
        this.print()
      },
      put() {
        console.log(this.modelval,this.fields)
        this.thead = `
        <tr>
          <th class="biaoti" colspan=${this.modelval.length} style="font-size: 6mm">
              运城市经纬燃气缴费通知单
          </th>
        </tr>
        <tr>
          <th class="biaoti"  style="text-align: left;border-bottom: 0.2mm solid black"colspan=${this.modelval.length}>打印日期：${this.model.model.startDate}</th></tr>
        `

        this.thead += '<tr>';
        for (let key of this.modelval) {
          if (this.fields[key] == '本次抄码时间'|| this.fields[key] == '上次抄码时间' || 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(isHeng, 0, 0, "A4");

        // 定义样式
        let style;
        if (isHeng == 2) {//横向打印
          style = '' +
            '<style>table {border-collapse: collapse;font-size:2.5mm} ' +
            'table th {border-collapse: collapse;width: 264mm;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: 36mm;text-align: left;display:inline-block}' +
            '.comments {text-overflow: ellipsis;overflow: hidden} ' +
            '.khxm {border-top: 0;width: 44.88mm} ' +
            '.bz {border-top: 0;width: 34.32mm} ' +
            '.other {border-top: 0;width: 25.48mm}</style>'
        } else {
          // 定义样式
          style = '<style>table {border-collapse: collapse;font-size:2.2mm}' +
            '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} ' +
            '.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: 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 if(field === 'f_user_name' || field === 'f_payment' && 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>'
          }
        }
        tHtml += `<tr><td style="text-align: center;">
          </td>
          <td style="text-align: center;">
          <nobr>合计：</nobr>
        </td>
        <td style="text-align: center;">
          </td>
          <td style="text-align: center;">
          </td>
          <td style="text-align: center;">
          </td>
          <td style="text-align: center;">
          </td>
          <td style="text-align: center;">
          </td>
          <td style="text-align: center;">
          </td>
          <td style="text-align: center;">
          </td>
          <td style="text-align: center;">
          </td>
          <td style="text-align: center;">
          </td>
          <td style="text-align: center;">
          <nobr>${this.model.sums.f_oughtfee}</nobr>
        </td>
        <td style="text-align: center;">
          <nobr>${this.model.sums.f_oughtamount}</nobr>
        </td>
        <td style="text-align: center;">
          <nobr>${this.model.sums.f_balance}</nobr>
        </td>
        <td style="text-align: center;">
          <nobr>${this.model.sums.yinshou}</nobr>
        </td>
        <td style="text-align: center;">
          <nobr>${this.model.sums.f_curbalance}</nobr>
        </td>
        <td style="text-align: center;">
          </td>
          <td style="text-align: center;">
          <nobr>${this.model.sums.lswjd}</nobr>
        </td></tr>`

        // 如果有表尾，则添加表尾，如果内容翻页的话，表尾每一页都要打印
        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, "180mm", '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>
