<template>
  <button  class="btn btn-default" @click='PrintAsFile()' :id="'vc-printtable-'+attach">打印表格</button>
</template>

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

  let printGen = function * (self, pageNo) {

    console.log('开始打印', self.isSelected, self.model, self.tfoot)
    //是否分页打印
    if(pageNo){
      self.PrintSinglePage()
      return
    }
    if (self.isSelected) {
      // 判断是否选择行数据进行打印
      self.PrintSelectedPage()
      return
    }

    let lodop = getLodop()
    lodop.PRINT_INIT(self.printName+'打印')
    var style
    if (!self.style) {
      style = '<style>table{font-size:15px;border-width: 2px;border-collapse: collapse;border:solid 2px ; border-color:black;}' +
        'table th {border-width: 1px;padding: 2px;border-style: solid;}table td {border-width: 1px;padding: 2px;border-style: solid;}</style>'
    } else {
      style = self.style
    }
    let tHtml = style + '<table class="table table-hover"><thead>' + self.thead + '</thead><tbody>'

    // for (var i = 1; i <= self.model.totalPage; i++) {
    //   console.log('pageIndex = ' + i)
    //   yield self.model.loadPage(i)
      for (let row of self.model.rows) {
        tHtml += '<tr>'
        for (let field of self.fields) {
          if (field && (row[field] || row[field] === 0) ) {
            tHtml += '<td>' + row[field] + '</td>'
          } else {
            tHtml += '<td></td>'
          }
          // console.log(row[field])
        }
        tHtml += '</tr>'
      }
    // }
    // 加入合计数据 (最后一页显示)
    if (self.sumhtml) {
      tHtml += self.sumhtml
    }
    // 如果有表尾，则添加表尾，如果内容翻页的话，表尾每一页都要打印
    if (self.tfoot) {
      self.tfoot += `<tr><td width="100%" colspan=${self.fields.length} style="text-align: center;border-width: 0;">第<font tdata="PageNO" format="#" color="blue">#</font>页</span>/共<font tdata="PageCount" format="#" color="blue">##</font></span>页</td></tr>`
      tHtml += `</tbody><tfoot>${self.tfoot}</tfoot></table>`
    } else {
      tHtml += '</tbody></table>'
    }

    lodop.ADD_PRINT_TABLE(self.top, self.left, 'RightMargin:' + self.right, 'BottomMargin:' + self.bottom, tHtml)
    lodop.SET_PRINT_STYLEA(0, 'HOrient', 3)
    lodop.SET_PRINT_STYLEA(0, 'VOrient', 3)
    // 指定打印方向 0--可通过打印机选择纵向或横向，1--锁定为纵向打印，2---锁定为横向打印
    LODOP.SET_PRINT_PAGESIZE(self.xyprint, 0, 0, "A4");

    if(self.printview==null){
      // 打印维护
      lodop.PRINT_SETUP()
    }else if(self.printview){
      // 使用打印预览是为了方便调整打印格式。例如：横向打印
      lodop.PREVIEW()
    }else if(!self.printview){
      // 直接打印
      lodop.PRINT()
    }

  }

  export default {
    // props: ['model', 'thead', 'fields', 'top', 'left', 'right', 'bottom', 'tfoot'],
    props: {
      model: Object,
      thead: String,
      tfoot: String,
      sumhtml: String,
      fields: Array,
      //null 打印维护， true 打印预览 false 直接打印
      printview:{},
      // 指定打印方向 0--可通过打印机选择纵向或横向，1--锁定为纵向打印，2---锁定为横向打印
      xyprint:{
        default : 0
      },
      top: {},
      left: {},
      right: {},
      bottom: {},
      attach: {
        type: String,
        default: 'name'
      },
      style: {
        type: String
      },
      isSelected: {
        type:Boolean,
        default:false
      },
      //是否分页打印
      printpage:{
        type:Boolean,
        default:false
      },
      // 打印名称,在同一个名称下的打印可以统一风格(打印维护界面点击应用即可)
      printName: {
        type: String
      }
    },
    data(){
      return{
        //打印任务的JOB代码,用于获得打印状态信息及最终结果。
        P_ID:"",
        //第几次任务
        loop:0,
      }
    },
    methods: {
      PrintAsFile () {
        console.log(this.model)
        let gen = printGen(this,this.printpage)
        co(gen)
      },
      async PrintSinglePage(){
        let lodop = getLodop()
        lodop.PRINT_INIT('打印表格'+(this.loop+1))
        var style
        if (!this.style) {
          style = '<style>table{font-size:15px;border-width: 2px;border-collapse: collapse;border:solid 2px ; border-color:black;}' +
            'table th {border-width: 1px;padding: 2px;border-style: solid;}table td {border-width: 1px;padding: 2px;border-style: solid;}</style>'
        } else {
          style = this.style
        }
        let tHtml = style + '<table class="table table-hover"><thead>' + this.thead + '</thead><tbody>'

        //所有数据全部打印,打印任务结束
        if(this.loop+1>this.model.totalPage){
          this.printinit()
          console.log('打印结束')
          return
        }
        console.log('获取数据pageIndex = ' + (this.loop+1))
        await this.model.loadPage(this.loop+1)
        for (let row of this.model.rows) {
          tHtml += '<tr>'
          for (let field of this.fields) {
            if (field && (row[field] || row[field] === 0) ) {
              tHtml += '<td>' + row[field] + '</td>'
            } else {
              tHtml += '<td></td>'
            }
          }
          tHtml += '</tr>'
        }
        //插入页码
        let page = `<tr><th width="100%" colspan=${this.fields.length} style="border-width: 0;">当前是第<font format="ChineseNum" color="blue">${this.loop+1}</font>页/共<font format="ChineseNum" color="blue">${this.model.totalPage}</font>页`
        // 如果有表尾，则添加表尾，如果内容翻页的话，表尾每一页都要打印
        if (this.tfoot) {
          tHtml += `</tbody><tfoot>${this.tfoot}${page}</tfoot></table>`
        } else {
          tHtml += '</tbody></table>'
        }

        lodop.ADD_PRINT_TABLE(this.left, this.top, 'RightMargin:' + this.right, 'BottomMargin:' + this.bottom, tHtml)
        lodop.SET_PRINT_STYLEA(0, 'HOrient', 3)
        lodop.SET_PRINT_STYLEA(0, 'VOrient', 3)
        //设置打印机为默认打印机
        lodop.SET_PRINTER_INDEX(-1)

        this.P_ID=lodop.PRINT()
        if(this.P_ID){
          this.loop+=1
          console.log("正打印第"+this.loop+"个任务(JOB代码"+this.P_ID)
        }
        //延迟循环缓解压力
        await this.timeout(1500)
        this.PrintSinglePage()
      },

      async PrintSelectedPage () {
        console.log('选择列打印开始', this.model)

        if (this.model.rows.length > 100) {
          this.$showAlert('您选择的数据过大，请选择少于100条数据进行打印', 'warning', 3000)
          return
        }

        let lodop = getLodop()
        lodop.PRINT_INIT('打印表格'+(this.loop+1))
        var style
        if (!this.style) {
          style = '<style>table{font-size:15px;border-width: 2px;border-collapse: collapse;border:solid 2px ; border-color:black;}' +
            'table th {border-width: 1px;padding: 2px;border-style: solid;}table td {border-width: 1px;padding: 2px;border-style: solid;}</style>'
        } else {
          style = this.style
        }
        let tHtml = style + '<table class="table table-hover"><thead>' + this.thead + '</thead><tbody>'
        for (let row of this.model.rows) {
          tHtml += '<tr>'
          for (let field of this.fields) {
            tHtml += '<td style="text-align: center;">' + row[field] + '</td>'
          }
          tHtml += '</tr>'
        }
        // // 如果有表尾，则添加表尾，如果内容翻页的话，表尾每一页都要打印
        if (this.tfoot) {
          tHtml += `</tbody><tfoot>${this.tfoot}</tfoot></table>`
        } else {
          tHtml += '</tbody></table>'
        }
        console.log('选择列打印的html', tHtml)
        lodop.ADD_PRINT_TABLE(this.left, this.top, 'RightMargin:' + this.right, 'BottomMargin:' + this.bottom, tHtml)
        lodop.SET_PRINT_STYLEA(0, 'HOrient', 3)
        lodop.SET_PRINT_STYLEA(0, 'VOrient', 3)
        //设置打印机为默认打印机
        lodop.SET_PRINTER_INDEX(-1)
        console.log('开始调用打印。。')
        this.P_ID=lodop.PRINT()
      },
      printinit(){
        this.P_ID="",
          this.loop=0
      },
      async timeout(ms){
        return new Promise((resolve, reject) => {
          setTimeout(resolve, ms)
        })
      }
    }
  }
</script>
