<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">&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"></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>
        <div class="row" style="margin-top:6px;">
          <div class="col-sm-4" :class="[$v.printstyle.required ? 'has-error' : '']">
            <label for="f_print" class="font_normal_body">打印格式</label>
            <input type="text" v-show="false" v-model="$refs.printstyle.selectedItems"
                   v-validate:printstyle='{required: true }'>
            <v-select id="print"
                      v-model="f_print"
                      placeholder='请选择'
                      :multiple="mulPrint"
                      :value.sync="model.f_print"
                      :options='printstyle'
                      close-on-select clear-button v-ref:printstyle>
            </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>
      <validate-bill v-if="hasValidateBill" :data="model" @validate-bill="validateBill"></validate-bill>
      <print-bill :show="print" :bill-config='config' :bill-data='billData' :data='row' @toggle="printCancel" @printok="printok"  v-ref:printbill></print-bill>
      <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' v-on:toggle="clean" @printok="printok" :data='printModel'  v-ref:printbill></print-bill>-->
  </div>
  <upload :blodid="row.f_userinfo_id"  v-ref:upload    :isremark="true" fusetype="赠气"></upload>

</template>


<script>
  import Vue from 'vue'
  let asyncIOTMeterTable = async function (self) {
    await self.$getConfig(self, 'IotChangeFill')
    console.log('物联网表赠气费config', self.config)
    // 默认打印格式
    self.model.f_print = self.config.printType instanceof Array ? self.config.printType : [self.config.printType]
    self.model.f_payment = [self.config.payment]

    self.model.f_use_type = self.config.billType
    self.model.f_bill_type = self.model.f_print
    self.hasValidateBill = self.config.hasBillManage
  }
export default {
    title: '赠气赠费',
    data () {
      return {
        blodid: '',
        showupload: false,
        config: {
          hasPrint: false,
          hasBillManage: true,
          billType: '燃气费',
          printType: '普通收据'
        },
        operator_type: [{label: '赠费', value: '物联网赠费'}],
        operator_type2: [{label: '赠气', value: '物联网赠气'}],
        model: {
          f_operat_type: '',
          f_pregas: 0,
          f_preamount: 0,
          f_print: [],
          f_comments: ''
        },
        eticket_msg: false,
        eticket_show: false,
        invoice_is_pax: '不征税',
        hasValidateBill: false,
        validateOk: false,

        print: false,
        mulPrint: false,
        billData: {
          url: 'rs/report/fill_gas_send_bill',
          bill: ''
        },
        printstyle: this.$appdata.getParam('打印格式'),
      }
    },
    props: ['row'],
    ready() {
      if (this.row.f_collection_type === '按气量') {
        this.model.f_operat_type = ['物联网赠气']
      } else {
        this.model.f_operat_type = ['物联网赠费']
      }
      asyncIOTMeterTable(this)
    },
    methods: {
      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_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_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)
        let res = await  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}失败！！请重试！！`})

        console.log('开始打票', this.config)
        if (this.config.hasPrint) {
          if (this.model.f_print!= '电子发票') {
            console.log('this.config.hasBillManage', this.config.hasBillManage)
            if (this.config.hasBillManage) {
              // 启用发票管理，获取票据管理中的票号并存储记录
              this.row.id = res.data.id
              this.row.f_bill_type = this.model.f_operat_type
              this.row.f_bill_style = this.model.f_print[0]
              this.print = true
            } else {
              this.row.id = res.data.id
              this.print = true
            }
          } else if (this.config.printType === '国税发票') {
            // TODO
          } else if (this.model.f_print[0] === '电子发票') {
            // TODO
          }
        } else {
          this.clean()
        }
      },
      printCancel () {
        this.$dispatch('refresh')
      },
      close () {
        this.print = false
        this.clean()
      },
      validateBill (val) {
        this.validateOk = !val.isOk
        this.billData.bill = val.bill
      },
      printok () {
        this.$dispatch('success')
      },
      clean () {
        this.$dispatch('refresh')
      }
    }
  }
</script>
