<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">
        <!--        <div v-if="bill.data">-->
        <!--          {{{bill.data}}}-->
        <!--        </div>-->
        <validator name='v'>
          <form class="form-horizontal  select-overspread">
            <div class="row" style="display: flex;justify-content: center;" id='normal-bill-sld' v-if="bill.data">
              {{{bill.data}}}
            </div>
          </form>
        </validator>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button type="button" class="btn btn-success" v-if="!disprint" @click='print()'>打印</button>
        <report-print :style="printstyle" id='normal-bill-sld' top='10mm' left='10mm'  width='90%'  height='90%' printdiect="2" :notrepeat="false" :showbtn="false" v-ref:reportprint></report-print>
        <button type="button" class="btn btn-default"  @click='cancel()' v-if="!billConfig.hasBillManage">取消</button>
      </footer>
    </modal>
  </div>
</template>
<script>
  import {DataModel, HttpResetClass} from 'vue-client'

  let getBillData = async function (self, val) {
    let temp = {}
    temp.staticData = {
      f_user_name: 'test'
    }
    console.log('self.billData.url:', self.billData.url)
    self.bill = new DataModel(self.billData.url, temp)
    await self.bill.search(self.data, self.data)
  }

  export default {
    title: '收费发票',
    data () {
      return {
        printstyle: 'td {border-width: 2px;padding: 0.5px;border-style: solid; border-color:#000000;background:#ffffff;  }  \n' +
          'table{background:#ffffff;border-collapse:collapse; border}\n' +
          '.noborder{border: none;background:#ffffff;}\n' +
          '.tdleft{text-align: left;background:#ffffff;}\n' +
          '.tdright{text-align: right;background:#ffffff;}  \n' +
          '.tdcenter{text-align: center;background:#ffffff;}\n' +
          '.fs10{font-size: 0.4em;}\n' +
          '.fs11{font-size: 0.5em;}\n' +
          '.fs12{font-size: 0.6em;}\n' +
          '.fs13{font-size: 0.7em;}\n' +
          '.fs15{font-size: 0.9em;}\n' +
          '.fs17{font-size: 1.1em;}\n' +
          '.fs19{font-size: 1.2em;}\n' +
          '.fs24{font-size: 1.5em;}\n' +
          '.fs26{font-size: 1.6em;}\n' +
          '.b{font-weight: bold;}\n' +
          '.td-border-right{BORDER-RIGHT: #000000 1px solid; background:#ffffff; }\n' +
          '.td-border-top{BORDER-TOP: #000000 1px solid; background:#ffffff;}\n' +
          '.td-border-left{BORDER-LEFT: #000000 1px solid;background:#ffffff;}\n' +
          '.td-border-bottom{BORDER-BOTTOM: #000000 1px solid;background:#ffffff;}',
        model: {
          f_using_number: null
        },
        batch: false,
        // bill: new DataModel('rs/report/meterForm'),
        bill: {},
        messShow: false,
        printControl: null
      }
    },
    props: ['show', 'data', 'billData', 'billConfig', 'disprint'],
    ready () {
      console.log('打票', this.billConfig, this.billData)
    },
    methods: {
      async cancel  () {
        await this.$dispatch('toggle')
      },

      async printChecked (p) {
        return new Promise(async (resolve) => {
          this.printControl = resolve
          this.bill = new DataModel(`${this.billData.url}`)

          this.bill.paramSource = p
          // this.bill.paramSource = {condition:`${this.data}` ,staticData:{f_user_name:`${this.data.f_user_name}`}}
          await this.bill.search()

          if (this.bill.state == '错误') {
            this.$showMessage(this.bill.error)
            return
          }
          await this.$refs.reportprint.print()
          await this.addPrintNum()
        })
      },
      async addPrintNum(){
        var param = {
          f_print_id:this.data.id,
          f_print_num:this.data.f_print_num+1
        }
        if(!this.data.f_verify_state){
          param.f_verify_state='未审核'
        }
        let http = new HttpResetClass()
        await http.load('POST', 'rs/entity/t_print_sheet',
          param,
          {resolveMsg: null, rejectMsg: "打印次数未成功加一"})
      },
      async print () {
        // 更新系统发票
        try {
          // if (this.billConfig.hasBillManage) {
          //  if(this.data.f_bill_type != '清算单')
          //
          // } else {
          //  await this.$resetpost('rs/logic/sale_billReprint', {data: this.data}, {resolveMsg: null, rejectMsg: '票据补打出错！！'})
          // }
          this.$refs.reportprint.print()
          await this.addPrintNum()
          await this.cancel()
        } catch (error) {
          this.$dispatch('refresh', this.data)
        }
      }
  },
  watch: {
    'show' (val) {
      if (val && (!this.batch)) {
        getBillData(this, val).then(() => {
          this.$emit('ready')
        }).catch((error) => {
          this.$emit('error', error)
        })
      }
    }
  },
  events: {
    async 'print' () {
      // 对票据明细进行更新或插入操作
      await this.$dispatch('printok', '发票打印', this.data)
    },
    'print-error' () {
      this.batch = false
      this.$dispatch('error', '发票打印', this.data, null)
    }
  }
  }
</script>
<style >
</style>
