<template>
  <div class="flex-row">
    <div class="span" style="overflow-y: auto;">
      <validator name='v'>
        <form novalidate class="form-horizontal">
          <div class="row">
            <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.row.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.row.f_collection_type==='按气量'" :value.sync="model.f_operat_type"
                        :options='operator_type2' placeholder='请选择'
                        selected="赠气"
                        style="width:60%"
                        close-on-select></v-select>
            </div>
            <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"></div>

            <div v-if="this.row.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.row.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 class="col-sm-4" :class="[$v.f_gift_gas_reason.required ? 'has-error select-error' : '']">
              <label class="font_normal_body" v-show="model.f_operat_type[0] === '物联网赠气'" title="参数：赠气/费原因">赠气原因</label>
              <label class="font_normal_body" v-show="model.f_operat_type[0] === '物联网赠费'" title="参数：赠气/费原因">赠费原因</label>
              <input v-show="false" v-model="model.f_gift_gas_reason" v-validate:f_gift_gas_reason='{required: true}'>
              <v-select :value.sync="model.f_gift_gas_reason"
                        value-single
                        :options='giftGasReason'
                        placeholder='请选择'
                        selected="是否计入累购"
                        style="width:60%"
                        close-on-select>
              </v-select>
            </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>
      <!--<validate-bill v-if="hasValidateBill" :data="model" @validate-bill="validateBill"></validate-bill>-->
      <!--<print-bill :show="print" :bill-manager='config.hasBillManage' v-ref:printbill :bill-url='billUrl' v-on:toggle="clean" @printok="printok" :data='printModel'></print-bill>-->
      <print-bill :show="print" :bill-config='config' :bill-data='billData' :data='row' @toggle="close"
                  @printok="printok"
                  v-ref:printbill></print-bill>

      <upload :blodid="row.f_userinfo_id" v-ref:upload :businessid="randomBusinessId" :isremark="true"
              fusetype="赠气"></upload>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'

let readyGen = async function (self) {
  await self.$getConfig(self, 'iotTable')
  if (self.config.addgas) {
    self.model.f_add_gas = '计入'
  } else {
    self.model.f_add_gas = '计入'
  }
}
export default {
  title: '赠气赠费',
  data () {
    return {
      blodid: '',
      showupload: false,
      randomBusinessId: '',
      addGas: [{label: '计入', value: '计入'}, {label: '不计入', value: '不计入'}],
      resid: [],  // 存放新增的f_files表中id
      operator_type: [{label: '赠费', value: '物联网赠费'}],
      operator_type2: [{label: '赠气', value: '物联网赠气'}],
      giftGasReason: this.$appdata.getParam('赠气/费原因') ? this.$appdata.getParam('赠气/费原因') : [],
      model: {
        f_operat_type: '',
        f_pregas: 0,
        f_preamount: 0,
        f_add_gas: '计入',
        f_comments: '',
        f_gift_gas_reason: ''
      },
      config: {
        hasPrint: false
      },
      billData: {
        url: 'rs/report/iotGiftGas',
        operator: this.$login.f.name,
        billnumber: ''
      },
      print: false

    }
  },
  props: ['row'],
  ready () {
    readyGen(this)
    // this.blodid = this.row.f_userinfo_id
    this.getRandomId()
    if (this.row.f_collection_type === '按气量') {
      this.model.f_operat_type = ['物联网赠气']
      this.billData.url = 'rs/report/iotGiftGas'
    } else {
      this.model.f_operat_type = ['物联网赠费']
      this.billData.url = 'rs/report/iotGiftFee'
    }
  },
  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_userfiles_id: this.row.f_userfiles_id,
          f_userinfo_id: this.row.f_userinfo_id,
          f_user_id: this.row.f_user_id,
          f_user_name: this.row.f_user_name,
          f_user_type: this.row.f_user_type,
          f_add_gas: this.model.f_add_gas,
          f_gasproperties: this.row.f_gasproperties,
          f_meter_type: this.row.f_meter_type,
          f_pregas: this.model.f_pregas,
          f_address: this.row.f_address,
          f_meter_brand: this.row.f_meter_brand,
          f_preamount: this.model.f_preamount,
          f_type: this.model.f_operat_type[0],
          f_comments: this.model.f_comments,
          f_gift_gas_reason: this.model.f_gift_gas_reason,
          f_billing: this.row.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.row.f_card_id,
          f_balance: this.row.f_balance_amount,
          f_operat_type: this.model.f_operat_type[0],
          f_describe: `${this.$login.f.name}对用户${this.row.f_user_name}进行${this.model.f_operat_type}操作`,
          f_state: '有效'
          // f_table_state:'有效'
        }
      }
      console.log(param)
      this.$resetpost('rs/logic/fedWithgas', {data: param}, {
        warnMsg: `确定要对用户${this.row.f_user_name}进行${this.model.f_operat_type}吗？`,
        resolveMsg: `${this.model.f_operat_type}成功！！`,
        rejectMsg: `${this.model.f_operat_type}失败！！请重试！！`
      }).then((res) => {
        console.log('看一下赠气赠费返回的数据======>', res)
        let param = []
        for (let row of this.resid) {
          param.push({id: row.id})
        }
        let data = {
          param: param,
          f_blobid: res.data.id
        }
        this.$resetpost('rs/logic/updatefiles', data)
        if (this.config.hasPrint) {
          this.row.id = res.data.id
          this.row.f_bill_type = this.row.f_collection_type === '按气量' ? '物联网赠气' : '物联网赠费'
          this.row.f_bill_style = '普通收据'
          this.print = true
        } else {
          this.printok()
        }
      })
    },
    printok () {
      this.$dispatch('success', '物联网赠费')
    },
    clean () {
      this.$dispatch('refresh')
    }
  }
}
</script>
