<template>
  <div class="auto">
    <validator name='v'>
      <form novalidate class="form-horizontal">
      <!-- <p class="bg-info text-center" style="padding: 8px;">收费信息</p> -->
      <ul class="nav nav-tabs">
        <li class="active"><a href="#">收费信息</a></li>
      </ul>
      <div class="row" style="margin-top:10px;">
        <div class="col-sm-4 form-input-group">
            <label for="f_payment" class="col-sm-2 control-label">付款方式:</label>
              <v-select id="f_payment"
              v-model="model.f_payment"
              placeholder='请选择'
              :value.sync="model.f_payment"
              :options='paytype'
              close-on-select clear-button>
            </v-select>
        </div>
        <div class="col-sm-4 form-input-group">
          <label for="f_pregas" class=" col-sm-2 control-label">超用气量:</label>
          <div class="col-sm-2">
            <input  class="form-control" type="number" v-model="model.f_pregas" @blur="overusegas()" v-scale = "[model.f_pregas, 2]">
          </div>
        </div>
        <div class="col-sm-4 form-input-group">
          <label for="f_preamount" class=" col-sm-2 control-label">超用金额:</label>
          <input  class="form-control" type="number" v-model="model.f_preamount.toFixed(2)" disabled>
        </div>
      </div>
      <div class="row" style="margin-top:10px;">
        <div class="form-input-group col-sm-4">
          <label for="f_print" class="control-label col-sm-2">打印格式:</label>
          <div class="col-sm-2">
            <v-select id="print"
                      v-model="f_print"
                      placeholder='请选择'
                      :multiple="mulPrint"
                      :value.sync="model.f_print"
                      :options='printstyle'
                      close-on-select clear-button>
            </v-select>
          </div>
        </div>
        <div class="form-group form-input-group col-sm-4" v-if="model.f_payment[0].includes('POS')">
          <label for="f_voucher_number" class="control-label col-sm-2">凭证号:</label>
          <input type="text" class="form-control col-sm-2" v-model="model.f_voucher_number" placeholder="凭证号">
        </div>
        <div class="form-group form-input-group col-sm-4" v-if="!model.f_payment[0].includes('POS')">
        </div>
        <div class="form-input-group col-sm-4">
          <label for="f_totalcost" class=" col-sm-2 control-label">应交金额:</label>
          <input  class="form-control" type="number" v-model="model.f_totalcost.toFixed(2)" disabled>
        </div>
      </div>
      <div class="row" style="margin-top:10px;">
        <div class="form-input-group col-sm-4">
          <label for="f_totalcost" class=" col-sm-2 control-label">上期结余:</label>
          <input  class="form-control" type="number" v-model="row.f_balance" disabled>
        </div>
        <div class="form-input-group col-sm-4">
          <label for="f_totalcost" class=" col-sm-2 control-label">本期结余:</label>
          <input  class="form-control" type="number" v-model="f_curbalance" disabled>
        </div>
        <div  class="form-input-group col-sm-4" :class="[$v.f_collection.required ? 'has-error' : 'has-success']">
          <label for="f_collection" class="col-sm-2 control-label">收款:</label>
          <div class="col-sm-2">
            <input id="f_collection" class="form-control" type="number" v-model="model.f_collection"
            v-validate:f_collection='{  required: true, dctest: [ model.f_totalcost, ">=" ] }'
             v-scale = "[model.f_collection, 2]">
            <span v-if="$v.f_collection.dctest && model.f_pregas && model.f_preamount "
            style="color:red ">收款不能小于应收金额</span>
          </div>
        </div>
      </div>
      <div class="row" style="margin-top:10px;" v-if="calculatedetail">
        <div class="col-sm-4 col-sm-offset-7">
          <strong>计算公式: {{calculatedetail}}</strong>
        </div>
      </div>
    </form>
      <print-bill :show="print" :bill-manager='config.hasBillManage' :bill-url='billUrl' v-on:toggle="close" :data='row' @printok="printok" v-ref:printbill></print-bill>
    <div style="text-align:right;margin-top:30px;margin-right:50px;">
      <button class="btn btn-success" @click="confirm()"  title="演示系统暂不支持" :disabled="true">确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;认</button>
      <button class="btn btn-default" @click="clean()" style="width:80px;margin-left:20px;">取&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;消</button>
    </div>
    </validator>
  </div>
</template>
<script>
/**
*综合业务
*超用收费组件
*/

