<template>
  <div class="auto" id="GarbageCharge">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <div class="row">
          <div class="col-sm-4 form-group" :class="[$v.payment.required ? 'select-error' : '']">
            <label class="font_normal_body" title="参数名称：付款方式">收款方式</label>
            <input type="text" v-show="false" v-model="$refs.payment.selectedItems"
                   v-validate:payment='{required: true }'>
            <v-select v-model="model.f_payment"
                      placeholder='请选择'
                      style="width:60%"
                      :value.sync="model.f_payment"
                      :options='paytype'
                      v-ref:payment
                      close-on-select clear-button>
            </v-select>
          </div>
          <div class="col-sm-4 form-group">
            <label for="f_print" class="font_normal_body">打印格式</label>
            <v-select id="print"
                      v-model="f_print"
                      style="width:60%"
                      placeholder='请选择'
                      :value.sync="model.f_print"
                      :options='printstyle'
                      close-on-select clear-button>
            </v-select>
          </div>
          <div class="col-sm-4 form-group">
            <label class="font_normal_body">总&ensp;欠&ensp;费</label>
            <input class="input_search" style="width:60%"  type="number" v-model="money" placeholder="总欠费" disabled>
          </div>
        </div>
        <div class="row">
          <div class="panel panel-info auto">
            <div class="panel-body">
              <data-grid :model="list" partial='list' class="list_area table_sy" v-ref:grid>
                <template partial='head'>
                  <tr>
                    <th><nobr>序号</nobr></th>
                    <th><nobr>欠费月份</nobr></th>
                    <th><nobr>金额</nobr></th>
                  </tr>
                </template>
                <template partial='body'>
                  <tr>
                    <td>{{$index + 1}}</td>
                    <td>{{row.f_hand_date}}</td>
                    <td>{{row.f_money}}</td>
                  </tr>
                </template>
              </data-grid>
            </div>
          </div>
        </div>
      </form>
      <div style="text-align:right;">
        <button class="button_search btn-gn" @click="confirm()" :disabled='!$v.valid'>确认</button>
        <button class="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-config='config' :bill-data='billData' v-on:toggle="clean" @printok="printok"
                :data='printModel' v-ref:printbill></print-bill>
  </div>
<!--  <upload :blodid="data.f_userinfo_id" isremark="true" fusetype="退费管理"></upload>-->
</template>


<script>
let readyGen = async function (self) {
  await self.$getConfig(self, 'GarbageCharge')
  await self.getList()
  // 默认打印格式
  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 {
      config: {
        showupload: false, // 默认不上传附件
        hasPrint: false,  // 默认打票
        hasBillManage: false,  // 默认不启用发票管理
        overBalance: false,   // 为true时退费金额可以大于余额
        printType: '普通收据', // 收据/电子票/专用发票/国税发票
        payment: '现金',
        billType: '燃气费'
      },
      model: {
        f_refund_fee: '',
        f_refund_gas: '',
        f_comments: '',
        f_payment: '',
        f_print: '',
        f_price: 0
      },
      blodid: '',
      hasValidateBill: false,
      validateOk: false,

      printModel: {},
      print: false,
      billUrl: '',
      billData: {
        url: 'rs/report/refund_sell',
        bill: ''
      },
      // 下拉框值
      paytype: this.$appdata.getParam('付款方式'),
      printstyle: this.$appdata.getParam('打印格式'),
      list: {data: []},
      money: 0
    }
  },
  props: ['row'],
  ready () {
    readyGen(this)
  },
  methods: {
    async getList () {
      let res = await this.$resetpost('rs/logic/queryGarbageCharges', {f_userfiles_id: this.row.f_userfiles_id}, {resolveMsg: null,rejectMsg: '查询垃圾费异常'})
      this.list.data = res.data.list
      this.money = res.data.money
    },
    async confirm () {
      if (this.money <= 0) {
        this.$showMessage('收费金额不能小于等于0')
        return
      }
      let param = {
        f_userfiles_id: this.row.f_userfiles_id,
        money: this.money,
        number: this.list.data.length,
        f_operator: this.$login.f.name,
        f_operatorid: this.$login.f.id,
        f_orgid: this.$login.f.orgid,
        f_orgname: this.$login.f.orgs,
        f_depid: this.$login.f.depids,
        f_depname: this.$login.f.deps,
        f_payment: this.model.f_payment[0]
      }
      console.log('垃圾费收费', param)
      let res = await this.$resetpost('rs/logic/garbageCharges', {data: param}, {warnMsg: `确定要对用户${this.row.f_user_name}进行垃圾费收费吗？`, resolveMsg: '收费成功！！', rejectMsg: '收费失败！！请重试！！'})
      console.log('垃圾费收费', res)
      if (this.config.hasPrint) {
        if (this.model.f_print.indexOf('电子发票') == -1) {
          console.log('this.config.hasBillManage', this.config.hasBillManage)
          if (this.config.hasBillManage) {
            // 启用发票管理，获取票据管理中的票号并存储记录
            this.printModel.id = res.data.id
            this.printModel.f_bill_type = '退费'
            this.printModel.f_bill_style = this.model.f_print[0]
            // this.billUrl = 'rs/report/refund_sell'
            this.print = true
          } else {
            this.printModel.id = res.data.id
            // this.billUrl = 'rs/report/refund_sell'
            this.print = true
          }
        } else if (this.config.printType === '国税发票') {
          // TODO
          this.$dispatch('success')
        } else if (this.config.printType === '电子发票') {
          // TODO
          this.$dispatch('success')
        }
      } else {
        this.$dispatch('success')
      }
    },
    clean () {
      this.$dispatch('refresh')
    },
    printok () {
      this.$dispatch('success')
    },
    validateBill (val) {
      this.validateOk = !val.isOk
      this.billData.bill = val.bill
    }
  },
  watch: {
  }
}
</script>
<style lang="less">
#GarbageCharge {
  tr {
    th {
      text-align: center;
    }

    td {
      text-align: center;
    }
  }
}
</style>
