<template>
  <div class="auto">
    <div style="float: right">
      <button @click="getQueryData()" class="button_search" >获取数据</button>
    </div>
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <div class="row">
          <div class="col-sm-4 form-group">
            <label class="font_normal_body">开始时间</label>
            <datepicker :format="'yyyy-MM-dd HH:mm:ss'" :value.sync="model.f_start_date"
                        class="datepicker"
                        placeholder="起始时间"
                        style="width:60%"
                        v-model="model.f_start_date"
                        v-ref:f_start_date
           ></datepicker>
          </div>
          <div class="col-sm-4 form-group" >
            <label class="font_normal_body" >结束时间</label>
            <datepicker :format="'yyyy-MM-dd HH:mm:ss'" :value.sync="model.f_end_date"
                        class="datepicker"
                        placeholder="结束时间"
                        style="width:60%"
                        v-model="model.f_end_date"
                        v-ref:f_end_date
            ></datepicker>
          </div>


        </div>

        <div class="row">
          <div class="col-sm-4">
            <label for="f_paper_account" class="font_normal_body ">累购金额</label>
            <input class="input_search" style="width:60%"   disabled  v-model="model.f_preamount">
          </div>
          <div class="col-sm-4">
            <label for="f_paper_account" class="font_normal_body ">累购气量</label>
            <input class="input_search" style="width:60%" disabled v-model="model.f_pregas">
          </div>

          <div class="col-sm-4">
            <label for="f_paper_account" class="font_normal_body ">待退金额</label>
            <input class="input_search" style="width:60%" disabled  v-model="model.f_refund_fee">
          </div>
          <div class="col-sm-4">
            <label for="f_paper_account" class="font_normal_body ">已退金额</label>
            <input class="input_search" style="width:60%" disabled v-model="model.f_refunded_fee">
          </div>
          <div class="col-sm-8">
            <label for="f_paper_account" class="font_normal_body ">备&emsp;&emsp;注</label>
            <input class="input_search" style="width:60%" v-model="model.f_comments">
          </div>
          <div class="col-sm-4 form-group">
            <label class="font_normal_body">退费金额</label>
            <input class="input_search" style="width:60%"
                   type="number" v-model="model.f_refund_fee"
                    disabled
                    placeholder="退费金额">
          </div>
          <div class="col-sm-8">
            <label for="f_paper_account" class="font_normal_body ">收款事由</label>
            <input class="input_search" style="width:60%"  v-model="model.f_reason">
          </div>
        </div>
      </form>
      <validate-bill v-if="hasValidateBill" :data="model" @validate-bill="validateBill"></validate-bill>
      <print-bill :show="print" :bill-config='config' :bill-data='billData' :data='data' @toggle="close" @printok="printok"  v-ref:printbill></print-bill>
      <div style="text-align:right;">
        <button class="button_search btn-gn" @click="confirm()" :disabled='!$v.valid'>确认</button>
        <button class="button_clear btn-gn" @click="clean()">取消</button>
      </div>
    </validator>
  </div>

  <upload :blodid="data.f_userinfo_id"  isremark="true" fusetype="退费管理"></upload>
</template>


<script>
  let readyGen = async function (self) {
    self.$refs.f_start_date.value = self.$login.toStandardTimeString()
    self.$refs.f_end_date.value = self.$login.toStandardTimeString()
  }

  export default {
    title: '卡表退费',
    data () {
      return {
        model: {
          f_refunded_fee: '',
          f_refund_fee: '',
          f_comments: '',
          f_reason: '',
          f_pregas: '',
          f_preamount: '',
          f_price: 0
        },

        validateBill (val) {
          this.validateOk = !val.isOk
          this.billData.bill = val.bill
        },
        config: {
          notShowFormula: false, // 不显示计算公式,默认显示
          priceCalculation: false, // 划气量控制
          showupload: true,  // 默认显示附件上传组件
          hasPrint: true,  // 默认打票
          floor: true,  // 是否取整收费
          hasBillManage: false,  // 默认不启用发票管理
          billType: '燃气费',   // 票据类型（燃气费，其他费用，调用的发票代码不同）
          printType: '普通收据', // 收据/电子票/专用发票/国税发票
          payment: '现金缴费'
        },
        blodid: '',
        hasValidateBill: false,
        validateOk: false,
        printModel: {},
        print: false,
        billUrl: '',
        billData: {
          url: 'rs/report/refund_sell',
          operator: this.$login.f.name,
          billnumber: '',
          bill: ''
        }
      }
    },
    props: ['data'],
    ready () {
      readyGen(this)
    },
    methods: {
      async confirm () {
        let param = {
          f_user_id: this.data.f_user_id,
          f_userfiles_id: this.data.f_userfiles_id,
          f_userinfo_id: this.data.f_userinfo_id,
          version: this.data.version,
          f_refund_gas: 0,
          f_refund_fee: this.model.f_refund_fee,
          f_comments: this.model.f_comments,
          f_reason: this.model.f_reason,
          f_balance: this.data.f_balance,
          f_start_date: this.model.f_start_date,
          f_end_date: this.model.f_end_date,
          f_price_id: this.data.f_price_id,
          f_total_gas: parseFloat(this.data.f_total_gas) - 0,
          f_total_fee: parseFloat(this.data.f_total_fee) - parseFloat(this.model.f_refund_fee),
          f_stairprice_id: this.data.f_stairprice_id,
          operInfo: {
            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
          }
        }
        console.log('退费传参', param)
        let res = await this.$resetpost('rs/logic/cardRefund', {data: param}, {warnMsg: `确定要对用户${this.data.f_user_name}进行退费吗？`, resolveMsg: '退费成功！！', rejectMsg: '退费失败！！请重试！！'}).then((res) => {
          if (this.config.hasPrint) {
            if (this.config.hasBillManage) {
              // 启用发票管理，获取票据管理中的票号并存储记录
              this.data.id = res.data.id
              this.data.f_bill_type = '退费'
              this.print = true
            } else {
              this.data.id = res.data.id
              this.print = true
            }
          } else {
            this.$dispatch('success')
          }
        })
      },
      async getQueryData () {
        await this.$resetpost('rs/sql/cardRefundQuery', {
          data: {
            condition: `sg.f_userfiles_id='${this.data.f_userfiles_id}' and sg.f_operate_date >= '${this.model.f_start_date}' and
            sg.f_operate_date <= '${this.model.f_end_date}'`
          }
        }, {resolveMsg: null, rejectMsg: '获取数据失败!'}).then((res) => {
          this.model.f_refunded_fee = res.data[0].f_refunded_fee
          this.model.f_refund_fee = res.data[0].f_refund_fee
          this.model.f_pregas = res.data[0].f_pregas
          this.model.f_preamount = res.data[0].f_preamount
          if(res.data[0].f_refund_fee) {
            this.model.f_comments = '退费时间周期以及一共退费' + res.data[0].f_refund_fee + '元'
          }
        })
      },
      refundChange () {
        this.model.f_refund_fee = (this.model.f_refund_gas - 0) * (this.model.f_price - 0)
      },
      close () {
        this.print = false
        this.clean()
      },
      printok () {
        // 收据打完，判断是否还有其他票据进行请求
        for (let i = 0; i < this.model.f_print.length; i++) {
          if (this.model.f_print[i] === '电子发票') {
            this.$CommonService.openEticket(this.row.id, '售气收费')
          }
        }
        this.$dispatch('success')
      },
      clean () {
        this.$dispatch('refresh')
      }
    }
  }
</script>
