<template>
  <div class="auto">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <div  class="row">
          <div class="col-sm-4 form-group">
            <label class="font_normal_body" title="参数值:计入/不计入，计入:更新档案结余及累购，不计入:只保存记录不对档案进行操作">计入累购</label>
            <input v-show="false" v-model="model.f_add_gas" v-validate:f_operat_type='{required: true}'>
            <v-select :value.sync="model.f_add_gas" value-single
                      :options='addGas' placeholder='请选择'
                      selected="是否计入累购"
                      style="width:60%"
                      close-on-select></v-select>
          </div>

          <div class="col-sm-4 form-group">
            <label class="font_normal_body">操&nbsp;&nbsp;作</label>
            <input v-show="false" v-model="model.f_operat_type"
                   v-validate:f_operat_type='{required: true}'>
            <v-select v-if="this.data.f_collection_type==='按金额'" :value.sync="model.f_operat_type"
                      :options='operator_type' placeholder='请选择'
                      selected="补费"
                      style="width:60%"
                      close-on-select></v-select>
            <v-select v-if="this.data.f_collection_type==='按气量'" :value.sync="model.f_operat_type"
                      :options='operator_type2' placeholder='请选择'
                      selected="补气"
                      style="width:60%"
                      close-on-select></v-select>
          </div>
          <div v-if="this.data.f_collection_type==='按金额'" class="col-sm-4 form-group"
               :class="[$v.f_preamount.required || $v.f_preamount.dctest ? 'has-error' : 'has-success']">
            <label class="font_normal_body">&nbsp;金额&nbsp;</label>
            <input class="input_search" type="number" v-model="model.f_preamount" style="width:60%"
                   v-validate:f_preamount='{required: true,  dctest: [0, ">" ]}' placeholder="金额">
          </div>
          <div v-if="this.data.f_collection_type==='按气量'" class="col-sm-4 form-group"
               :class="[$v.f_pregas.required || $v.f_pregas.dctest ? 'has-error' : 'has-success']">
            <label class="font_normal_body">&nbsp;气量&nbsp;</label>
            <input class="input_search" type="number" v-model="model.f_pregas" style="width:60%"
                   v-validate:f_pregas='{required: true,  dctest: [0, ">" ]}' placeholder="气量">
          </div>
        </div>

        <div class="row">
          <div class="col-sm-12">
            <label for="f_paper_account" class="font_normal_body ">备&nbsp;&nbsp;注</label>
            <input class="input_search" style="width:87%" rows="1" v-model="model.f_comments"></input>
          </div>
        </div>
      </form>
      <div style="text-align:right;">
        <button class="btn button_search btn-gn" @click="confirm()" :disabled='!$v.valid'>确认</button>
        <button class="btn button_clear btn-gn" @click="clean()">取消</button>
      </div>
    </validator>
    <print-bill :show="print" :bill-config='config' :bill-data='billData' :data='data' @toggle="close" @printok="printok"  v-ref:printbill></print-bill>
  </div>
  <upload :blodid="data.f_userinfo_id" v-if="true" :businessid="randomBusinessId" isremark="true" fusetype="补气"></upload>
</template>


<script>
  import Vue from 'vue'
  let asyncfeeDeduction = async function (self) {
    await self.$getConfig(self, 'feeDeduction')
    console.log('物联网补费扣费config', self.config)
    if (self.config.addgas) {
      self.model.f_add_gas = '计入'
    } else {
      self.model.f_add_gas = '计入'
    }
  }
export default {
    title: '物联网表补费扣费',
    data () {
      return {
        blodid: '',
        randomBusinessId: '',
        resid: [],  // 存放新增的f_files表中id
        showupload: false,
        config: {
          addgas: false,
          hasPrint: false
        },
        addGas: [{label: '计入', value: '计入'}, {label: '不计入', value: '不计入'}],
        operator_type: [{label: '补费', value: '物联网补费'}, {label: '扣费', value: '物联网扣费'}],
        operator_type2: [{label: '补气', value: '物联网补气'}, {label: '扣气', value: '物联网扣气'}],
        print: false,
        billData: {
          url: 'rs/report/MakeUp',
          operator: this.$login.f.name,
          billnumber: ''
        },
        model: {
          f_operat_type: '',
          f_pregas: 0,
          f_preamount: 0,
          f_comments: ''
        }
      }
    },
    props: ['data'],
    ready () {
      if (this.data.f_collection_type === '按气量') {
        this.model.f_operat_type = ['物联网补费']
      } else {
        this.model.f_operat_type = ['物联网补费']
      }
      this.getRandomId()
      asyncfeeDeduction(this)
    },
    events: {
      // 删除Resid数组元素
      'delResid' (val) {
        this.resid.$remove({id: val, f_biobid: ''})
        // this.resid.splice(this.resid.indexOf({id:val,f_biobid:''}),1);
      },
      // 增加Resid数组元素
      'resid' (val) {
        this.resid.push({id: val, f_biobid: ''})
      }
    },
    methods: {
      getRandomId () {
        this.randomBusinessId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
          let res = Math.random() * 16 | 0
          let v = c == 'x' ? res : (res & 0x3 | 0x8)
          return v.toString(16)
        })
      },
      async confirm () {
        let param = {
          fillgas: {
            f_add_gas: this.model.f_add_gas,
            f_userinfo_id: this.data.f_userinfo_id,
            f_user_id: this.data.f_user_id,
            f_calculation:this.data.f_calculation,
            f_userfiles_id: this.data.f_userfiles_id,
            f_user_name: this.data.f_user_name,
            f_user_type: this.data.f_user_type,
            f_gasproperties: this.data.f_gasproperties,
            f_meter_type: this.data.f_meter_type,
            f_pregas: this.model.f_pregas,
            f_address: this.data.f_address,
            f_meter_brand: this.data.f_meter_brand,
            f_preamount: this.model.f_preamount,
            f_type: this.model.f_operat_type[0],
            f_comments: this.model.f_comments,
            f_billing: this.data.f_billing,
            f_operator: Vue.$login.f.name,
            f_operatorid: Vue.$login.f.id,
            f_orgid: Vue.$login.f.orgid,
            f_orgname: Vue.$login.f.orgs,
            f_depid: Vue.$login.f.depids,
            f_depname: Vue.$login.f.deps,
            f_card_id: this.data.f_card_id,
            f_balance: this.data.f_balance_amount,
            f_operat_type: this.model.f_operat_type[0],
            f_describe: `${this.$login.f.name}对用户${this.data.f_user_name}进行${this.model.f_operat_type}操作`,
            f_state: '有效'
          }
        }
        this.$resetpost('rs/logic/feededuction', {data: param}, {
          warnMsg: `确定要对用户${this.data.f_user_name}进行${this.model.f_operat_type}吗？`,
          resolveMsg: `${this.model.f_operat_type}成功！！`,
          rejectMsg: `${this.model.f_operat_type}失败！！请重试！！`
        }).then((res) => {
          let param = []
          for (let row of this.resid) {
            param.push({id: row.id})
          }
          let data1 = {
            param: param,
            f_blobid: res.data.id
          }
          console.log('-------------------------->', data1)
          this.$resetpost('rs/logic/updatefiles', data1)
          if (this.config.hasPrint) {
            this.data.id = res.data.id
            this.data.f_bill_type = '补费扣费'
            this.data.f_bill_style = '普通收据'
            this.print = true
          } else {
            this.clean()
          }
        })
      },
      clean () {
        this.$dispatch('refresh')
      },
      printok () {
        this.$dispatch('success')
      },
      close () {
        this.print = false
        this.clean()
      },
    }
  }
</script>
