<template>
  <div class="flex">
    <validator name='v'>
      <form novalidate class="form-horizontal flex">
        <p class="bg-info text-center" style="padding: 8px;">退费管理</p>

        <div class="row" style="margin-top:10px;">
          <label class = "font_normal_body">姓名</label>
          <div class="col-sm-2">
            <input id = "coo_refund_f_user_name" type="text" class= "input_search" v-model = "row.f_user_name" disabled value="">
          </div>
          <label class = "font_normal_body">电话</label>
          <div class="col-sm-2">
            <input id = "coo_refund_f_user_phone"    type="text" class= "input_search" v-model = "row.f_user_phone" disabled value="">
          </div>
          <label class = "font_normal_body">地址</label>
          <div class="col-sm-5">
            <input id = "coo_refund_f_address"    type="text" class= "input_search" v-model = "row.f_address" disabled value="">
          </div>
        </div>
        <div class="span" style="margin-top:10px;">
          <criteria-paged :model="refundata" v-ref:paged >
          <data-grid :model="model" partial='list' v-ref:grid class = "overflow:auto">
            <template partial='head'>
              <tr>
                 <th>序号</th>
                 <th>客户编号</th>
                 <th>上期指数</th>
                 <th>本期指数</th>
                 <th>欠费日期</th>
                 <th>应交气量</th>
                 <th>应交金额</th>
                 <!-- <th>账户余额</th>
                 <th>欠费金额</th> -->
                 <th>滞纳金</th>
              </tr>
            </template>
            <template partial='body'>
              <td style="text-align: center;">{{$index+1}}</td>
              <td style="text-align: center;">{{row.f_userinfo_id}}</td>
              <td style="text-align: center;">{{row.f_last_tablebase.toFixed(2)}}</td>
              <td style="text-align: center;">{{row.f_tablebase.toFixed(2)}}</td>
              <td style="text-align: center;">{{row.f_input_date}}</td>
              <td style="text-align: center;">{{row.f_oughtamount.toFixed(2)}}</td>
              <td style="text-align: center;">{{row.f_oughtfee.toFixed(2)}}</td>
              <!-- <td style="text-align: center;">{{row.f_balance.toFixed(2)}}</td>
              <td style="text-align: center;">{{row.qianfei.toFixed(2)}}</td> -->
              <td style="text-align: center;">{{row.f_deplay.toFixed(2)}}</td>
            </template>
          </data-grid>
          </criteria-paged>
        </div>
        <div class="row" style="margin-top:10px;">
          <label class = "font_normal_body">账户余额</label>
          <div class="col-sm-2">
            <input id = "coo_refund_f_balance"    type="text" class= "input_search" v-model = "row.f_balance" disabled value="">
          </div>
          <label class = "font_normal_body">欠费金额</label>
          <div class="col-sm-2">
            <input id = "coo_refund_f_qianfei"    type="text" class= "input_search" v-model= "(refundata.sums.f_oughtfee + refundata.sums.f_deplay) ? (refundata.sums.f_oughtfee + refundata.sums.f_deplay) : 0 " disabled value="">
          </div>
          <label  class = "font_normal_body "   v-if = "(refundata.sums.f_oughtfee + refundata.sums.f_deplay) > row.f_balance" style = "color:red;margin-left:10px">注: 欠费金额大于账户余额，无法退费</label>
        </div>
        <div class="row" style="margin-top:10px;" >
          <label class = "font_normal_body">打印格式</label>
          <div class="col-sm-2" id="ComprehenOperation-refMan-打印格式">
            <v-select
              v-model="model.printype"
              placeholder='打印格式'
              :value.sync="model.printype"
              :options='printype'
              condition=""
              close-on-select >
            </v-select>
          </div>
          <!-- <div class="col-sm-2">
            <input id = "coo_refund_"    type="text" class= "input_search" v-model = "" value="">
          </div> -->
          <label class = "font_normal_body">是否销户</label>
          <div class="col-sm-2" id="ComprehenOperation-refMan-是否销户">
            <v-select
              v-model="model.userdelete"
              placeholder='是否销户'
              :value.sync="model.userdelete"
              :options='userdele'
              close-on-select >
            </v-select>
          </div>
          <!-- <div class="col-sm-2">
            <input id = "coo_refund_"    type="text" class= "input_search" v-model = "" value="">
          </div> -->
          <div v-show = "(refundata.sums.f_oughtfee + refundata.sums.f_deplay) < row.f_balance"  :class="[$v.refundmoney.max || $v.refundmoney.min || $v.refundmoney.required ? 'has-error' : 'has-success']">
            <label class = "font_normal_body">退费金额</label>
            <div class="col-sm-2" v-if= "model.userdelete[0] === '是' ">
              <input type="number" class= "input_search" v-model= "model.refundmoney" v-validate:refundmoney='{max: row.f_balance - (refundata.sums.f_oughtfee + refundata.sums.f_deplay),min: 0, required: true}' disabled  value="{{row.f_balance - (refundata.sums.f_oughtfee + refundata.sums.f_deplay)}}" >
            </div>
            <div class="col-sm-2" v-else>
              <input type="number" class= "input_search" v-model= "model.refundmoney" v-validate:refundmoney='{max: row.f_balance - (refundata.sums.f_oughtfee + refundata.sums.f_deplay),min: 0, required: true}' value="">
            </div>

          </div>
        </div>
        <div v-show = "(refundata.sums.f_oughtfee + refundata.sums.f_deplay) < row.f_balance" class="row" style="margin-top:10px;">
          <div  :class="[$v.f_refundreason.required ? 'has-error' : 'has-success']">
            <label for="f_refundreason" class=" font_normal_body">退费原因:</label>
            <div class="col-sm-10">
              <textarea id = "coo_refund_refundreason" class="input_search" style="width:60%" v-model= "model.refundreason" v-validate:f_refundreason='{required: true }' rows="1" placeholder="请填写原因"></textarea>
            </div>
          </div>
        </div>
      </form>
      <!-- <print-bill :show="print" v-ref:printbill :data='billdata' v-on:toggle="close"></print-bill> -->
    </validator>
    <div style="text-align:right;height:auto;margin-top:30px;">
      <button class="btn btn-success" @click="confirm()"  :disabled='!$v.valid || refundata.sums.f_oughtfee > row.f_balance' id="ComprehenOperation-refMan-确认">确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;认</button>
      <button class="btn btn-default" @click="clean()" style="width:80px;margin-left:20px;" id="ComprehenOperation-refMan-取消">取&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;消</button>
    </div>
  </div>
