<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: "MonthByUserPrint",
    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_open_my","f_count_my","f_pregas_my","f_collection_my",
          "f_open_sy","f_count_sy","f_pregas_sy",
          "f_collection_sy","f_open_gy","f_count_gy","f_pregas_gy",
          "f_collection_gy","f_open","f_count","f_pregas","f_collection"
        ];
        this.fields = {
          "f_operate_date":"时间",
          "f_open_my":"民用开户","f_count_my":"民用人次","f_pregas_my":"民用气量","f_collection_my":"民用金额",
          "f_open_sy":"商业开户","f_count_sy":"商业人次","f_pregas_sy":"商业气量", "f_collection_sy":"商业金额",
          "f_open_gy":"工业开户","f_count_gy":"工业人次","f_pregas_gy":"工业气量","f_collection_gy":"工业金额",
          "f_open":"合计开户","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.YeanMouthDate
        }
        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>
