<template>
  <div class="auto form-horizontal" >
    <modal :show.sync="show"  v-ref:modal large backdrop="false">
      <header slot="modal-header" class="modal-header">
        <h3 style="color:black" class="modal-title">收费发票</h3>
      </header>
      <article slot="modal-body" style = "display: flex;justify-content: center;" id="bill">
        <div v-if="bill.data">
          {{{bill.data}}}
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <div v-if="billConfig.hasBillManage">
          <div class="col-sm-2">
            <span><strong>发票号:{{model.f_using_number}}</strong></span>
          </div>
        </div>
        <button type="button" class="btn btn-success" @click='print()'>打印</button>
        <report-print id='bill' top='15mm' left='5mm'  width='90%'  height='70%' :notrepeat="true" :showbtn="false" v-ref:reportprint></report-print>
        <button type="button" class="btn btn-default" @click='cancel()' v-if="!billManager">取消</button>
      </footer>
    </modal>
  </div>
</template>
<script>

  import { DataModel } from 'vue-client'

  let getBillData = async function(self, val) {
    console.log('看看打票里面的data', self.data, self.billData, val)
    self.bill = new DataModel(self.billData.url, {reprint: "'冲正'"})
    await self.bill.search(self.data.id)

    self.model.f_using_number =  self.billData.bill.number
  }

  export default {
    title: '收费发票',
    data () {
      return {
        model: {
          f_using_number: null
        },
        bill: {},
        messShow: false
      }
    },
    props: ['show', 'data', 'billData', 'billConfig'],
    ready () {
      console.log('打票', this.billConfig, this.billData)
    },
    methods: {
      cancel  () {
        this.$dispatch('toggle')
      },

      async print () {
        // 更新系统发票号
        try{
          console.log('开具冲红收据的参数：', this.data.id, this.data.f_type)
          if (this.billConfig.hasBillManage) {
            await this.$CommonService.correctPaperBill(this.data.id, this.data.f_type,  this.data.f_use_type, this.billData.bill, 'newBill')
          }
          console.log('系统票号更新完毕')
          this.$refs.reportprint.print()
        } catch (error) {
          this.$dispatch('refresh')
        }

      }
    },
    watch: {
      'show' (val) {
        if (val) {
          getBillData(this, val).then(() => {
            this.$emit('ready')
          }).catch((error) => {
            this.$emit('error', error)
          })
        }
      }
    },
    events: {
      'print' () {
        // 对票据明细进行更新或插入操作
        this.$dispatch('printok', '发票打印', this.data)
      },
      'print-error' () {
        this.$dispatch('error', '发票打印', this.data, null)
      }
    }
  }

</script>
<style >
</style>