</template>
<script>
/**
*综合业务
*退费管理组件
*/
import { PagedList } from 'vue-client'
export default {
  title: '退费管理',
  data () {
    return {
      refundata: '',
      print: false,
      billdata: {
        logicmodel: {}
      },
      model: {
        printype: '普通收据',
        userdelete: '否'
      },

      printype: this.$appdata.getParam('打印格式'),
      userdele: this.$appdata.getParam('是否')
    }
  },
  props: ['row'],
  ready () {
    this.getrefundata()
  },
  methods: {
    // 查询欠费的记录
    getrefundata () {
      this.refundata = new PagedList('rs/sql/getArreaUser', 10, {condition: `"ss.f_userinfo_id = ${this.row.f_userinfo_id}"`}, {f_oughtamount: '', f_oughtfee: '', f_deplay: ''})
      this.refundata.search()
    },
    confirm () {
      // 整理数据 发送请求
      let data = this.sortdata()
      this.$resetpost('rs/logic/refundManage', data, {resolveMsg: '退费成功', rejectMsg: '退费失败'})
      .then((res) => {
        let backdata = res.data
        this.billdata = Object.assign(this.billdata, this.row)
        this.billdata.id = backdata.id
        this.billdata.f_bill_type = '用户退费'
        this.billdata.f_bill_style = this.model.printype
        this.billdata.logicmodel.f_preamount = this.model.refundmoney
        this.billdata.logicmodel.f_collection = this.model.refundmoney
        this.billdata.logicmodel.f_print = this.model.printype
        // this.print = true
        // this.$refs.printbill.bill.search(backdata.id)
        this.$dispatch('success', '用户退费', this.row, res)
      }).catch((error) => {
        if (error.status === 603) {
          this.$error('重复提交')
          return
        }
        // this.$dispatch('error', '用户退费', this.row, error)
      })
    },
    clean () {
      this.$dispatch('clean', '退费管理', this.row)
    },
    sortdata () {
      // 用户表余额 用户状态 更改
      let user = {
        f_userinfo_id: this.row.f_userinfo_id,
        f_balance: this.row.f_balance - this.model.refundmoney,
        version: this.row.userinfo_version
      }

      // 如果有销户操作，产生销户数据
      let cancel
      if (this.model.userdelete[0] === '是') {
        // 用户数据中 更改用户状态为销户
        user.f_user_state = '销户'
        // 产生的销户表数据 和 记录表数据
        cancel = {
          userinfo: this.row.f_userinfo_id,
          f_user_id: this.row.f_user_id,
          f_user_name: this.row.f_user_name,
          f_state: '有效',
          f_comments: '用户退费, 同时销户',
          f_operator: this.$login.f.name,
          // 同时产生 记录数据
          f_operat_type: '销户',
          f_describe: `对客户${this.row.f_user_name}进行退费, 同时进行销户操作`,
          f_state: '有效',
          f_filiale: this.$login.f.f_fengongsi,
          f_outlets: this.$login.f.f_parentname,
          f_orgstr: this.$login.f.orgpathstr,
          f_filialeids : this.$login.f.f_orgids
        }
      }

      // sellinggas 收费表数据
      let sellgas = {
        f_balance: this.row.f_balance,
        f_curbalance: this.row.f_balance - this.model.refundmoney,
        f_tablebase: 0,
        f_last_tablebase: 0,
        f_meter_type: this.row.f_meter_classify,
        f_payment: '现金缴费',
        f_price_id: this.row.f_price_id,
        f_stairprice_id: this.row.f_stairprice_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_address: this.row.f_address,
        f_userinfo_id: this.row.f_userinfo_id,
        f_meter_brand: this.row.f_meter_brand,
        f_meter_style: this.row.f_meter_style,
        f_meternumber: this.row.f_meternumber,
        f_card_id: this.row.f_card_id,
        f_collection: (-this.model.refundmoney),
        f_operator: this.$login.f.name,
        f_filiale: this.$login.f.f_fengongsi,
        f_outlets: this.$login.f.f_parentname,
        f_orgstr: this.$login.f.orgpathstr,
        f_filialeids : this.$login.f.f_orgids,
        f_type: '用户退费',
        f_state: '有效',
        f_comments: this.model.refundreason,
        // 记录表数据
        f_operat_type: '用户退费',
        f_describe: `对用户${this.row.f_user_name}进行退费, 退费金额为${this.model.refundmoney}。`,
        f_state: '有效'
      }
      let data = {user: user, sellgas: sellgas, model: this.model, cancel: cancel}
      return data
    }
  }
}
</script>

<style>
</style>
