<template>
  <criteria-paged :model="model" v-ref:paged>
    <criteria partial='criteria' v-ref:criteria @condition-changed="$parent.selfSearch">
      <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
        <div class="row">
          <div class="form-group col-sm-2">
            <label class="font_normal_body"><nobr>用户编号</nobr></label>
            <input type="text" style="width:60%" class="input_search" v-model="model.f_userinfo_code"
                   condition="tbd.f_userinfo_code='{}'" placeholder='用户编号'>
          </div>
          <div class="form-group col-sm-2">
            <label class="font_normal_body"><nobr>用户姓名</nobr></label>
            <input type="text" style="width:60%" class="input_search" v-model="model.f_user_name"
                   condition="tbd.f_user_name = '{}'" placeholder='用户姓名'>
          </div>
          <div class="form-group col-sm-2" v-if="!$parent.$parent.chargeShow" >
            <label class="font_normal_body"><nobr>用气性质</nobr></label>
            <input type="text" style="width:60%" class="input_search" v-model="model.f_gasproperties"
                   condition="uf.f_gasproperties='{}'" placeholder='用气性质'>
          </div>

          <div class="span" style="float:right;">
            <button class="button_export button_spacing" @click="$parent.$parent.importToSuccess()">一键生成补扣费记录</button>
            <button class="button_export button_spacing" @click="$parent.$parent.importForThe()">导入待处理记录</button>
            <button class="button_search button_spacing" @click="$parent.$parent.search()">查询</button>
            <button class="button_clear button_spacing" @click="$parent.$parent.clear()">清空</button>

            <div style="float: right" class="button_spacing"
                 :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                 @click="$parent.$parent.hidden()"></div>
          </div>
        </div>
        <div class="row" v-show="$parent.$parent.criteriaShow">
          <div class="form-group col-sm-2">
            <label class="font_normal_body"><nobr>记录状态</nobr></label>
            <v-select v-model="model.f_state"
                      placeholder='记录状态'
                      :value.sync="model.f_state"
                      :options='$parent.$parent.states'
                      condition="tbd.f_state = '{}'"
                      style="width: 60%"
                      close-on-select></v-select>
          </div>
          <div class="form-group col-sm-2" v-if="$parent.$parent.chargeShow" >
            <label class="font_normal_body"><nobr>用气性质</nobr></label>
            <input type="text" style="width:60%" class="input_search" v-model="model.f_gasproperties"
                   condition="uf.f_gasproperties='{}'" placeholder='用气性质'>
          </div>
        </div>
      </div>
    </criteria>
    <data-grid :model="model" partial='list' v-ref:grid class="list_area table_sy">
      <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>
        </tr>
      </template>
      <template partial='body'>
        <td style="text-align: center;"> {{ $index + 1 }}</td>
        <td>{{ row.f_userinfo_code }}</td>
        <td>{{ row.f_user_name }}</td>
        <td>{{ row.f_gasproperties }}</td>
        <td>{{ row.f_jval }}</td>
        <td>{{ row.f_balance_amount }}</td>
        <td>{{ row.f_collection }}</td>
        <td>{{ row.f_collection < 0 ? '扣费' : '补费' }}</td>
        <td>{{ row.f_operate_date }}</td>
        <td>{{ row.f_state }}</td>
        <td>{{ row.f_operator }}</td>
        <td>{{ row.f_orgname }}</td>
        <td style="text-align: center"> <button v-if="row.f_state === '待生成'" type="button" class="btn btn-danger" @click.stop="$parent.$parent.$parent.cancel(row)">作废</button>&nbsp;&nbsp;&nbsp;
        </td>
      </template>
    </data-grid>
  </criteria-paged>
  <work-busy :is-busy='isbusy'></work-busy>
  <modal :show.sync="fileShow" width="480px" v-ref:modal backdrop="true">
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="importForThe"><span>&times;</span></button>
      <h4 class="modal-title">选择文件</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <div class="form-group">
        <file-upload class="my-file-uploader" name="useraddressUploadFile" action="rs/file/uploadFile" tagname="确定"
                     multiple v-ref:file></file-upload>
        <button type="button" style="margin-top: 10px;" @click="downloadFiles()" class="button_clear btn-gn">模板下载</button>
      </div>
    </article>
    <footer slot="modal-footer" class="modal-footer" style="display: none"></footer>
  </modal>
</template>
<script>
import {PagedList, HttpResetClass} from 'vue-client'
import Vue from 'vue'

