<template>
    <div class="auto">
        <validator name='v'>
            <form novalidate class="form-horizontal">
                <div class="row">
                    <div class="col-sm-4 form-group">
                        <label class="font_normal_body" title="参数名称：付款方式">收款方式</label>

                        <v-select v-model="f_payment"
                                  placeholder='请选择'
                                  style="width:60%"
                                  :value.sync="model.f_payment"
                                  :options='paytype'
                                  close-on-select clear-button></v-select>

                    </div>
                    <div class="col-sm-4 form-group">
                        <label for="f_print" class="font_normal_body">打印格式</label>

                        <v-select id="print"
                                  v-model="f_print"
                                  style="width:60%"
                                  placeholder='请选择'
                                  :value.sync="model.f_print"
                                  :options='printstyle'
                                  close-on-select clear-button>
                        </v-select>
                    </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="data.f_balance" readonly>
                    </div>
                </div>

                <div class="row" v-if="price_type == '阶梯气价'" v-for="(index,row) in ladderinfo" >
                  <div class="col-sm-4 form-group"
                  >
                    <label class="font_normal_body">阶梯{{row.f_pirce_name}}:气量</label>
                    <input class="input_search" style="width:56%" type="number" v-model="row.f_gas"
                           placeholder="气量">
                  </div>
                  <div class="col-sm-4 form-group"
                  >
                    <label class="font_normal_body">单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;价</label>
                    <input class="input_search" style="width:60%" type="number" v-model="row.f_price"
                           placeholder="单价">
                  </div>
                  <div class="col-sm-4 form-group"
                  >
                    <label class="font_normal_body">金&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;额</label>
                    <input class="input_search" style="width:60%" type="number" v-model="row.f_money"  disabled="true"
                           placeholder="金额">
                  </div>
                </div>

                <div class="row" v-if="price_type == '固定气价'" v-for="(index,row) in ladderinfo" >
                  <div class="col-sm-4 form-group"
                  >
                    <label class="font_normal_body">气&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;量</label>
                    <input class="input_search" style="width:60%" type="number" v-model="ladderinfo[0].f_gas"
                           placeholder="气量">
                  </div>
                  <div class="col-sm-4 form-group"
                  >
                    <label class="font_normal_body">单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;价</label>
                    <input class="input_search" style="width:60%" type="number" v-model="ladderinfo[0].f_price"
                           placeholder="单价">
                  </div>
                  <div class="col-sm-4 form-group"
                  >
                    <label class="font_normal_body">金&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;额</label>
                    <input class="input_search" style="width:60%" type="number" v-model="ladderinfo[0].f_money" disabled="true"
                           placeholder="金额">
                  </div>
                </div>

                <div class="row">

                  <div class="col-sm-4 form-group"
                       :class="[$v.f_refund_gas.required || $v.f_refund_gas.dctest ? 'has-error' : 'has-success']">
                    <label class="font_normal_body">退还气量</label>
                    <input class="input_search" style="width:60%" type="number" v-model="getrefundgas" disabled="true"
                           v-validate:f_refund_fee='{required: true,  dctest: [data.f_total_gas, "<=" ]}' placeholder="退还气量">
                  </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="getrefundfee" disabled="true"
                               placeholder="退费金额">
                    </div>
                  <div class="col-sm-8">
                    <label for="f_paper_account" class="font_normal_body ">&nbsp;&nbsp;&nbsp;备注&nbsp;&nbsp;&nbsp;</label>
                    <input class="input_search" style="width:80%"  v-model="model.f_comments">
                  </div>
                </div>
            </form>
            <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>
        <validate-bill v-if="hasValidateBill" :data="model" @validate-bill="validateBill"></validate-bill>
        <!--<print-bill :show="print" :bill-manager='config.hasBillManage' v-ref:printbill :bill-url='billUrl' v-on:toggle="clean" @printok="printok" :data='printModel'></print-bill>-->
        <print-bill :show="print" :bill-config='config' :bill-data='billData' v-on:toggle="clean" @printok="printok" :data='printModel'  v-ref:printbill></print-bill>
    </div>

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


