<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search">
      <div class="flex" :class="{showbottom:showbottomsum}" v-if="!show">
        <criteria-paged :model="model" v-ref:paged  @sort="sort">
          <criteria partial='criteria' @condition-changed='$parent.selfSearch' 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 for="startDate" class="font_normal_body">开始日期</label>
                  <datepicker id="startDate" placeholder="开始日期" style="width:60%"
                              v-model="model.startDate"
                              :value.sync="model.startDate"
                              :format="'yyyy-MM-dd HH:mm:ss'"
                              :show-reset-button="true"
                              condition="f_operate_date >= '{}'">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label for="endDate" class="font_normal_body">结束日期</label>
                  <datepicker id="endDate" placeholder="结束日期" style="width:60%"
                              v-model="model.endDate"
                              :value.sync="model.endDate"
                              :format="'yyyy-MM-dd HH:mm:ss'"
                              :show-reset-button="true"
                              condition="f_operate_date <= '{}'">
                  </datepicker>
                </div>
                <div class="span" style="float:right;">
                  <button class="button_search button_spacing" @click="search()">查询</button>
                  <button class="button_clear  button_spacing" @click="$parent.$parent.clear()">清空</button>

                  <export-excel :data="$parent.$parent.getCondition" :footer="$parent.$parent.footer"
                                :field="$parent.$parent.getfield" :header="$parent.$parent.other"
                                :defaultselect="$parent.$parent.defaultexport"
                                sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="groupChargeQuery" template-name='团缴查询导出'
                                :choose-col="true">
                  </export-excel>

                  <print-data :sum-field="$parent.$parent.getfield" :model="$parent.model" :field="$parent.$parent.getfield"
                    :defaultfield="$parent.$parent.config.defaultPrint"
                    titletable="团体缴费记录报表" :starthead="$parent.$parent.getstart"
                              :sumsmodel="$parent.$parent.sumsmodel"></print-data>
                  <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">
                <res-select-group :show-component="['company','department','operator']" :selectin="true" :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_user_name"
                         condition="f_user_name like '%{}%'" placeholder="客户名称">
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户类型</label>
                  <v-select :value.sync="model.f_user_type"
                            :search="false"
                            :options='$parent.$parent.usertypes' placeholder='请选择' v-model="model.f_user_type"
                            condition="f_user_type = '{}'"
                            close-on-select></v-select>
                </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_address"
                         condition="f_address like '%{}%'" placeholder='客户地址'>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body" title="参数:付款方式查询">付款方式</label>
                  <v-select :value.sync="model.f_payment" multiple
                            v-model="model.f_payment"
                            :options='$parent.$parent.payment' placeholder='请选择'
                            condition="f_payment in {}"
                            close-on-select></v-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">收费类型</label>
                  <v-select :value.sync="model.f_type" multiple
                            :search="false"
                            :options='$parent.$parent.chargetype' placeholder='请选择'
                            close-on-select clear-button
                            condition="f_type in {}"
                            v-model="model.f_type"></v-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">收费状态</label>
                  <v-select :value.sync="model.f_state" multiple
                            :search="false"
                            v-model="model.f_state"
                            :options='$parent.$parent.charge_state' placeholder='请选择'
                            condition="f_state in {}"
                            close-on-select></v-select>
                </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_customer"
                         condition="f_customer like '%{}%'" placeholder="团缴编号">
                </div>
              </div>
            </div>
          </criteria>

          <data-grid :model="model" partial='list' class="list_area table_sy" style="height: 95%" v-ref:grid>
            <template partial='head'>
              <tr>
                <th>
                  <nobr>收费编号</nobr>
                </th>
                <th>
                  <nobr>团缴编号</nobr>
                </th>
                <th>
                  <nobr>客户姓名</nobr>
                </th>
                <th>
                  <nobr>客户类型</nobr>
                </th>
                <th>
                  <!--<nobr>客户地址</nobr>-->
                  <data-order field="f_address" name="客户地址" :order.sync="$parent.$parent.$parent.orderFields.f_address"></data-order>
                </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>
                <th>
                  <nobr>收款</nobr>
                </th>
                <th>
                  <nobr>收费状态</nobr>
                </th>
                <th>
                  <!--<nobr>收费日期</nobr>-->
                  <data-order field="f_operate_date" name="收费日期" :order.sync="$parent.$parent.$parent.orderFields.f_operate_date"></data-order>
                </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>
              </tr>
            </template>
            <template partial='body'>
              <td style="text-align: center;">
                <nobr>{{row.id}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_customer}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_user_name}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_user_type}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_address}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_pregas}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_preamount}}</nobr>
              </td>
