<template>
  <div class="flex-row">
    <div  class="basic-main">
      <criteria-paged :model="model" v-ref:paged>
        <criteria partial='criteria' @condition-changed='$parent.search' v-ref:cri>
          <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
            <div class="row">
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">客户编号</label>
                <input type="text" style="width:60%" class="input_search" v-model="model.f_userinfo_code"
                       condition="f_userinfo_code = '{}'" @keyup.enter="search" placeholder='客户编号'>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">客户姓名</label>
                <input type="text" style="width:60%" class="input_search" v-model="model.f_user_name"
                       condition="f_user_name = '{}'" @keyup.enter="search" placeholder='客户姓名'>
              </div>
              <div class="row" >
                <div class="form-group col-sm-2">
                  <label class="font_normal_body">起始日期</label>
                  <datepicker id="statDate" placeholder="起始日期"
                              v-model="model.f_stat_data"
                              :value.sync="model.f_stat_data"
                              condition="f_hand_date >'{}'"
                              :format="'yyyy-MM-dd'"
                              class="datepicker"
                              style="width:60%">
                  </datepicker>
                </div>
                <div class="form-group col-sm-2">
                  <label class="font_normal_body">结束日期</label>
                  <datepicker id="endDate" placeholder="结束日期"
                              v-model="model.f_end_data"
                              :value.sync="model.f_end_data"
                              condition="f_hand_date <'{}'"
                              :format="'yyyy-MM-dd'"
                              class="datepicker"
                              style="width:60%">
                  </datepicker>
                </div>

              <div class="span" style="float:right;">
                <button class="button_search button_spacing" @click="search()" v-el:cx>查询</button>
                <export-excel :data="$parent.$parent.getCondition"
                              :field="$parent.$parent.getfield"
                              sqlurl="rs/logic/saleExport" progress="saleGetExportProgress"
                              sql-name="gas-fee-remission" template-name='气费减免导出'  :choose-col="true">
                </export-excel>
                <div
                  :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                  @click="$parent.$parent.hidden()"
                  class="button_spacing"
                  style="float: right">
                </div>
              </div>
              </div>
              <div class="row" v-show="$parent.$parent.criteriaShow" >
                <res-select-group :initres="$parent.$parent.initres"  @re-res="$parent.$parent.getRes" v-ref:sel></res-select-group>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">已减免金额</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_debt_money_jm"
                         condition="f_debt_money_jm >= '{}'" placeholder='已减免金额大于'>
                </div>
              </div>
              <div class="row" v-show="$parent.$parent.criteriaShow" >
                 <div class="col-sm-2 form-group">
                  <label class="font_normal_body">是否减免</label>
                   <v-select :value.sync="model.isjm" v-model='model.isjm'
                             :options='$parent.$parent.jianmian' placeholder='是否减免'
                             condition="f_debt_money_jm {}"
                             close-on-select></v-select>
                </div>
              </div>
          </div>
          </div>
        </criteria>
        <data-grid :model="model"  partial='list' class="table_sy" v-ref:grid>
          <template partial='head'>
            <tr>
              <th>序号</th>
              <th>客户编号</th>
              <th>客户姓名</th>
              <th>地址</th>
              <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_code}}</td>
            <td style="text-align: center;">{{row.f_user_name}}</td>
            <td style="text-align: center;">{{row.f_address}}</td>
            <td style="text-align: center;">{{row.f_last_tablebase}}</td>
            <td style="text-align: center;">{{row.f_tablebase}}</td>
            <td style="text-align: center;">{{row.f_input_date}}</td>
            <td style="text-align: center;">{{row.f_oughtfee}}</td>
            <td style="text-align: center;">{{row.f_debt_money}}</td>
            <td style="text-align: center;">{{row.f_debt_money_jm}}</td>
            <td style="text-align: center;">{{(row.f_oughtfee.toFixed(2)-row.f_debt_money.toFixed(2)).toFixed(2)}}</td>
            <td style="text-align: center;">{{row.sell_operator}}</td>
            <td style="text-align: center;">{{row.sell_operate_date}}</td>
            <div class="form-group" style="margin-bottom:0px;display: contents;" :class="{'has-error has-feedback': row.error}"
            >
              <input title="气费减免" type="text" class="input_search" v-model="row.amount"
                     style="width: 100px;padding: 6px;" @blur="$parent.$parent.$parent.submits(row)"
              >
            </div>
          </template>
        </data-grid>
      </criteria-paged>
      <table class="table-hover">
        <tr style="position: relative" class="table-bordered">
          <td
                  style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;color: #5CB95C;font-weight: bold">
            汇总信息
          </td>
          <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
            用气金额合计:&emsp;{{sumsmodel.f_oughtfee.toFixed(2)}}
          </td>
          <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
            已交金额合计:&emsp;{{sumsmodel.f_debt_money.toFixed(2)}}
          </td>
          <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
            已减免金额合计:&emsp;{{sumsmodel.f_debt_money_jm.toFixed(2)}}
          </td>
          <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
            欠费金额合计:&emsp;{{sumsmodel.f_qf_money}}
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>