<script>
    let readyGen = async function (self) {
      await self.$getConfig(self, 'MachineTRefund')
      console.log('机表退费config', self.config)
        // 默认打印格式
      self.blodid = self.data.f_userinfo_id
      self.model.f_print = self.config.printType instanceof Array ? self.config.printType : [self.config.printType]
      self.model.f_payment = [self.config.payment]

      self.model.f_use_type = self.config.billType
      self.model.f_bill_type = self.model.f_print
      self.hasValidateBill = self.config.hasBillManage
      self.price_type = self.data.f_price_type
    self.initladderdata()
}

    export default {
      title: '卡表退费',
      data () {
        return {
          ladderinfo: [],
          priceinfo: [],
          price_type: '',
          config: {
            showupload: false, // 默认不上传附件
            hasPrint: false,  // 默认打票
            hasBillManage: false,  // 默认不启用发票管理
            printType: '普通收据', // 收据/电子票/专用发票/国税发票
            payment: '现金缴费',
            billType: '燃气费'
          },
          model: {
            f_refund_fee: '',
            f_refund_gas: '',
            f_comments: '',
            f_payment: '',
            f_print: ''
          },

          blodid: '',
          hasValidateBill: false,
          validateOk: false,

          printModel: {},
          print: false,
          billUrl: '',
          billData: {
            url: 'rs/report/refund_sell',
            bill: ''
          },

                // 下拉框值
          paytype: this.$appdata.getParam('付款方式'),
          printstyle: this.$appdata.getParam('打印格式')
        }
      },
      props: ['data'],
      ready () {
        readyGen(this)
      },
      computed: {
        'getrefundgas' () {
          this.model.f_refund_gas = 0
          let allgas = 0
          if (this.ladderinfo.length > 0) {
            for (let i = 0; i < this.ladderinfo.length; i++) {
              if (this.ladderinfo[i].f_gas < 0) this.ladderinfo[i].f_gas = 0
              if (this.ladderinfo[i].f_price < 0) this.ladderinfo[i].f_price = this.priceinfo[i].f_price
              this.model.f_refund_gas = ((this.model.f_refund_gas - 0) + (this.ladderinfo[i].f_gas - 0)).toFixed(2)
            }
          }
          return this.model.f_refund_gas
        },
        'getrefundfee' () {
          this.model.f_refund_fee = 0
          if (this.ladderinfo.length > 0) {
            for (let i = 0; i < this.ladderinfo.length; i++) {
              if (this.ladderinfo[i].f_gas < 0) this.ladderinfo[i].f_gas = 0
              if (this.ladderinfo[i].f_price < 0) this.ladderinfo[i].f_price = this.priceinfo[i].f_price
              this.ladderinfo[i].f_money = ((this.ladderinfo[i].f_gas - 0) * (this.ladderinfo[i].f_price - 0)).toFixed(2)
              this.model.f_refund_fee = ((this.model.f_refund_fee - 0) + (this.ladderinfo[i].f_money - 0)).toFixed(2)
            }
          }
          return this.model.f_refund_fee
        }
      },
      methods: {
        async initladderdata () {
          let res = await this.$resetpost('rs/sql/getpriceInfo',
              {data: {price_id: this.data.f_price_id}}, {resolveMsg: null, rejectMsg: null})
          if (res && res.data) {
            if (res.data.length > 0) {
              this.priceinfo = res.data
            } else {
              this.priceinfo = []
            }
          }
          if (this.priceinfo.length > 0) {
            let row = {}
            for ({f_price: row.f_price, f_price_name: row.f_price_name} of this.priceinfo) {
              this.ladderinfo.push({f_gas: 0, f_price: row.f_price, f_money: 0, f_pirce_name: row.f_price_name})
            }
          } else {
            this.ladderinfo.push({f_gas: 0, f_price: 0, f_money: 0, f_pirce_name: '1'})
          }
        },
        async confirm () {
          if (this.model.f_refund_fee == 0) {
            this.$showAlert('不能退费0元！！', 'warning', 2000)
            return
          }
          if (this.data.f_total_fee < this.model.f_refund_fee) {
            this.$showAlert('退费金额不能大于累购金额！！', 'warning', 2000)
            return
          }
          if (this.data.f_total_gas < this.model.f_refund_gas) {
            this.$showAlert('退费气量不能大于累购气量！！', 'warning', 2000)
            return
          }
          let param = {
            f_user_id: this.data.f_user_id,
            f_userfiles_id: this.data.f_userfiles_id,
            version: this.data.version,
            f_refund_fee: this.model.f_refund_fee,
            f_refund_gas: this.model.f_refund_gas,
            f_comments: this.model.f_comments,
            f_payment: this.model.f_payment[0],
            f_print: this.model.f_print[0],
            f_price_id: this.data.f_price_id,
            f_price_type: this.data.f_price_type,
            f_total_fee: parseFloat(this.data.f_total_fee) - parseFloat(this.model.f_refund_fee),
            f_total_gas: parseFloat(this.data.f_total_gas) - parseFloat(this.model.f_refund_gas),
            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
            },
            ladderinfo: this.ladderinfo
          }

          let res = await this.$resetpost('rs/logic/cardRefund', {data: param}, {warnMsg: `确定要对用户${this.data.f_user_name}进行退费吗？`, resolveMsg: '退费成功！！', rejectMsg: '退费失败！！请重试！！'})

          if (this.model.f_print.indexOf('电子发票') == -1 && this.config.hasPrint && !this.$login.r.includes('不打印发票')) {
            console.log('this.config.hasBillManage', this.config.hasBillManage)
            if (this.config.hasBillManage) {
                        // 启用发票管理，获取票据管理中的票号并存储记录
              this.printModel.id = res.data.id
              this.printModel.f_bill_type = '退费'
              this.printModel.f_bill_style = this.model.f_print[0]
                        // this.billUrl = 'rs/report/refund_sell'
              this.print = true
            } else {
              this.printModel.id = res.data.id
                        // this.billUrl = 'rs/report/refund_sell'
              this.print = true
            }
          } else if (this.config.printType === '国税发票') {
                    // TODO
            this.$dispatch('success')
          } else if (this.config.printType === '电子发票') {
                    // TODO
            this.$dispatch('success')
          } else {
            this.$dispatch('success')
          }
        },
        clean () {
          this.$dispatch('refresh')
        },
        printok () {
          this.$dispatch('success')
        },
        validateBill (val) {
          this.validateOk = !val.isOk
          this.billData.bill = val.bill
        }
      }
    }
</script>
