<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" class="modal-body">
        <validator name='v'>
          <form class="form-horizontal  select-overspread">
            <div class="row" style="display: flex;justify-content: center;" id='normal-bill' v-if="bill.data">
              {{{bill.data}}}
            </div>
          </form>
        </validator>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <p>将打印在第<input type="number" v-model="data.f_zzhh"/>行</p>
        <button type="button" class="btn btn-success" @click='printmai()'>打印</button>
        <report-print id='normal-bill' :top='top' left='0mm'  width='90%'  height='80%' :notrepeat="true" :showbtn="false" v-ref:print></report-print>
        <button type="button" class="btn btn-default"  @click='cancel()'>取消</button>
      </footer>
    </modal>
  </div>
</template>
<script>
  import { DataModel } from 'vue-client'

  let getBillData = async function (self) {
    console.log('asdfasf', self.data.f_zzhh)
    self.data.content.f_zzhh = `'${self.data.f_zzhh}'`
    self.bill = new DataModel(self.url, self.data.content)
    console.log(self.bill.url)
    let height = self.zzheight - 0.0
    let spacing = self.zzspacing - 0.0
    let pageRow = self.zzpageRow - 0
    let marginTop = self.zzmarginTop - 0
    var hh = self.data.f_zzhh
    if (hh != -1) {
      var top = marginTop + (hh - 1) * height
      if (hh > pageRow) { top = top + spacing }
      self.top = `${top}mm`
    }
    await self.bill.search()
  }

  export default {
    title: '收费发票',
    data () {
      return {
        top: '5mm',
        bill: {},
        zzheight: this.$appdata.getSingleValue('折子行高') ? this.$appdata.getSingleValue('折子行高') : 4.5,
        zzspacing: this.$appdata.getSingleValue('折子中间间距') ? this.$appdata.getSingleValue('折子中间间距') : 23,
        zzpageRow: this.$appdata.getSingleValue('每页折子行数') ? this.$appdata.getSingleValue('每页折子行数') : 23,
        zzmarginTop: this.$appdata.getSingleValue('折子上边距') ? this.$appdata.getSingleValue('折子上边距') : 11
      }
    },
    props: ['show', 'url', 'data'],
    methods: {
      cancel () {
        this.$dispatch('toggle')
      },
      async printmai () {
        if (this.data.f_zzhh == 1) {
          let msg = await this.$showMessage('请注意，当前折子需要翻页，请确认当前页是新的一页', ['confirm', 'cancel'])
          if (msg == 'confirm') {
            this.print()
          }
        } else {
          this.print()
        }
      },
      async print () {
        try {
          let height = this.zzheight - 0.0
          let spacing = this.zzspacing - 0.0
          let pageRow = this.zzpageRow - 0
          let marginTop = this.zzmarginTop - 0
          var hh = this.data.f_zzhh
          if (hh != -1) {
            var top = marginTop + (hh - 1) * height
            if (hh > pageRow) { top = top + spacing }
            this.top = `${top}mm`
          }
          await this.$resetpost('rs/logic/editBook', {
            f_book_no: this.data.f_book_no,
            f_userinfo_id: this.data.f_userinfo_id,
            f_zzhh: this.data.f_zzhh == -1 ? 13 : this.data.f_zzhh == 24 ? 1 : (parseInt(this.data.f_zzhh) + 1)
          }, {
            resolveMsg: null,
            rejectMsg: '折子打印出错！！'
          })
          this.$refs.print.isPrint = true
          this.$refs.print.print()
        } catch (error) {
          this.$dispatch('refresh')
        }
      }
    },
    watch: {
      'show' (val) {
        if (val) {
          getBillData(this).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>
