<template>
  <div class="auto">
    <modal :show.sync="show" v-ref:modal large backdrop="false" style="overflow-y: auto !important;">
      <header slot="modal-header" class="modal-header" style="text-align: center;">
        <h4 class="modal-title">{{data.type}}票据明细</h4>
      </header>
      <article slot="modal-body" class="modal-body">
        <validator name='v'>
          <form class="form-horizontal  select-overspread">
            <div class="row" style="display: flex;justify-content: center;" id='reissue-bill' v-show="false">
              {{{bill.data}}}
            </div>
            <div class="row" style="display: flex;justify-content: center;" >
              <p>{{messText}}</p>
            </div>
          </form>
        </validator>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <!--<button type="button" class="btn btn-success" @click='print(true)' :disabled="!$v.valid">选择打印机打印</button>-->
        <button type="button" class="btn btn-success" @click='print()' :disabled="!$v.valid">打印</button>
        <report-print id='reissue-bill' top='5mm' left='5mm'  width='90%'  height='80%' :notrepeat="false" :showbtn="false" v-ref:reportprint></report-print>
        <button class="btn btn-default" @click="clean()">取消</button>
      </footer>
    </modal>
  </div>
</template>
<script>
  export default {
    title: '票据明细打印',
    data () {
      return {
        messText: '',
        bill: {data: ''},
        sellingData: [],
        timeOut: this.$appdata.getSingleValue('打印票据间隔时间') ? this.$appdata.getSingleValue('打印票据间隔时间') : 1000,
        printControl: null
      }
    },
    props: ['show', 'data'],
    ready () {
      this.readyGen()
    },
    methods: {
      async readyGen () {
        let res = await this.$resetpost('rs/sql/querySingleTable', {
          data: {
            items: 'id',
            tablename: 't_sellinggas',
            condition: ` f_batch_id = '${this.data.id}' `
          }
        }, {resolveMsg: null, rejectMsg: '查询缴费信息失败'})
        this.sellingData = res.data
        this.messText = `共有${res.data.length}条明细票据, 是否打印?`
      },
      clean () {
        this.$dispatch('cancel', '票据补打', this.row)
      },
      async print (select) {
        // 进行补打
        try {
          for (let row of this.sellingData) {
            let bill = await this.$resetpost(this.data.billUrl, {data: {condition: row.id, operator: `'${this.data.f_operator}'`}}, {resolveMsg: null})
            this.bill.data = bill.data
            this.$nextTick(function () {
              setTimeout(function () {
                this.asyncPrint(select)
              }, parseInt(this.timeOut)) // 延迟时间从this.timeOut中获取，需要确保timeOut是一个数字值
            })
            this.printControl = null
          }
          this.$dispatch('reissue-success')
        } catch (error) {
          this.$dispatch('refresh')
        }
      },
      async asyncPrint (select) {
        return new Promise(async (resolve) => {
          this.printControl = resolve
          this.$refs.reportprint.print(select)
        })
      }
    },
    events: {
      'print' () {
        this.printControl('打印完成')
      },
      'print-error' () {
        this.$showAlert('打印失败，若有需要，请补打发票', 'error', 3)
        this.$dispatch('error', '票据补打', this.data, null)
      }
    }
  }
</script>

<style>
</style>