<!--              <td style="text-align: center;">-->
<!--                <nobr>{{row.f_delay}}</nobr>-->
<!--              </td>-->
<!--              <td style="text-align: center;">-->
<!--                <nobr>{{row.f_delaypay}}</nobr>-->
<!--              </td>-->
              <td style="text-align: center;">
                <nobr>{{row.f_balance}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_curbalance}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_newbalance}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_newcurbalance}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_collection}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_state}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_operate_date}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_payment}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_type}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_comments}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_operator}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_orgname}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_depname}}</nobr>
              </td>
              <td style="text-align: center;">
                <button class="btn btn-link" @click.stop="$parent.$parent.$parent.showchargedetail(row)">查看</button>
              </td>
            </template>
            <template partial='foot'></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_pregas}}
            </td>
            <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
              燃气费合计:&emsp;{{sumsmodel.f_preamount}}
            </td>
            <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
              收款合计:&emsp;{{sumsmodel.f_collection}}
            </td>
          </tr>
        </table>
      </div>
    </div>
  </div>
  <modal :show.sync="showdetail" width="80%" title="详细记录" v-ref:modal large backdrop="false">
    <article slot="modal-body" class="modal-body" style="height: 700px">
      <charge-query :batchid="f_batch_id" :data="data" v-if="showdetail"></charge-query>
    </article>
    <footer slot="modal-footer" class="modal-footer">
    </footer>
  </modal>
</template>

