<template>
  <div  class="auto">
    <validator name='v'>
      <form novalidate class="form-horizontal" οnsubmit="return false;">
        <div class="row">
          <div class="col-sm-4" :class="[$v.f_tablebase.required || $v.f_tablebase.dctest ? 'has-error' : 'has-success']">
            <label class="font_normal_body">表底数</label>
            <input class="input_search" style="width:60%" type="number" v-model="model.f_tablebase"
                   @keyup.enter="confirm()" @blur='tablebaseChange()'
                   v-validate:f_tablebase='{required: true, dctest: [0, ">=" ]}'
                   placeholder="表底数" :disabled="disabled">
          </div>
        </div>
     </form>
      <div style="margin-left:8%;text-align: right" v-if="!message">
        <button class="button_search" @click="confirm()"  :disabled='!$v.valid'>确认</button>
        <button class="button_clear" @click="clean()" >取消</button>
      </div>
    </validator>
  </div>
</template>
<script>
/**
*换表管理
*处理表类型为气量卡表的旧表信息
*/
export default {
  data () {
    return {
      model: {},
      isdecimal: '否',
      disabled: false
    }
  },
  props: ['row', 'message'],
  ready () {
    console.log('看看气量卡表组件的内容：', this.row)
    this.isdecimal = this.row.f_isdecimal
  },
  methods: {
    confirm () {
      if (this.model.f_tablebase > this.row.f_total_gas) {

        this.$showMessage('该表将会超用,确定要进行超用吗？', ['confirm', 'cancel']).then((oper) => {
          if (oper === 'confirm') {
            this.$ChangeMeterService.changeMeter(this.model, this.row).then((res) => {
              res.data.errorType = 'exceeduse'
              this.$dispatch('firstsuccess', 'hand', res.data)
              this.disabled = true
            }).catch((error) => {
              this.$error(error)
              this.$dispatch('error')
            })
          }
        })
      } else {
        if (this.row.f_remanent_gas > 0) {
          this.$showAlert('旧表仍有剩余气量没有处理，请处理完成之后在进行换表操作。', 'warning', 5000)
        } else {
          this.row.f_newremanent_gas = ((this.row.f_remanent_gas ? this.row.f_remanent_gas : 0 - 0) + (this.row.f_total_gas - this.model.f_tablebase)).toFixed(2)
          this.row.f_newbalance = this.row.f_balance
          this.row.f_tablebase = this.model.f_tablebase
          this.$dispatch('firstsuccess', 'nohand')
        }
      }
    },
    clean () {
      this.$dispatch('refresh')
    },
    tablebaseChange () {
      if (this.isdecimal === '是') {
        this.model.f_tablebase = (this.model.f_tablebase - 0).toFixed(2)
      } else {
        this.model.f_tablebase = (this.model.f_tablebase - 0).toFixed(0)
      }
    }
  }
}
</script>
