<template>
  <div class="auto">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <div class="row">
          <div class="col-sm-4 form-group"
               :class="[$v.f_type.required ? 'has-error' : 'has-success']">
            <label for="f_type" class="font_normal_body ">操作类型</label>
            <input v-show="false" class="input_search" style="width:60%" v-model="model.f_type" v-validate:f_type='{required: true}'>
            <v-select id="f_paytype"
                      v-model="model.f_type"
                      placeholder='操作类型'
                      :value.sync="model.f_type"
                      :options='ftype'
                      condition="f_table_state = '{}'"
                      style="width: 60%"
                      value-single
                      close-on-select></v-select>
          </div>
          <div class="col-sm-4 form-group"
               :class="[$v.f_refund_fee.required || $v.f_refund_fee.dctest ? 'has-error' : 'has-success']">
            <label class="font_normal_body">退费金额</label>
            <input class="input_search" style="width:60%" type="number" v-model="model.f_refund_fee" :disabled="dis"
                   v-validate:f_refund_fee='{required: true}' placeholder="退费金额">
          </div>
          <div class="col-sm-4 form-group" :class="[$v.payment.required ? 'has-error' : '']">
            <label for="f_paytype" class="font_normal_body" title="参数名称：付款方式">收款方式</label>
            <input type="text" v-show="false" v-model="$refs.payment.selectedItems"  v-validate:payment='{required: true }'>
            <v-select id="f_payment"
                      v-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>
        <div class="row" style="margin-top:10px;" v-if="model.f_type == '卡表退费'">
          <div class="col-sm-4 form-group" :class="[$v.f_pregas.required || $v.f_pregas.dctest ? 'has-error' : 'has-success']">
            <label for="f_pregas" class="font_normal_body">退费气量</label>
            <input  class="input_search" style="width:60%" type="number"
                    v-validate:f_pregas='{required: true, dctest: [0, ">" ]}' v-model="model.f_pregas" v-scale = "[model.f_pregas, 4]">
          </div>
          <div class="col-sm-4 form-group">
            <label for="f_pregas" class="font_normal_body">单&emsp;&emsp;价</label>
            <input  class="input_search" style="width:60%" type="number" v-model="model.f_price" v-scale = "[model.f_price, 4]">
          </div>
        </div>

        <div class="row">
          <div class="col-sm-8">
            <label for="f_paper_account" class="font_normal_body ">&nbsp;&nbsp;&nbsp;备注&nbsp;&nbsp;&nbsp;</label>
            <input class="input_search" style="width:80%" v-model="model.f_comments">
          </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>
    <!--<print-bill :show="print" :bill-manager='config.hasBillManage' v-ref:printbill :bill-url='billUrl' v-on:toggle="clean" @printok="printok" :data='printModel'></print-bill>-->
    <modal :show.sync="modalShow" v-ref:modalShow backdrop="false">
      <article slot="modal-body" class="modal-body">
        <div class="modal_class flex">
          <data-grid class="list_area table_sy" :model="modalData" partial='list' v-ref:grid>
            <template partial='head'>
              <th><nobr>序号</nobr></th>
              <th><nobr>品名规格</nobr></th>
              <th><nobr>单价</nobr></th>
              <th><nobr>数量</nobr></th>
              <th><nobr>金额</nobr></th>
              <th><nobr>收据号码</nobr></th>
              <th><nobr>操作日期</nobr></th>
              <th><nobr>收款方式</nobr></th>
              <th><nobr>操作</nobr></th>
            </template>
            <template partial='body'>
              <td>{{$index+1}}</td>
              <td>{{row.f_typename}}</td>
              <td>{{row.f_unitprice}}</td>
              <td>{{row.f_number}}</td>
              <td>{{row.f_unitprice * row.f_number}}</td>
              <td>{{row.f_bill_number ? row.f_bill_number : '无'}}</td>
              <td>{{row.f_operate_date}}</td>
              <td>
                <v-select v-model="f_payment"
                          placeholder='收款方式'
                          style="width:60%"
                          :value.sync="row.f_payment"
                          :options='$parent.$parent.$parent.paytype'
                          close-on-select clear-button>
              </v-select>
              </td>
              <td>
                <button class="btn btn-link btn-sm" @click="$parent.$parent.$parent.returnOperate(row)">退保证金</button>
              </td>
            </template>
          </data-grid>
        </div>
      </article>
      <footer slot="modal-footer">
      </footer>
    </modal>
  </div>
</template>


