<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"
             v-validate:f_tablebase='{dctest: [row.f_meter_base, ">=" ] }'  placeholder="表底数">
             <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 Vue from 'vue'

let validatorGen = async function (self) {
  let hasHand = await Vue.resetpost('rs/sql/saleSingleTable', {data: {tablename: 't_handplan', condition: `f_meter_state = '未抄表' and f_user_id = '${self.model.f_user_id}'`}}, {resolveMsg: null, rejectMsg: null, newly: true})
  // 如果是总表判断分表是否已抄
  console.log('换标，看有没有总分表', self.row.f_totalsplit_type)
  let getSplitHand = false
  if (self.row.f_totalsplit_type === '总表') {
    getSplitHand = await self.$HandService.getSplitInfo(self.row.f_user_id)
  }
  if (hasHand.data.length > 0) {
    console.log('有未抄表信息')
    self.row.errorType = 'hand'
    self.$dispatch('firstsuccess', 'hand', self.row)
  } else if (hasHand.data.length === 0 && getSplitHand) {
    console.log('有分表没有抄表')
    self.row.errorType = 'split'
    self.$dispatch('firstsuccess', 'hand', self.row)
  } else if (hasHand.data.length === 0 && !getSplitHand) {
    let machineOwe = await self.$ChangeMeterService.changeMeter(self.model, self.row)
    if (machineOwe.data.isAutoAcc === '是') {
      self.row.isHasOwe = false
      console.log('自动下账了，，，', machineOwe.data.row)
      self.$dispatch('firstsuccess', 'nohand', machineOwe.data.row)
    } else {
      machineOwe.data.row.errorType = 'hasowe'
      self.row.isHasOwe = true
      self.$dispatch('firstsuccess', 'hand', machineOwe.data.row)
    }
  }
}

export default {
  data () {
    return {
      model: {}
    }
  },
  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.model.f_user_id = this.row.f_user_id
    this.model.f_userinfo_id = this.row.f_userinfo_id
  },
  methods: {
    confirm () {
      this.model.lazy = this.$appdata.getSingleValue('滞纳期限')
      this.row.f_tablebase = (this.model.f_tablebase - 0)
      if (this.model.f_tablebase > this.row.f_meter_base) {
        // validatorGen(this)
        this.$showMessage('本次录入表底数大于档案表底数。请到抄表管理界面进行抄表！！', ['confirm']).then((res) => {
          if (res === 'confirm') {
            this.$dispatch('refresh')
          }
        })
      } else if ((this.model.f_tablebase - 0) - this.row.f_meter_base === 0) {
        this.$ChangeMeterService.isHasOwe(this.row).then((res) => {
          console.log('换表底数相同。。', res)
          if (res.owe.length > 0) {
            this.row.errorType = 'owe' // 事件通知这是欠费的
            this.row.isHasOwe = true
            this.$showMessage('该表有欠费, 换表之后欠费记录将转到新表。请告知客户！！', ['confirm']).then((res) => {
              if (res === 'confirm') {
                this.$dispatch('firstsuccess', 'nohand', this.row)
              }
            })
          } else if (res.hasHandplan.length > 0) {
            this.row.errorType = 'hand' // 事件通知这是有未抄表的
            this.$dispatch('firstsuccess', 'hand', this.row)
          } else {
            this.row.isHasOwe = false
            this.row.f_newremanent_amount = this.row.f_balance
            this.$dispatch('firstsuccess', 'nohand', null)
          }
        }).catch((error) => {
          this.$dispatch('error', '换表管理', this.data, error)
        })
      }
    },
    clean () {
      this.$dispatch('refresh')
    }
  }
}
</script>
