<template>
  <div class="auto form-horizontal" >
    <modal :show.sync="false"  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="isEnableBill">
          <div class="col-sm-5" v-if="messShow">
            <span style="color: red"><strong>未获取到收据代码，发票不能正常打印，请确认后补打发票</strong></span>
          </div>
          <div class="col-sm-2" v-if="!messShow">
            <span><strong>发票号:{{model.f_bill_id}}</strong></span>
          </div>
        </div>
        <report-print id='bill' top='15mm' left='5mm'  width='90%'  height='70%' :notrepeat="true" v-ref:reportprint></report-print>
        <button type="button" class="btn btn-default" @click='cancel()'id="common-priBill-取消">取消</button>
      </footer>
    </modal>
  </div>
</template>
<script>


import { DataModel } from 'vue-client'
import co from 'co'

let getBillGen = function * (self) {
  // 判断发票管理是否启用
  if (self.hasBillManage) {
    let data = {
      items: '*',
      condition: `f_user = '${self.$login.f.name}' and f_bill_state = '启用' and f_bill_type = '${self.data.f_bill_style}'`,
      tablename: 't_billcollec',
      orderitem: 'id'
    }
    let res = yield self.$SqlService.singleTableOrderBy(data)
    self.isEnableBill = true
    if (res.data.length > 0) {
      if (res.data[0].f_bill_id > res.data[0].f_end_number) {
        res.data[0].f_bill_state = '停用'
        yield self.$resetpost('rs/entity/t_billcollec', res.data[0], {resolveMsg: null, rejectMsg: null})
        self.messShow = true
      } else {
        self.model.f_bill_id = res.data[0].f_bill_id
        self.model.f_billcoll_id = res.data[0].id
      }
    } else {
      // 提示没有分配发票号段
      self.$showAlert('收费成功。但发票号段未分配, 请联系相关人员分配发票！稍后可进行票据补打进行打票', 'warning', 5000)
      self.$dispatch('toggle')
    }
  } else {
    self.isEnableBill = false
    self.model.f_bill_id = self.data.id
  }
}

let getBillData = async function(self, val) {
  console.log('看看打票里面的data', self.data, self.billUrl, val)
  self.bill = new DataModel(self.billUrl, {reprint: "'正常'"})
  await self.bill.search(self.data.id)
  // 获取发票号
  await self.getBillNumber()
  self.$refs.reportprint.print()
}

export default {
  title: '收费发票',
  data () {
    return {
      model: {
        f_bill_id: null,
        f_billcoll_id: null
      },
      bill: Object,
      // bill: new DataModel('rs/report/machine_bill', {reprint: "'正常'"}),
      // prebill: new DataModel('rs/report/pre_sell', {reprint: "'正常'"}),
      // cardbill: new DataModel('rs/report/card_bill', {reprint: "'正常'"}),
      // billUser: new DataModel('rs/report/machine_user_bill', {reprint: "'正常'"}),
      messShow: false,
      isEnableBill: false
    }
  },
  props: ['show', 'data', 'billUrl', 'billManager'],
  ready () {
    console.log('--------看看this.$login.f是啥', this.$login.f)
  },
  methods: {
    getBillNumber () {
      // 获取是否启用发票管理
      return co(getBillGen(this))
    },
    cancel  () {
      this.$dispatch('toggle')
    }
  },
  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)
      // let data = {
      //   billcoll: {},
      //   detail: {
      //     f_bill_id: this.model.f_bill_id,
      //     f_userinfo_id: this.data.f_userinfo_id,
      //     f_user_id: this.data.f_user_id,
      //     f_user_name: this.data.f_user_name,
      //     f_billdetail_id: this.data.id,
      //     f_card_id: this.data.f_card_id,
      //     f_bill_type: this.data.f_bill_type,
      //     f_bill_state: '已用',
      //     f_total_money: this.data.f_oughtfee,
      //     f_paid_money: this.data.f_collection,
      //     f_operator: this.$login.f.name,
      //     f_filiale: this.$login.f.f_fengongsi,
      //     f_outlets: this.$login.f.f_parentname,
      //     f_bill_style: this.data.f_bill_style
      //   },
      //   type: '打印发票'
      // }
      // if (this.model.f_billcoll_id) {
      //   data.billcoll =  {
      //     id: this.model.f_billcoll_id,
      //     f_bill_id: this.model.f_bill_id + 1
      //   }
      //   this.data.f_billcoll_id = this.model.f_billcoll_id
      // }
      // this.$resetpost('rs/logic/printBill', {data: data}, {resolveMsg: '打印发票成功', rejectMsg: '打印发票失败'}).then((res) => {
      //   this.$dispatch('success', '发票打印', this.data)
      // }).catch((err) => {
      //   console.log('发票打印成功，更新发票数据出错。。。', err)
      //   this.$dispatch('error', '发票打印', this.data, null)
      // })
    },
    'print-error' () {
      this.$dispatch('error', '发票打印', this.data, null)
    }
  }
}

</script>
<style >
</style>