export default {
  title: '批量预存',
  props: ['chargeShow'],
  data () {
    return {
      criteriaShow: false,
      model: new PagedList('rs/sql/getDeductionRecord', 30 ,{}),
      fileShow: false,
      condition: '',
      searchEd: false,
      templateName: '批量补扣费导入模板',
      states: [
        {label: '待生成', value: '待生成'},
        {label: '已生成', value: '已生成'},
        {label: '作废', value: '作废'}
      ],
      isbusy: false
    }
  },

  ready () {
  },
  methods: {
    downloadFiles () {
      let downurl = 'rs/downloadfile/file?filename=' + this.templateName
      this.$downFile(downurl, this.templateName + '.xlsx')
    },
    selfSearch (args) {
      this.condition = args.condition
      this.model.search(args.condition, this.model)
      this.searchEd = true
      this.$dispatch('reSearch')
    },
    // 待生成记录生成为有效收费记录
    async importToSuccess () {
      if (this.searchEd) {
        // 查询有多少待执行记录
        let condition = this.condition + ` and tbd.f_state = '待生成' `
        let http = new HttpResetClass()
        let getCount = await http.load('POST', 'rs/sql/getDeductionRecord',
          {data: {
            condition: condition
          }},
          {resolveMsg: null, rejectMsg: null})
        let sum = getCount.data.length
        if (sum === 0) {
          this.$showAlert('没有待生成的记录', 'warning', 2000)
          return
        }
        this.$showMessage(`确定要生成收费记录吗?共计${sum}条记录`, ['confirm', 'cancel']).then(async (res) => {
          if (res === 'confirm') {
            this.$dispatch('batchToSell', condition)
          }
        })
      } else {
        this.$showAlert('请先执行查询操作', 'warning', 2000)
      }
    },
    importForThe () {
      this.fileShow = !this.fileShow
    },
    // 作废待生成记录
    async cancel (row) {
      await this.$resetpost('rs/entity/t_batch_deductions', {id: row.id, f_state: '作废'}, {
        resolveMsg: '作废成功',
        rejectMsg: '作废失败!!!'
      })
      await this.search()
    },
    // 隐藏折叠条件
    hidden () {
      this.criteriaShow = !this.criteriaShow
    },
    // 清空
    clear () {
      Object.keys(this.$refs.paged.$refs.criteria.model).forEach((key) => {
        this.$refs.paged.$refs.criteria.model[key] = []
      })
    },
    search () {
      this.$refs.paged.$refs.criteria.search()
    }
  },
  events: {
    onFileUpload: async function (file, res) {
      this.isbusy = true
      let data = {
        filepath: res.f_downloadpath,
        f_operator: Vue.$login.f.name,
        f_operatorid: Vue.$login.f.id,
        f_orgid: Vue.$login.f.orgid,
        f_orgname: Vue.$login.f.orgs,
        f_depid: Vue.$login.f.depids,
        f_depname: Vue.$login.f.deps
      }
      await this.$resetpost(`rs/logic/sale_import_deduction`, data, {
        resolveMsg: null,
        rejectMsg: '导入失败',
        silent: true
      }, 0).then((res) => {
        this.isbusy = false
        if (res.data.type == 'success') {
          this.$showAlert('导入信息:' + res.data.msg, res.data.type, 2000)
        } else {
          this.$showAlert('文件上传失败，请检查数据格式(每个数据字段值都为文本格式,收费金额为数字,日期格式为 年年年年-月月-日日 时时:分分:秒秒),多次错误请尝试重新下载模板', 'warning', 2000)
        }
        // 关闭上传框
        this.importForThe()
        // 重新查询
        this.search()
      }).catch(() => {
        this.$showAlert('文件上传失败，请检查数据格式(每个数据字段值都为文本格式,收费金额为数字,日期格式为 年年年年-月月-日日 时时:分分:秒秒),多次错误请尝试重新下载模板', 'warning', 2000)
        this.isbusy = false
      })
    },
    'onFileError' (error) {
      this.$showAlert('文件上传失败，请检查数据格式(每个数据字段值都为文本格式,收费金额为数字,日期格式为 年年年年-月月-日日 时时:分分:秒秒),多次错误请尝试重新下载模板', 'warning', 2000)
    }
  }

}
</script>
<style lang="less" scoped>
.table_sy {
  tr {
    td {
      text-align: center;
    }
  }
}
</style>