<script>
  /**
   *气费减免页面
   */
  import Vue from 'vue'
  import {PagedList, HttpResetClass} from 'vue-client'
  import * as Util from '../../../../Util'

  export default {
    title: '气费减免',
    data () {
      return {
        initres: {
          org: [this.$login.f.orgid],
          dep: [],
          user: []
        },
        // 查询条件显示
        criteriaShow: false,
        model: new PagedList('rs/sql/gas-fee-remission', 20, {}, {
          f_oughtfee: 0,
          f_debt_money: 0,
          f_debt_money_jm: 0,
          f_qf_money: 0
        }),
        sumsmodel: {},
        getfield: {
          'f_userinfo_code': '用户编号',
          'f_user_name': '用户姓名',
          'f_address': '地址',
          'f_last_tablebase': '上期底数',
          'f_tablebase': '本期底数',
          'f_hand_date': '抄表时间',
          'f_oughtfee': '用气金额',
          'f_debt_money': '已交金额',
          'f_debt_money_jm': '已减免金额',
          'f_qf_money': '欠费金额',
          'sell_operator': '减免操作人',
          'sell_operate_date': '减免操作时间'
        },
        jianmian: [{label: '已减免', value: '> 0'}, {label: '未减免', value: '= 0'}]
      }
    },
    ready () {
      // this.$refs.paged.$refs.cri.search()
      this.$refs.paged.$refs.cri.model.f_stat_data = Util.toStandardDateString() + ' 00:00:00'
      this.$refs.paged.$refs.cri.model.f_end_data = Util.toStandardDateString() + ' 23:59:59'
      this.sumsmodel = this.$refs.paged.$refs.grid.model.sums
    },
    methods: {
      getRes (obj) {
        this.orgCondtionStr = obj
      },
      hidden () {
        this.criteriaShow = !this.criteriaShow
      },
      search (args) {
        console.log('===>', args)
        console.log('=this.model==>', this.model)
        if (!this.orgCondtionStr) {
          args.condition = `${args.condition}` + ` and f_orgid = '${this.$login.f.orgid}'`
        } else {
          args.condition = `${args.condition}` + this.orgCondtionStr
        }
        this.model.search(args.condition)
        this.sumsmodel = this.$refs.paged.$refs.grid.model.sums
      },
      submits (row) {
        console.log('开始减免了', row)
        let message = `确认给用户${row.f_user_name}减免${row.amount}元`
        let operInfo = {
          f_operatorid: Vue.$login.f.id,
          f_operator: Vue.$login.f.name,
          f_depid: Vue.$login.f.depids,
          f_depname: Vue.$login.f.deps
        }

        console.log('fffff', Vue.$login.f)
        if (row.amount == 0) {
          this.$showMessage('减免金额不能为0')
        } else if (row.amount > (row.f_oughtfee - row.f_debt_money)) {
          this.$showMessage('输入值大于欠费金额，请重新输入')
        } else {
          row.isqianfei = 'y'
          if ((row.amount - 0) === ((row.f_oughtfee - 0) - (row.f_debt_money - 0))) {
            row.isqianfei = 'n'
          }
          this.$showMessage(message, ['confirm', 'cancel']).then((res) => {
            if (res === 'confirm') {
              this.$resetpost('rs/logic/gasfeereduction', {data: {con: row, operInfo: operInfo}}, {resolveMs: '减免成功', rejectMsg: '减免出错'})
              this.$refs.paged.$refs.cri.search()
            }
          })
        }
      }
    },
    computed: {
      getCondition () {
        if (!this.orgCondtionStr) {
          return {condition: `${this.$refs.paged.$refs.cri.condition}` + ' and f_orgid = ' + this.$login.f.orgid}
        } else {
          return {condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr}
        }
      }
    }
  }
</script>
<style lang="less" scoped>

</style>

