<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: "MonthByChargePrint",
    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_operate_date","f_count_open", "f_pregas_open", "f_collection_open",
                        "f_count_zg","f_pregas_zg", "f_collection_zg",
                        "f_count_bk", "f_collection_bk",
                        "f_count_bq", "f_pregas_bq","f_collection_bq",
                        "f_count_hb", "f_collection_hb",
                        "f_count", "f_pregas", "f_collection"]

          this.fields = {
            "f_operate_date":"时间",
            "f_count_open":"开户人次","f_pregas_open":"开户气量","f_collection_open":"开户金额",
            "f_count_zg":"再购人次", "f_pregas_zg":"再购气量","f_collection_zg":"再购金额",
            "f_count_bk":"补卡人次", "f_collection_bk":"补卡金额",
            "f_count_bq":"补气人次", "f_pregas_bq":"补气气量","f_collection_bq":"补气金额",
            "f_count_hb":"换表人次", "f_collection_hb":"换表金额",
            "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}${this.depname ? this.depname : ''}统计报表   ${this.model.model.f_gasproperties ? this.model.model.f_gasproperties : ''}
          </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>
      `

        this.thead += '<tr>';
        for (let key of this.modelval) {
          if (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 =  2
        lodop.SET_PRINT_PAGESIZE(isHeng, 0, 0, "A4");

        // 定义样式
        let style;
        if (isHeng == 2) {//横向打印
          style = '' +
            '<style>table {border-collapse: collapse;font-size:2.8mm} ' +
            '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: 18.48mm}</style>'
        } else {
          // 定义样式
          style = '<style>table {border-collapse: collapse;font-size:2.2mm}' +
            'table th {border-collapse: collapse;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} ' +
            '.bz {border-top: 0;width: 21.6mm} ' +
            '.other {border-top: 0;width: 14.4mm}</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 (field === 'f_comments' && this.model.rows[j][field]) {
                  tHtml += '<td class="comments">' +
                    this.model.rows[j][field].substring(0, 10)
                    + '</td>'
                } else {
                  tHtml += '<td>' + this.model.rows[j][field] + '</td>'
                }
              }
            }
            tHtml += '</tr>'
          }
        }
        this.tfoot = `
        <tr>
          <td class="defaulttd" colspan='1' >
            合计
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="left">
            <font color="#0000FF"  tindex="2">###人</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="right">
            <font color="#0000FF" tindex="3">###方</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="left">
            <font color="#0000FF" tindex="4">###元</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="right">
            <font color="#0000FF" tindex="5">###人</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="left">
            <font color="#0000FF"  tindex="6">###方</font>
          </td>
           <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="right">
            <font color="#0000FF" tindex="7">###元</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="left">
            <font color="#0000FF" tindex="8">###人</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="right">
            <font color="#0000FF" tindex="9">###元</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="left">
            <font color="#0000FF"  tindex="10">###人</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="right">
            <font color="#0000FF" tindex="11">###方</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="left">
            <font color="#0000FF" tindex="12">###元</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="right">
            <font color="#0000FF" tindex="13">###人</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="left">
            <font color="#0000FF"  tindex="14">###元</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="right">
            <font color="#0000FF" tindex="15">###人</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="left">
            <font color="#0000FF" tindex="16">###方</font>
          </td>
          <td class="defaulttd" colspan='1'  tdata="allSum" format="#,##0.00" align="left">
            <font color="#0000FF" tindex="17">###元</font>
          </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, "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>
