<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 class="row" style="display: flex;justify-content: center;" v-if="!eticket_src">
          {{ return_msg }}
        </div>
        <iframe :src="eticket_src" v-if="eticket_src" width="100%" height="500px">
        </iframe>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button type="button" class="btn btn-default" @click="cancel">{{ '关闭' }}</button>
      </footer>
    </modal>
  </div>
</template>
<script>
import {HttpResetClass} from 'vue-client'
import Vue from 'vue'
import axios from "axios";

export default {
  title: '电子发票',
  data () {
    return {
      eticket_src: '',
      return_msg: '航信收到开票请求，正在查询开票状态，请稍后',
      show: true
    }
  },
  props: ['show'],
  ready () {
  },
  methods: {
    cancel () {
      console.log('电子哈哈哈，你走了没')
      this.eticket_src = ''
      this.return_msg = '正在查询中，请稍后'
      this.$dispatch('toggle')
    },
    /**
     * src赋值 外部调用
     *  */
    watch_ch (src) {
      this.eticket_src = src
    },
    /** 冲红
     * */
    async rushRedInvoice (data) {
      try {
        // 否则正常打票
        let result = await axios.post('/invoice/rs/logic/getInvoice', Object.assign(data, {invoice_type: 1}), {
          resolveMsg: null,
          rejectMsg: '发票冲红失败!!'
        })
        this.return_msg = '冲红成功请前往发票管理页面查看'
      } catch (error) {
        this.return_msg = '冲红失败请前往发票管理页面重试'
        this.$showAlert('电子票冲红出现异常，请联系管理员！！！', 'warning', 5000)
      }
    },
    /**
     *
     * @param id 收费id
     * @param model
     * @param f_charge_type
     * @param is_tax
     * @param invoice_type
     * @param retry
     * @returns {Promise<void>}  如果查到返回url
     */
    async openETicket (id, model, f_charge_type, invoice_type, retry, is_tax, handplanTableName) {
      console.log('>>> f_invoice_type', invoice_type)
      try {
        let result = await axios.post('/invoice/rs/logic/getInvoice', Object.assign({
          id,
          f_charge_type,
          retry,
          handplanTableName,
          isTax: is_tax === '不征税' ? 0 : 1,
          eticket_id: retry ? model.id : null,
          invoice_kind: invoice_type,
          f_buy_openbank: model.f_paper_account,
          f_buy_phone: model.f_buy_phone,
          bill_type: model.f_print ? model.f_print : null,
          f_paper_name: model.f_paper_name,
          f_taxpayer_id: model.f_taxpayer_id,
          f_address_phone: model.f_address_phone,
          f_email: model.f_email,
          org_id: this.$login.f.orgid,
          bz: model.f_eticket_reason ? model.f_eticket_reason : null,
          loginInfo: {
            operator: Vue.$login.f.name,
            operator_id: Vue.$login.f.id,
            org_id: Vue.$login.f.orgid,
            orgname: Vue.$login.f.orgs,
            dep_id: Vue.$login.f.depids,
            depname: Vue.$login.f.deps
          }
        }, {f_invoice_type: 0}), {
          resolveMsg: null,
          rejectMsg: '发票打印失败!!'
        })
        let res = result.data.data
        if (res.code === '9999') {
          this.return_msg = res.msg
        } else {
          this.return_msg = '开票成功,请前往发票管理页面查看'
        }
      } catch (error) {
        this.return_msg = '开票失败请前往发票管理页面重试'
        this.$showAlert('电子发票打印出现异常，请联系管理员！！！', 'warning', 5000)
      }
    }

    //* 公用保存信息接口 弃用 最新电子票接口自己保存 营收不做保存操作
    // async save_msg (obj) {
    //   let model = {
    //     id: obj.f_eticket_id,
    //     f_bill_code: obj.f_bill_code ? obj.f_bill_code : undefined,
    //     f_bill_number: obj.f_bill_number ? obj.f_bill_number : undefined,
    //     f_jym: obj.f_jym ? obj.f_jym : undefined,
    //     f_bill_url: obj.f_bill_url ? obj.f_bill_url : undefined,
    //     f_red_url: obj.f_red_url ? obj.f_red_url : undefined,
    //     f_bill_state: obj.f_bill_state,
    //     f_sign_result: obj.f_sign_result ? obj.f_sign_result : undefined,
    //     f_sign_time: obj.f_sign_time ? obj.f_sign_time : undefined
    //   }
    //   await this.$resetpost('rs/entity/t_eticket', model, {resolveMsg: null, rejectMsg: '保存发票记录失败'})
    // },
    // 弃用 最新电子票接口自己开票组装数据 营收不做组装数据操作
    // async openETicket (sellId, row, show_gas, is_pax, type) {
    //   try {
    //     let model = {
    //       id: sellId,
    //       f_charge_type: type,
    //       f_dept_id: this.$login.f.depids,
    //       f_operator: this.$login.f.name,
    //       f_orgid: this.$login.f.orgid,
    //       f_orgname: this.$login.f.orgs,
    //       f_depname: this.$login.f.deps,
    //       f_operatorid: this.$login.f.id,
    //       f_filialeids: this.$login.f.f_orgids,
    //       f_buy_name: row.f_paper_name,
    //       f_pay_id: row.f_taxpayer_id,
    //       f_buy_address: row.f_address_phone,
    //       f_mail_push: row.f_email,
    //       f_buy_openbank: row.f_paper_account,
    //       invoice_show_gas: show_gas === '是',
    //       is_pax: is_pax === '征税'
    //     }
    //     console.log('组织开票数据并保存', model)
    //     // TODO 组织开票数据并保存
    //     let ticket_id = await this.$resetpost('rs/logic/TCcombineData', model, {resolveMsg: null, rejectMsg: '保存开票信息失败'})
    //     return this.openEticketGen(ticket_id.data)
    //   } catch (error) {
    //     this.$showAlert(`电子发票开票失败！！请到发票管理重新开票。错误原因: ${JSON.stringify(error)}。`, 'danger', 0)
    //   }
    // },
    /**
     * 电票查询 外部调用
     * 弃用 最新电子票接口自己定时器查询发票 不需要用户手动查询
     * */
    // fp_search (row, ticket_id) {
    //   // 通过收费记录id查询
    //   let http = new HttpResetClass()
    //   http.load('POST', '/invoice/rs/tongchuan/queryInvoice', {eticket_id: ticket_id}, {
    //     resolveMsg: null,
    //     rejectMsg: null
    //   }).then((req) => {
    //     if (!req.data.RESPONSE_FPXXXZ) {
    //       this.$showAlert('电子票查询出错,暂无错误信息.请联系管理员', 'danger', 5000)
    //       return
    //     }
    //     req = req.data.RESPONSE_FPXXXZ
    //     let model
    //     if (req.RETURN_CODE === '0000') {
    //       if (req.PDF_URL) {
    //         model = {
    //           id: ticket_id,
    //           f_bill_code: req.FPDM,
    //           f_bill_number: req.FPHM,
    //           f_jym: req.INVOICE_CHECK_CODE,
    //           f_bill_url: req.PDF_URL,
    //           f_bill_state: '开票成功',
    //           f_sign_result: req.RETURN_MESSAGE
    //         }
    //         this.eticket_src = req.PDF_URL
    //       } else if (req.PDF_RED_URL) {
    //         model = {
    //           id: ticket_id,
    //           f_bill_code: req.FPDM,
    //           f_bill_number: req.FPHM,
    //           f_jym: req.INVOICE_CHECK_CODE,
    //           f_red_url: req.PDF_RED_URL,
    //           f_bill_state: '冲红',
    //           f_sign_result: req.RETURN_MESSAGE
    //         }
    //         this.eticket_src = req.PDF_RED_URL
    //       }
    //     } else if (req.RETURN_CODE === '0011') {
    //       model = {
    //         id: ticket_id,
    //         f_bill_state: '等待查询',
    //         f_sign_result: req.RETURN_MESSAGE
    //       }
    //       this.return_msg = '请稍后再试。' + req.RETURN_MESSAGE
    //     } else {
    //       model = {
    //         id: row.id,
    //         f_bill_state: '开票失败',
    //         f_sign_result: req.RETURN_MESSAGE
    //       }
    //       this.return_msg = ' 开票失败。' + req.RETURN_MESSAGE
    //     }
    //     this.$resetpost('rs/entity/t_eticket', model, {resolveMsg: null, rejectMsg: '保存发票记录失败'}).then(
    //       res => this.$dispatch('search')
    //     )
    //   })
    // },
  },
  watch: {},
  events: {}
}
</script>
<style>
</style>
