<template>
  <div class="auto">
    <modal :show.sync="show" v-ref:modal large backdrop="false">
      <header slot="modal-header" class="modal-header" style="text-align: center">
        <h4 class="modal-title">{{data.type}}退费</h4>
      </header>
      <article slot="modal-body" class="modal-body">
        <validator name='v'>
          <form class="form-horizontal select-overspread" onsubmit="return false">
<!--            <div v-if=""></div>-->
            <upload  v-if="file" :blodid="blodid" cols="col-sm-12" isremark="true" takeimg="true" fusetype="签字图片"></upload>
            <div v-else >是否进行卡表退费</div>
          </form>
        </validator>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button class="btn btn-success" @click="confirm()" :disabled="!$v.valid">确认</button>
        <button class="btn btn-default" @click="clean()">取消</button>
      </footer>

    </modal>

  </div>
</template>

<script>
import {HttpResetClass} from 'vue-client'


  export default {
    props: ['show', 'data', 'file'],
    title: '操作撤销',
    data () {
      return {
        config: {
          hasPrint: true,
          hasBillManage: true,  // 默认不启用发票管理
          cancelType: 'newBill'
        },
        blodid: '',
        model: {
          f_cancel_type: '',
          f_cancel_reason: '',
          f_cancel_name: this.$login.f.name,
          f_operator: this.$login.f.name,
          f_operatorid: this.$login.f.id,
          f_orgid: this.$login.f.orgid,
          f_orgname: this.$login.f.orgs,
          f_depid: this.$login.f.depids,
          f_depname: this.$login.f.deps,
          record_id: ''
        },

        // 发票信息
        printModel: {},
        print: false,
        paytype: this.$appdata.getParam('付款方式'),
        billData: {
          url: '',
          bill: ''
        },
        // 启用发票管理需要对票号进行验证
        hasValidateBill: false,
        validateOk: false
      }
    },
    ready () {
      this.blodid = this.data.f_userinfo_id
    },
    methods: {
      async confirm () {
        // 银行退款操作
        // if (this.data.f_payment === '银行') {
        if (this.file){
          let tablename = 't_sellinggas'
          let condition = `id = '${this.data.id}'`
          let sell = await this.$resetpost('rs/sql/saleSingleTable', {data: {tablename: tablename, condition: condition}}, {
            resolveMsg: null,
            rejectMsg: '退费失败!!'
          })
          if (sell.data[0].f_examine_state === '待审核'){
            this.$showAlert('正在审核中，请等待审核完成后继续退费', 'warning', 1500)
          } else if (sell.data[0].f_examine_state === '已审核'){
            this.tuifei()
          } else {
            this.$showMessage(`此收费记录已隔天，退费需要先进行审核，是否提交审核？`, ['confirm', 'cancel']).then(async (res) => {
              if (res === 'confirm') {
                let sql = `update t_sellinggas set f_examine_state = '待审核',f_examine_date = getdate() where id = '${this.data.id}'`
                let data = {
                  sql: sql
                }
                await this.$resetpost('rs/logic/runSQL',data)
              }
            })
          }
        }else {
          this.tuifei()
        }

        // }
      },
      async tuifei (){
        this.data.f_operator = this.$login.f.name
        this.data.f_operatorid = this.$login.f.id
        this.data.f_orgid = this.$login.f.orgid
        this.data.f_orgname = this.$login.f.orgs
        this.data.f_depid = this.$login.f.depids
        this.data.f_depname = this.$login.f.deps
        let res1 = await this.$resetpost('rs/logic/returns_money',  this.data, {
          resolveMsg: null,
          rejectMsg: '退费失败!!'
        })
        console.log('退费详情',res1)
        let param = {
          'encode': 'qmfRefund',
          'orderNo': this.data.f_serial_id,
          'userCode': this.data.f_userinfo_code,
          'refundTime': res1.refundPayTime
        }
        let res = await this.$resetpost('rs/logic/returns_money_com', {
          param
        }, {
          resolveMsg: null,
          rejectMsg: '退费同步失败!!'
        })
        this.$showAlert(res.data.resMsg, 'warning', 3000)
        if (res.data.resCode === '00') {
          return
        }
        this.clean()
      },
      clean () {
        this.$dispatch('returns', this.model)
      },
      printok () {
        this.$dispatch('returns-success')
      },
      printCancel () {
        this.$dispatch('returns')
      }
    },
    computed: {
      cancelTypes () {
        return [{label: '使用新票号', value: 'newBill'}, {label: '旧发票作废', value: 'abort'}]
      }
    }
  }
</script>