let getOverUseGen = async function(self) {
  await self.$getConfig(self,'OverUseCharge')
  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.mulPrint = self.config.printType instanceof Array
  self.model = Object.assign({}, self.model, self.row)
  let condition = 'f_user_id=' + "'" + self.row.f_user_id + "'" + 'and f_whether_pay = ' + "'" + '否' + "'" + 'and f_overuse_state = ' + "'" + '有效' + "'"
  let getData = await self.$resetpost('rs/sql/singleTable_OrderBy', {data: {items: '*', tablename: 't_overuse', condition: condition, orderitem: 'f_operate_date desc'}}, {resolveMsg: null, rejectMsg: '获取超用记录时出错！！'})
  console.log('获取超用收费', getData)
  if (getData.data && getData.data.length > 0) {
    self.model.f_pregas = getData.data[0].f_overuseamount
    self.model.f_preamount = getData.data[0].f_overusefee
    // 计算单价，超用采用一阶价格，在计算超用时已经计算，此处用金额和气量反算单价
    self.model.chargeprice = [{
      f_price: ((self.model.f_preamount - 0)/(self.model.f_pregas - 0)).toFixed(2),
      f_gas: self.model.f_pregas,
      f_money: self.model.f_preamount,
      f_price_name: '阶梯1'
    }]
    self.model.f_totalcost = self.model.f_balance > getData.data[0].f_overusefee ? 0 : getData.data[0].f_overusefee - self.model.f_balance
    self.model.f_collection = self.model.f_totalcost
    self.model.over_id = getData.data[0].id
  } else {
    self.$showAlert('该用户没有超用记录，请核实！！！', 'warning', 3000)
  }

}

let overUseGasGen = async function (self) {
  let res = await self.$CommonService.gasCalculate(self.model, self.model.f_pregas)
  self.model.f_preamount = (res.data.f_stair1price - 0) * (self.model.f_pregas - 0)
  self.model.f_totalcost = self.model.f_balance > self.model.f_preamount ? 0 : self.model.f_preamount - self.model.f_balance
  self.model.f_collection = self.model.f_totalcost
  self.model.chargeprice = [{
    f_price: (res.data.f_stair1price - 0),
    f_gas: self.model.f_pregas,
    f_money: self.model.f_preamount,
    f_price_name: '阶梯1'
  }]
  self.calculatedetail = res.data.f_stair1price + ' x ' + self.model.f_pregas
}

let overChargeGen = async function (self) {
  try {
    let res = await self.$ChangeMeterService.overCharge(self.model, self.row)
    console.log('超用收费返回id', res)
    if (self.config.hasPrint) {

      if (self.model.f_print.indexOf('普通收据') !== -1) {
        if (self.config.hasBillManage) {
          // 启用发票管理，获取票据管理中的票号并存储记录
          self.row.id = res.data.id
          self.row.f_bill_type = '超用收费'
          self.row.f_bill_style = self.model.f_print[0]
          self.billUrl = 'rs/report/overuse_bill'
          self.print = true
        } else {
          self.row.id = res.data.id
          self.billUrl = 'rs/report/overuse_bill'
          self.print = true
        }
      } else {
        if (self.model.f_print[0] === '电子发票') {
          self.$CommonService.openEticket(res.data.id, '超用收费')
          self.$dispatch('success')
        }
      }
    } else {
      self.$dispatch('success')
    }
  } catch (error) {
    self.$showAlert(error, 'danger', 0)
    self.$dispatch('refresh')
  }
}

export default {
  title: '超用收费',
  data () {
    return {
      config: {
        hasPrint : false,  //默认打票
        hasBillManage :false,  //默认不启用发票管理
        printType: '普通收据', // 收据/电子票/专用发票/国税发票
        payment: '现金缴费'
      },
      disable: false,
      total: 0,
      model: {
        f_pregas: 0,
        f_preamount: 0,
        f_totalcost: 0,
        f_collection: 0,
        f_balance: '',
        f_curbalance: '',
        f_payment: '现金缴费',
        f_print: [],
        f_voucher_number: ''
      },
      f_curbalance: 0,
      cal: false,
      calculatedetail: '',
      billUrl: '',
      print: false,

      mulPrint: false,
      paytype: this.$appdata.getParam('付款方式'),
      printstyle: this.$appdata.getParam('打印格式')
    }
  },
  props: ['row'],
  ready () {
    // 获取超用记录并赋值
    getOverUseGen(this)
  },
  methods: {
    confirm () {
      this.model.f_curbalance = this.f_curbalance
      overChargeGen(this)
    },
    clean () {
      this.$dispatch('clean', this.row)
    },
    overusegas () {
      if (this.model.f_pregas) {
        overUseGasGen(this)
      }
    },
    close () {
      this.showprint = false
      this.print = false
      this.clean()
    },
    printok () {
      // 收据打完，判断是否还有其他票据进行请求
      for (let i = 0; i < this.model.f_print.length; i++) {
        if (this.model.f_print[i] === '电子发票') {
          this.$CommonService.openEticket(this.row.id, '超用收费')
        }
      }
      this.$dispatch('success')
    },
  },
  computed: {
    'f_curbalance' () {
      if (this.model.f_preamount) {
        return (this.model.f_collection - this.model.f_totalcost).toFixed(2)
      } else {
        return 0
      }
    }
  }
}
</script>

<style>
</style>
