<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>-->
<!--            <input v-show="false" v-model="model.f_add_gas" v-validate:f_operat_type='{required: true}'>-->
<!--            <v-select :value.sync="model.f_add_gas" value-single-->
<!--                      :options='addGas' placeholder='请选择'-->
<!--                      selected="是否计入累购"-->
<!--                      style="width:60%"-->
<!--                      close-on-select></v-select>-->
<!--          </div>-->

<!--          <div class="col-sm-4 form-group">-->
<!--            <label class="font_normal_body">操&nbsp;&nbsp;作</label>-->
<!--            <input v-show="false" v-model="model.f_operat_type"-->
<!--                   v-validate:f_operat_type='{required: true}'>-->
<!--            <v-select v-if="this.data.f_collection_type==='按金额'" :value.sync="model.f_operat_type"-->
<!--                      :options='operator_type' placeholder='请选择'-->
<!--                      selected="补费"-->
<!--                      style="width:60%"-->
<!--                      close-on-select></v-select>-->
<!--            <v-select v-if="this.data.f_collection_type==='按气量'" :value.sync="model.f_operat_type"-->
<!--                      :options='operator_type2' placeholder='请选择'-->
<!--                      selected="补气"-->
<!--                      style="width:60%"-->
<!--                      close-on-select></v-select>-->
<!--          </div>-->
          <div v-if="row.f_meter_type === '金额卡表'" class="col-sm-4 form-group"
               :class="[$v.f_preamount.required || $v.f_preamount.dctest ? 'has-error' : 'has-success']">
            <label class="font_normal_body">&nbsp;金额&nbsp;</label>
            <input class="input_search" type="number" v-model="model.f_preamount" style="width:60%"
                   v-validate:f_preamount='{required: true,  dctest: [0, ">" ]}' placeholder="金额">
          </div>
          <div v-if="row.f_meter_type === '气量卡表'" class="col-sm-4 form-group"
               :class="[$v.f_pregas.required || $v.f_pregas.dctest ? 'has-error' : 'has-success']">
            <label class="font_normal_body">&nbsp;气量&nbsp;</label>
            <input class="input_search" type="number" v-model="model.f_pregas" style="width:60%"
                   v-validate:f_pregas='{required: true,  dctest: [0, ">" ]}' placeholder="气量">
          </div>
        </div>

        <div class="row">
          <div class="col-sm-12">
            <label for="f_paper_account" class="font_normal_body ">备&nbsp;&nbsp;注</label>
            <input class="input_search" style="width:87%" rows="1" v-model="model.f_comments"></input>
          </div>
        </div>
      </form>
      <div style="text-align:right;">
        <button class="btn button_search btn-gn" @click="confirm()" :disabled='!$v.valid'>确认</button>
        <button class="btn 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" v-if="true" isremark="true" fusetype="余量补差"></upload>
</template>


<script>
  import Vue from 'vue'
  // 执行保存逻辑
  let sellgasGen = async function (self) {
    try {
      self.row.cardInfo = self.cardData

      if (self.paymentModel.length === 0) {
        self.paymentModel = [{
          f_payment: self.model.f_payment[0],
          f_money: self.model.f_preamount
        }]
      }
      self.model.xiekamoney = self.model.f_preamount ? self.model.f_preamount : 0
      self.model.payments = self.paymentModel
      let res = await self.$CardService.margin(self.model, self.row)
      let param = []
      for (let row of self.resid) {
        param.push({id: row.id})
      }
      let writeid
      if (res.data && res.data.id) {
        writeid = res.data.id
      } else {
        writeid = res
      }
      let data = {
        param: param,
        f_blobid: writeid
      }
      await self.$resetpost('rs/logic/updatefiles', data)
      self.clickConfirm = false
    } catch (error) {
      console.log('捕获到异常', error)
      if (error instanceof Object) {
        self.$showAlert(error.data, 'danger', 0)
      } else {
        self.$showAlert(error, 'danger', 0)
      }
      self.$dispatch('refresh')
      self.clickConfirm = false
    }
    self.$dispatch('success', '余量补差')
  }

export default {
    title: '物联网表补费扣费',
    data () {
      return {
        blodid: '',
        resid:[],
        showupload: false,
        paymentModel: [], // 多笔付款方式
        model: {
          f_operat_type: '余量补差',
          f_purchase: 0,
          f_payment: ['现金收费'],
          f_pregas: '',
          f_preamount: '',
          f_totalcost: 0,
          f_curbalance: 0,
          f_collection: 0,
          f_balance: 0,
          f_print: [],
          f_meter_type: '',
          f_voucher_number: '',
          f_banktransfer_person: '',
          f_banktransfer_date: '',
          f_privilege_id: 0,
          f_privilege_money: 0,
          f_after_discount: 0
        },

      }
    },
    props: ['row'],
    ready () {
    },
  events: {
    // 删除Resid数组元素
    'delResid' (val) {
      this.resid.$remove({id: val, f_biobid: ''})
      // this.resid.splice(this.resid.indexOf({id:val,f_biobid:''}),1);
    },
    // 增加Resid数组元素
    'resid' (val) {
      this.resid.push({id: val, f_biobid: ''})
    }
  },
    methods: {
      clean () {
        this.$info('取消操作')
        this.$dispatch('refresh', this.row)
      },
      confirm () {

        this.$showMessage(`确定对用户${this.row.f_user_name}进行余量补差吗？`, ['confirm', 'cancel']).then(async (res) => {
          if (res === 'confirm') {
            sellgasGen(this)
          }
        })
      }
    }
  }
</script>
