<template>
  <div class="auto">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <div class="row" style="margin-top:10px;">
          <div class="col-sm-4" v-if='row.f_meter_type==="气量卡表"' :class="[$v.f_pregas.required || $v.f_pregas.dctest ? 'has-error' : 'has-success']">
            <label class=" font_normal_body" >补气量</label>
            <input class="input_search" style="width:60%" type="text" v-model="model.f_pregas"
                   v-validate:f_pregas='{required: true, dctest: [0, ">=" ]}'  placeholder="补气量">
          </div>
          <div class="col-sm-4" v-if='row.f_meter_type==="金额卡表"'>
            <label  for="f_pregas" class=" font_normal_body" >补气金额</label>
            <input class="input_search" style="width:60%" type="text" v-model="model.f_fillamount"
                   v-validate:f_pregas='{required: true }'  placeholder="补气金额">
          </div>
          <div class="col-sm-4" >
            <label for="f_reason" class=" font_normal_body" >补气原因</label>
            <v-select id="f_reason"
                      v-model="model.f_reason"
                      placeholder='补气原因'
                      :value.sync="model.f_reason"
                      :options='addGasReason'
                      close-on-select ></v-select>
          </div>
        </div>

        <div class="row" style="margin-top:10px;" v-if="model.f_reason[0] ==='其他'">
          <div class="col-sm-offset-2 col-sm-8" :class="[$v.f_othereason.required ? 'has-error' : '']">
            <label for="f_reason" class=" font_normal_body" >其他原因</label>
            <input class="input_search" style="width:60%" v-validate:f_othereason='{required: true }' rows="3" v-model="model.f_othereason" 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,'OffGasAddGas')
  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
  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: 'f_operate_date desc, id desc'
  }
  await self.$SqlService.singleTableOrderBy(data).then((res) => {
    console.log('看下吧返回的是什么：' + JSON.stringify(res))
    self.model.f_pregas = res.data[0].f_pregas
    self.model.f_fillamount = res.data[0].f_preamount
    self.model.f_lastremain_fillgas = self.model.f_pregas
    self.model.f_theremain_fillgas = self.model.f_lastremain_fillgas - self.model.f_pregas
  }).catch((error) => {
    self.$dispatch('error', '掉气补气', self.row, error)
  })
}
export default {
  title: '掉气补气',
  data () {
    return {
      config: {
        hasPrint : false,  //默认打票
        hasBillManage : false,  //默认不启用发票管理
        billType: '其他费用',   // 票据类型（燃气费，其他费用，调用的发票代码不同）
        printType: '普通收据', // 收据/电子票/专用发票/国税发票
        payment: '现金缴费',
        hasAcceptance: false // 是否需要打印受理单
      },
      model: {
        f_pregas: '',
        f_lastremain_fillgas: '',
        f_fillamount: '',
        f_theremain_fillgas: '',
        f_reason: '掉气补气',
        f_othereason: '',
        f_meter_brand: '',
        f_card_id: ''
      },
      hasValidateBill: false,
      validateOk: false,

      // 受理单
      acceptprint: false,
      acceptUrl: '',
      printData:{
        reason: '',
        fillgas:''
      },
      addGasReason: this.$appdata.getParam('补气原因')
    }
  },
  props: ['row'],
  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)
    })
  },
  watch: {
    // 'row' (val) {
    //   this.model.f_user_id = val.f_user_id
    //   this.model.f_userinfo_id = val.f_userinfo_id
    //   this.model.f_card_id = val.f_card_id
    //   this.model.f_meter_brand = val.f_meter_brand
    //   let data = {
    //     items: '*',
    //     tablename: 't_sellinggas',
    //     condition: 'f_user_id=' + "'" + this.model.f_user_id + "'" + 'and f_userinfo_id=' + "'" + this.model.f_userinfo_id + "'" + 'and (f_type=' + "'" + '卡表收费' + "'" + 'or f_type=' + "'" + '发卡售气' + "')" +"and f_state='有效'",
    //     orderitem: 'f_operate_date desc, id desc'
    //   }
    //   this.$SqlService.singleTableOrderBy(data).then((res) => {
    //     console.log('看下吧返回的是什么：' + JSON.stringify(res))
    //     this.model.f_pregas = res.data[0].f_pregas
    //     this.model.f_fillamount = res.data[0].f_preamount
    //     this.model.f_lastremain_fillgas = this.model.f_pregas
    //     this.model.f_theremain_fillgas = this.model.f_lastremain_fillgas - this.model.f_pregas
    //   }).catch((error) => {
    //     this.$dispatch('error', '掉气补气', this.row, error)
    //   })
    // }
  },
  methods: {
    close () {
      this.acceptprint = false
      // this.clean()
    },
    clear () {
      this.$dispatch('refresh')
    },
    accept () {
      // 打印受理单
      console.log('掉气补气model',this.model)
      console.log('受累单中row',this.row)
      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}'`
      console.log('受累单中printData',this.printData)
      this.acceptUrl = 'rs/report/acceptOffGasAddGas_bill'
      this.acceptprint = true
    },
    closeAccept () {
      this.acceptprint = false
    },
    confirm () {
      this.$dispatch('no-button')
      this.row.cardInfo = this.cardData
      this.$OffGasAddGasService.offGasAddGas(this.model, this.row).then((res) => {
        this.$dispatch('success', '掉气补气', this.row, res)
      }).catch((error) => {
        if (error.status === 603) {
          this.$error('重复提交')
          return
        }
        this.$dispatch('error', '掉气补气', this.row, error)
      })
    },
    clean () {
      this.$dispatch('cancelclean', this.row)
    }
  }
}
</script>

<style>
</style>
