<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" v-if="type === '票据补打'">{{ data[0].f_type }}票据明细</h4>
        <h4 class="modal-title" v-else>{{ 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-if="type === '票据补打'">
              {{{bill2.data}}}
            </div>
            <div class="row" style="display: flex;justify-content: center;" id='reissue-bill' v-show="false">
              {{{bill.data}}}
            </div>
          </form>
        </validator>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button type="button" class="btn btn-success" @click='print()' >打印</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>
import {DataModel} from "vue-client";

let getBillData = async function (self) {
  console.log('看看执行了吗？')
  // 显示票据
  let billurl = await self.getBillUrl(self.data[0].f_type)
  self.bill2 = new DataModel(billurl, {reprint: "'补打'"})
  await self.bill2.search(self.data[0].id)
}

export default {
  title: '批量打印票据',
  data() {
    return {
      messText: '',
      bill: {data: ''},
      bill2: Object,
      sellingData: [],
      timeOut: this.$appdata.getSingleValue('打印票据间隔时间') ? this.$appdata.getSingleValue('打印票据间隔时间') : 1000,
      printControl: null
    }
  },
  props: ['show', 'data', 'type'],
  ready() {
    this.$nextTick(function () {
      if (this.type === '票据补打') {
        getBillData(this)
      }
    })
  },
  methods: {
    clean() {
      this.$dispatch('print-cancel', '票据补打', this.row)
    },
    async print (select) {
      console.log('select', select)
      let self = this
      // 进行补打
      try {
        if (self.type === '票据补打') {
          self.$refs.reportprint.print()
        } else {
          console.log('走的批量补打')
          for (let row of self.data) {
            let billUrl = await self.getBillUrl(row.f_type)
            let bill = await self.$resetpost(billUrl, {
              data: {
                condition: row.id,
                reprint: "'补打'"
              }
            }, {resolveMsg: null})
            self.bill.data = bill.data
            self.$nextTick(function () {
              setTimeout(function () {
                self.asyncPrint(select)
              }, parseInt(self.timeOut))
            })
            self.printControl = null
          }
          self.$dispatch('reissue-success')
        }
      } catch (error) {
        self.$dispatch('refresh')
      }
    },
    getBillUrl (type) {
      let name = ''
      // 根据type数据 选择调用的Logic
      if (type === '过户') {
        name = 'api/af-revenue/report/transfer_bill'
      } else if (type === '换表') {
        name = 'api/af-revenue/report/change_meter'
      } else if (type === '机表收费') {
        name = 'api/af-revenue/report/machine_bill'
      } else if (type === '物联网收费' || type === '物联网开户' || type === '物联网赠气') {
        name = 'api/af-revenue/report/iot_bill'
      } else if (type === '物联网补费' || type === '物联网扣费' || type === '物联网补气' || type === '物联网扣气' || type === '机表补费' || type === '机表扣费') {
        name = 'api/af-revenue/report/MakeUp'
      } else if (type === '超用收费') {
        name = 'api/af-revenue/report/overuse_bill'
      } else if (type === '其他收费') {
        name = 'api/af-revenue/report/otherCharge_bill'
      } else if (type === '补卡') {
        name = 'api/af-revenue/report/replace_sell'
      } else if (type === '发卡售气') {
        name = 'api/af-revenue/report/sendCard_bill'
      } else if (type === '卡表收费' || type === '卡表赠气' || type === '卡表收费撤销') {
        name = 'api/af-revenue/report/card_bill'
      } else if (type === '预存缴费') {
        name = 'api/af-revenue/report/pre_sell'
      } else if (type === '退费') {
        name = 'api/af-revenue/report/refund_sell'
      } else if (type === '调价补费') {
        name = 'api/af-revenue/report/compensation_bill'
      } else if (type === '数码表收费') {
        name = 'api/af-revenue/report/digtial_bill'
      }
      return name
    },
    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>