<script>
  import {PagedList} from 'vue-client'
  import exportConfig from '../config/exportConfig'
  let readySomething = async function (self) {
    //加载抄表册
    self.$refs.paged.$refs.cri.model.startDate = self.data ? self.data.startDate : self.$login.toStandardDateString() + ' 00:00:00'
    self.$refs.paged.$refs.cri.model.endDate = self.data ? self.data.endDate : self.$login.toStandardDateString() + ' 23:59:59'
    self.$refs.paged.$refs.cri.search()
    await self.$MagLoadParams.loadParam()
    self.sumsmodel = self.$refs.paged.$refs.grid.model.sums;
  }
  export default {
    props:['data'],
    title: '团缴查询',
     data() {
      return {
        f_batch_id: null,
        showdetail: false,
        isSingleUser: null,
        startDate1:'',
        endDate1:'',
        other:[],
        footer:[],
        config: {
          defaultPrint: ['f_user_name', 'f_pregas', 'f_collection', 'f_operate_date']
        },
        //排序
        orderitem:'f_operate_date desc',
        orderFields: {
          f_operate_date: 'no'
        },
        //f_price_name:[],
        gasproperties:[],
        showinfo: false,
        data: {},
        getstart:'',
        show:false,
        chargetype: [{label: '团体收费', value: '团体收费'}, {label: '团体退费', value: '团体退费'}],
        rowdata:{},
        initres: {
          org:[this.$login.f.orgid],
          dep:[],
          user:[]
        },
        model: new PagedList('api/af-revenue/sql/groupChargeQuery', 20, {orderitem: this.orderitem?'"' + this.orderitem + ' "':`'f_operate_date desc'`},{
          f_pregas: 0,
          f_preamount: 0,
          f_collection: 0,
        }),
        criteriaShow: false,
        orgCondtionStr: '',
        area: [],
        sumsmodel: {},
        f_filialeid: this.$login.f.orgid,
        selected:{},
        showbottomsum:false  //页面下方是否有合计: 默认为false, 在watch修改
      }
    },
    ready() {
      this.$refs.paged.$refs.cri.model.f_state=['有效']
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      showchargedetail(row){
        this.showdetail = true
        this.f_batch_id = row.id
      },
      getotherfooter () {
        // this.$refs.paged.$refs.cri.$refs.exports.otherData = [];
        // this.$refs.paged.$refs.cri.$refs.exports.footerData = [];
        this.other = [];
        this.footer = [];
        let exportdata = this.getCondition;
        let otherInData = [];
        otherInData.push(`导出时间: ${this.$login.toStandardTimeString()}`);
        let footerData = [], exportfield = this.getfield;
        footerData.push("合计");
        let self = this;
        for (var field in self.sumsmodel) {
          footerData.push(`${exportfield[field]}合计:${self.sumsmodel[field]}`);
        }
        this.footer.push(footerData);
        this.other.push(otherInData);
      },
      search(){
        this.$refs.paged.$refs.cri.search()
      },
      selfSearch(args) {
        if (this.data) {
          this.$parent.$parent.$parent.data.startDate=this.$refs.paged.$refs.cri.model.startDate
          this.$parent.$parent.$parent.data.endDate=this.$refs.paged.$refs.cri.model.endDate
        }

        let f_orgstr = this.orgCondtionStr
        args.condition = `${args.condition} ` + f_orgstr
        this.model.search(args.condition, args.model)
        this.sumsmodel = this.$refs.paged.$refs.grid.model.sums;
      },
      clear() {
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })
      },
      dealmsg(val,startDate,endDate) {
        val.model = this.model.model
        startDate=this.startDate1
        endDate=this.endDate1
        this.$dispatch('deal-msg', val,startDate,endDate)
      },
      show() {
        this.criteriaShow = true
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      async getRes(condition,obj) {
        this.orgCondtionStr = condition
        this.orgname = obj.orgnames[0]
        this.depname = obj.depnames[0]
        this.operatorname = this.userresid.filter(x => obj.operatornames.includes(x.value)).map(x=>x.label).toString()
      },
      sort (field, rule) {
        // 将所有排序方式设为不排序，实现相互排斥
        for (let key in this.orderFields) {
          if (key === field) {
            this.orderFields[key] = rule
          } else {
            this.orderFields[key] = 'no'
          }
        }
        // 如果新规则不排序，还原为默认排序
        if (rule === 'no') {
          this.model.paramSource.orderitem = `'${this.orderitem}'`
        } else {
          this.model.paramSource.orderitem = `'${field} ${rule}'`
        }

        this.search()
      }

    },
    watch: {
      'model.rows'(val) {
        //如果有合计 这里为true, 其他为false ,反正相反
        this.showbottomsum=true;
        if(val.length === 0){
          this.sumsmodel = {}
          this.showbottomsum=false;
        }
      },
      'model.rows.length' () {
        this.isSingleUser = this.model.rows
      },
      sumsmodel:{
        handler: function(val) {
          this.getotherfooter();
        },
        deep: true
      }
    },
    computed: {
      getstart(){
        return `开始时间：${this.$refs.paged.$refs.cri.model.startDate} 结束时间：${this.$refs.paged.$refs.cri.model.endDate}`
      },
      chooserow(){
        let aaa= true
        if(this.$login.r && this.$login.r.includes('打印项选择')){
          aaa =  false
        }
        return aaa
      },
      getCondition() {
        return {
          startDate: this.$refs.paged.$refs.cri.model.startDate,
          endDate: this.$refs.paged.$refs.cri.model.endDate,
          condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr,
          orderitem: this.orderitem?`${this.orderitem}`: " f_operate_date desc",
          items: "*",
          tablename: "t_sellinggasbatch"
        }
      },
      getfield() {
        return exportConfig.GroupCharge
      },
      defaultexport() {
        return exportConfig.GroupCharge
      },
      usertypes() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用户类型')]
      },
      payment() {
        return [...this.$appdata.getParam('付款方式')]
      },
      banktype() {
        return [...this.$appdata.getParam('支付渠道查询')]
      },
      charge_state() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('收费状态')]
      },
      bank(){
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('银行名称')]
      }
    }
  }
</script>
<style scoped>
</style>