<script>
  export default {
    title: '退保证金',
    data () {
      return {
        model: {
          f_refund_fee: '',
          f_comments: '',
          f_price: 0,
          f_pregas: 0
        },
        dis: false,
        blodid: '',
        hasValidateBill: false,
        validateOk: false,

        printModel: {},
        print: false,
        billUrl: '',
        otherid: null,
        billData: {
          url: 'rs/report/refund_sell',
          bill: ''
        },
        ftype: [
          {label: '卡表退费', value: '卡表退费'},
          {label: '退押金', value: '退押金'},
          {label: '退保证金', value: '退保证金'}
        ],
        paytype: this.$appdata.getParam('付款方式'),
        // 保证金数据
        modalData: {
          rows: []
        },
        modalShow: false
      }
    },
    props: ['row'],
    ready () {
    },
    watch: {
      async 'model.f_type' (val) {
        if (val == '退保证金') {
          this.getEarnestMoney()
        } else {
          this.dis = false
          this.model.f_refund_fee = 0
          if (val == '卡表退费') {
            // 获取单价（阶梯使用一阶， 固定直接是固定）
            let data = {
              f_userfiles_id: this.row.f_userfiles_id,
              isGasValue: 0,
              value: 0
            }
            let res = await this.$CommonService.feeCalculate(data, 0)
            if (this.row.f_price_type == '固定气价') {
              this.model.f_price = res.data.chargeprice[0].f_price - 0
            } else if (this.row.f_price_type == '阶梯气价') {
              this.model.f_price = res.data.f_stair1price - 0
            }
            this.dis = true
          }
        }
      },
      'model.f_pregas' () {
        this.calPreamount()
      },
      'model.f_price' () {
        this.calPreamount()
      }
    },
    methods: {
      calPreamount () {
        this.model.f_refund_fee = ((this.model.f_pregas - 0) * (this.model.f_price - 0)).toFixed(4)
      },
      // 退保证金操作
      async returnOperate (row) {
        let rrr = await this.$showMessage('确认退此笔保证金吗?', ['confirm', 'cancel'])
        if (rrr != 'confirm') {
          return
        }
        let data = {
          f_userfiles_id: this.row.f_userfiles_id,
          f_user_id: this.row.f_user_id,
          f_collection: (0 - (row.f_unitprice * row.f_number)),
          f_comments: this.model.f_comments,
          f_payment: row.f_payment[0],
          f_userinfo_id: this.row.f_userinfo_id,
          f_operat_type: '其他收费',
          f_describe: `${this.$login.f.name}对客户${this.row.f_user_name}进行退保证金操作`,
          f_state: '有效',
          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_zoneid: this.$login.f.zoneid,
          f_zones: this.$login.f.zones,
          // 明细
          otherdetail: {
            f_brand_spec: row.f_brand_spec,
            f_typename: '退保证金',
            f_typenumber: row.f_typenumber,
            f_number: row.f_number,
            f_unitprice: (0 - row.f_unitprice)
          },
          // 当前退保证金的记录id
          otherid: row.id
        }
        let res = await this.$resetpost('rs/logic/cancelbzj', data, {resolveMsg: '退保证金成功', rejectMsg: '退保证金失败'})
        this.getEarnestMoney()
      },
      // 获取保证金数据
      async getEarnestMoney () {
        this.dis = true
        let param = {
          f_userinfo_id: this.row.f_userinfo_id
        }
        let res = await this.$resetpost('rs/logic/getbzj', param, {resolveMsg: null, rejectMsg: '获取保证金失败'})
        if (res.data.length > 0) {
          // this.$showAlert('获取到当前用户所交保证金', 'success', 2000)
          // this.model.f_refund_fee = (res.data[0].f_number * res.data[0].f_unitprice)
          // this.otherid = res.data[0].id
          this.modalData.rows = res.data
          this.modalShow = true
        } else {
          this.modalData.rows = []
          this.modalShow = false
          this.$showAlert('当前用户未交过保证金或保证金已退', 'warning', 2000)
        }
      },
      async confirm (row) {
        if (this.model.f_refund_fee < 0) {
          this.$showAlert('退费金额不能小于0', 'warning', 1500)
          return
        }
        if (this.model.f_type != '退保证金') {
          if (this.model.f_type == '退押金' && this.row.f_jayin && this.row.f_jayin == '1') {
            this.$showAlert('此用户已退过押金', 'warning', 1500)
            return
          }
          let param = {
            f_user_id: this.row.f_user_id,
            f_userfiles_id: this.row.f_userfiles_id,
            f_payment: this.model.f_payment[0],
            f_operat_type: this.model.f_type,
            version: this.row.version,
            f_comments: this.model.f_comments,
            f_collection: this.model.f_refund_fee,
            f_billing: this.row.f_billing,
            f_price_id: this.row.f_price_id,
            f_orgid: this.$login.f.orgid,
            f_stairprice_id: this.row.f_stairprice_id,
            operInfo: {
              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
            }
          }
          let url = ''
          if (this.model.f_type == '卡表退费') {
            param.f_pregas = this.model.f_pregas
            param.f_price = this.model.f_price
            url = 'rs/logic/cardTableRefund'
          } else {
            url = 'rs/logic/yajincancel'
          }
          await this.$resetpost(url, {data: param}, {
            warnMsg: `确定要进行${this.model.f_type}操作吗？`,
            resolveMsg: `${this.model.f_type}操作成功！！`,
            rejectMsg: `${this.model.f_type}操作失败！！请重试！！`
          })
          this.$dispatch('success')
        }
      },
      clean () {
        this.$dispatch('refresh')
      },
      printok () {
        this.$dispatch('success')
      },
      validateBill (val) {
        this.validateOk = !val.isOk
        this.billData.bill = val.bill
      }
    }
  }
</script>
<style lang="less">
  .modal_class {
    .table > tbody > tr > td {
      text-align: center;
    }
  }
</style>
