<template>
  <div class="auto">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <div class="row" style="margin-top:10px;">
          <div class="col-sm-6"  :class="[$v.f_pregas.required || $v.f_pregas.dctest ? 'has-error' : 'has-success']">
            <label class=" font_normal_body" >*气&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;量</label>
            <input class="input_search" style="width:60%" type="text" v-model="model.f_pregas" disabled
                   v-validate:f_pregas='{required: true, dctest: [0, ">=" ]}'  placeholder="气量">
          </div>
          <div class="col-sm-6">
            <label  for="f_preamount" class=" font_normal_body" >金&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;额</label>
            <input class="input_search" style="width:60%" type="text" v-model="model.f_preamount" disabled
                   v-validate:f_preamount='{required: true }'  placeholder="金额">
          </div>
        </div>

        <div class="row" style="margin-top:10px;" >
          <div class="col-sm-6" >
            <label for="f_comments" class=" font_normal_body" >备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注</label>
            <input class="input_search" style="width:60%"   v-model="model.f_comments" placeholder="备注">
          </div>
        </div>
      </form>
      <accept-bill :show="acceptprint" :bill-url='acceptUrl' v-on:toggle="close" :data='row' :billdata="printData" v-ref:acceptbill @closeaccept="closeAccept"></accept-bill>
      <div style="text-align:right;">
        <button class="button_search button_spacing" @click="accept()" v-if="config.hasAcceptance" :disabled='!$v.valid'>受理单</button>
        <button class="button_search button_spacing" @click="confirm()"  :disabled='!$v.valid'>初始化卡</button>
        <button class="button_clear" @click="clean()">取消</button>
      </div>
    </validator>
  </div>
</template>
<script>

let asyncOffGasManage = async function (self, val) {
  await self.$getConfig(self, 'ResetShowCard')
  console.log('重发卡售气config', self.config)
  self.model.f_user_id = val.f_user_id
  self.model.f_userinfo_id = val.f_userinfo_id
  self.model.f_card_id = val.f_card_id
  self.model.f_meter_brand = val.f_meter_brand
  if (!self.cardData.Gas && !self.cardData.Money){
    self.$showAlert('请先读卡在进行此操作', 'warning', 2000)
  }
  self.model.f_pregas = self.cardData.Gas
  self.model.f_preamount = self.cardData.Money
  self.model.f_lastremain_fillgas = self.model.f_pregas
  self.model.f_theremain_fillgas = self.model.f_lastremain_fillgas - self.model.f_pregas
/*  let data = {
    items: '*',
    tablename: 't_sellinggas',
    condition: 'f_user_id=' + "'" + self.model.f_user_id + "'" + 'and f_userinfo_id=' + "'" + self.model.f_userinfo_id + "'" + 'and (f_type=' + "'" + '卡表收费' + "'" + 'or f_type=' + "'" + '发卡售气' + "')" + "and f_state='有效'",
    orderitem: 'id desc'
  }
  await self.$SqlService.singleTableOrderBy(data).then((res) => {
    console.log('看下吧返回的是什么：' + JSON.stringify(res))
  }).catch((error) => {
    self.$dispatch('error', '重发卡售气', self.row, error)
  })*/
}

let showcard = async function(self){
  self.row.cardInfo = self.cardData
  try{
    await self.$CardService.resetinitCard(self.model, self.row, self.cardallot)
    self.$dispatch('success')
  }catch(e){
    self.$dispatch('error', '重发卡售气', self.row, e)
  }
}

export default {
  title: '初始化卡',
  data () {
    return {
      config: {
        hasPrint: false,  // 默认打票
        hasBillManage: false,  // 默认不启用发票管理
        billType: '其他费用',   // 票据类型（燃气费，其他费用，调用的发票代码不同）
        printType: '普通收据', // 收据/电子票/专用发票/国税发票
        payment: '现金缴费',
        hasAcceptance: false // 是否需要打印受理单
      },
      model: {
        f_pregas: '',
        f_lastremain_fillgas: '',
        f_preamount: '',
        f_theremain_fillgas: '',
        f_reason: '初始化卡',
        f_comments: '',
        f_meter_brand: '',
        f_card_id: ''
      },
      hasValidateBill: false,
      validateOk: false,

      // 受理单
      acceptprint: false,
      acceptUrl: '',
      printData: {
        reason: '',
        fillgas: '',
        fillamount: ''
      },
      addGasReason: this.$appdata.getParam('重发卡原因')
    }
  },
  props: ['row','cardData'],
  ready () {
    this.model.f_user_id = this.row.f_user_id
    this.model.f_userfiles_id = this.row.f_userfiles_id
    this.model.f_userinfo_id = this.row.f_userinfo_id
    this.model.f_card_id = this.row.f_card_id
    asyncOffGasManage(this, this.row).then(() => {
      this.$emit(ready())
    }).catch((error) => {
      this.$emit('error', error)
    })
  },
  methods: {
    close () {
      this.acceptprint = false
      // this.clean()
    },
    clear () {
      this.$dispatch('refresh')
    },
    accept () {
      // 打印受理单
      if (this.model.f_othereason || this.model.f_reason[0] === '其他') {
        this.printData.reason = `'${this.model.f_othereason}'`
      } else {
        this.printData.reason = this.model.f_comments ? `'${this.model.f_reason[0]}'` : '' + `'${this.model.f_reason}'`
      }
      this.printData.fillgas = `'${this.model.f_pregas}'`
      this.printData.fillamount = `'${this.model.f_preamount}'`
      this.acceptUrl = 'rs/report/acceptOffGasAddGas_bill'
      this.acceptprint = true
    },
    closeAccept () {
      this.acceptprint = false
    },
    confirm () {
      this.$dispatch('no-button')
      this.row.cardInfo = this.cardData
      this.$showMessage(`对客户${this.row.f_user_name}进行初始化卡操作。请确保你已经插入了${this.row.f_meter_brand}的卡？`, ['confirm', 'cancel']).then(async (res) => {
        if (res === 'confirm') {
          showcard(this);
        } else {
          return
        }
      })
    },
    clean () {
      this.$dispatch('cancelclean', this.row)
    }
  }
}
</script>

<style>
</style>
