<template>
  <div class="auto">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <div  style="margin-top:10px" :class="[$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" @blur='tablebaseChange()'
             v-validate:f_tablebase='{dctest: [row.f_meter_base, ">=" ] }'  placeholder="表底数" :disabled="disabled">
             <span v-if="$v.f_tablebase.dctest" style="color:red ">本次表底数不能小于上次表底数</span>
        </div>
      </form>
      <div class="col-sm-4" style="margin-left:8%" 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>
/**
*换表管理
*处理表类型为机表和物联网表的旧表信息
*/
import co from 'co'
import Vue from 'vue'

let validatorGen = function * (self) {
  // 校验表剩余气量或者余额是否为负值
  // let result = yield Vue.resetpost('rs/sql/querySingleTable', {data: {items: 'f_surplus_gas,f_surplus_amount', tablename: 't_userfiles', condition: `f_user_id = ${self.row.f_user_id}`}}, {resolveMsg: null, rejectMsg: null})
  // if (result.data[0].f_surplus_gas)
}

let handenter = async function (self) {
  //  物联网表结算
  let res = await self.$ChangeMeterService.changeMeter(self.model, self.row)

  console.log('iot结算结果', res)

  let temp = 0
  if (self.row.f_calculation === '预付费') {
    if (self.row.f_billing === '按气量') {
      temp = res.data.f_balance_gas
    } else {
      temp = res.data.f_balance_amount
    }
  } else {
    temp = res.data.f_balance
  }

  if (temp < 0) {
    res.data.errorType = 'iot'
    self.$dispatch('firstsuccess', 'hand', res.data)
  } else {
    if (self.row.f_calculation === '预付费') {
      if (self.row.f_billing === '按气量') {
        self.row.f_newremanent_gas = res.data.f_balance_gas
      } else {
        self.row.f_newremanent_amount = res.data.f_balance_amount
      }
    } else {
      self.row.f_newremanent_amount = res.data.f_balance
    }
    self.$dispatch('firstsuccess', 'nohand')
  }
}

export default {
  data () {
    return {
      isdecimal: '',
      model: {},
      disabled: false
    }
  },
  props: ['row', 'message'],
  watch: {
    'row' (val) {
      this.model.f_user_id = val.f_user_id
      this.model.f_userinfo_id = val.f_userinfo_id
    }
  },
  ready () {
    this.isdecimal = this.row.f_isdecimal
    console.log('看看物联网换表组件的内容：', this.row)
    this.model.f_user_id = this.row.f_user_id
    this.model.f_userinfo_id = this.row.f_userinfo_id
  },
  methods: {
    clean () {
      this.$dispatch('refresh')
    },
    confirm () {
      this.row.f_tablebase = this.model.f_tablebase
      if (this.model.f_tablebase > this.row.f_meter_base) {
        this.$showMessage('此操作将会扣除表中相应的气量或金额值，请确认是否继续？', ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            handenter(this)
          }
        })
      } else if ((this.model.f_tablebase - 0) === (this.row.f_meter_base - 0)) {
        // 判断剩余量是否为负
        if (this.row.f_calculation === '预付费') {
          if (this.row.f_billing === '按气量') {
            if ((this.row.f_balance_gas - 0) >= 0) {
              this.row.f_newremanent_gas = this.row.f_balance_gas
              this.$dispatch('firstsuccess', 'nohand')
            } else {
              this.row.errorType = 'iot'
              this.$dispatch('firstsuccess', 'hand', this.row)
            }
          } else {
            if ((this.row.f_balance_amount - 0) >= 0) {
              this.row.f_newremanent_amount = this.row.f_balance_amount
              this.$dispatch('firstsuccess', 'nohand')
            } else {
              this.row.errorType = 'iot'
              this.$dispatch('firstsuccess', 'hand', this.row)
            }
          }
        } else {
          if ((this.row.f_balance - 0) >= 0) {
            this.row.f_newremanent_amount = this.row.f_balance
            this.$dispatch('firstsuccess', 'nohand')
          } else {
            this.row.errorType = 'iot'
            this.$dispatch('firstsuccess', 'hand', this.row)
          }
        }
      }
    }
  }
}
</script>
