<template>
  <div id="unit" class="flex-row">
    <div class="basic-main">
      <div class="flex">

        <criteria-paged :model="model" v-ref:paged>
          <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"
                  >
                  </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"
                  >
                  </datepicker>
                </div>

                <div class="form-group col-sm-2">
                  <label class="font_normal_body">选择小区</label>
                  <v-select :value.sync="model.f_residential_area" :value-single="true"
                            class="select_list select"
                            style="width: 60%"
                            v-model="model.f_residential_area"
                            :options='$parent.$parent.residentialArea' placeholder='选择小区'
                            condition="f_residential_area = '{}'"
                            close-on-select search="true">
                  </v-select>
                </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"
                                :field="$parent.$parent.getfield"
                                :footer="$parent.$parent.footer"
                                :header="$parent.$parent.other"
                                sqlurl="api/af-revenue/logic/openapi/exportfile"
                                sql-name="CommunityPaymen"
                                template-name='小区缴费汇总(机表)导出'
                                :choose-col="true"></export-excel>
                  <print-data :sum-field="$parent.$parent.getfield" :model="$parent.model" :field="$parent.$parent.getfield"
                               :defaultfield="$parent.$parent.defaultfield"
                              titletable="小区缴费汇总(机表)导出"
                              :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 :initres="$parent.$parent.initres" :show-component="$parent.$parent.showComponent"  @re-res="$parent.$parent.getRes" v-ref:sel></res-select-group>

              </div>
            </div>
          </criteria>

          <data-grid :model="model" partial='list' class="list_area table_sy" 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>
                </th>
                <th>
                  <nobr>欠费</nobr>
                </th>
                <th>
                  <nobr>余额</nobr>
                </th>

              </tr>
            </template>
            <template partial='body'>
              <td style="text-align:center">
                <nobr>{{$index+1}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_residential_area}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_oughtfee}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_debt_money}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.sf_debt_money}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.qf}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_balance}}</nobr>
              </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_oughtfee}}
            </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}}
            </td>
            <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
              实收合计:&emsp;{{sumsmodel.sf_debt_money}}
            </td>

            <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
              欠费合计:&emsp;{{sumsmodel.qf}}
            </td>
            <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
              余额合计:&emsp;{{sumsmodel.f_balance}}
            </td>
          </tr>
        </table>
      </div>
    </div>
  </div>
</template>

<script>
  import {PagedList, HttpResetClass} from 'vue-client'
  import defaultPrint from '../config/DefaultPrint'
  import exportConfig from "../config/exportConfig";
  import plugins from "../../../plugins/GetLoginInfoService";

  let readySomething = async function (self) {
    self.defaultfield = [...self.defaultfield, ...self.config.defaultPrint]
    self.$refs.paged.$refs.cri.model.startDate = self.$login.toStandardDateString() + ' 00:00:00'
    self.$refs.paged.$refs.cri.model.endDate = self.$login.toStandardDateString() + ' 23:59:59'
    // await self.$refs.paged.$refs.cri.search()
    await self.$MagLoadParams.loadParam()
    self.initParams()

    self.sumsmodel = self.$refs.paged.$refs.grid.model.sums
  }

  export default {
    title: '抄表失败查询',
    data() {
      let model = new PagedList('api/af-revenue/sql/CommunityPaymen', 20, {
        startdate: 'this.model.startDate',
        enddate: 'this.model.endDate'
      },{
        f_oughtfee:0,
        f_debt_money:0,
        sf_debt_money:0,
        qf:0,
        f_balance:0
      })
      model.f_filialeid = this.$login.f.orgid
      return {
        data: {},
        initres: {
          org:[this.$login.f.orgid],
          dep:[],
          user:[],
        },
        showComponent:['company','department','operator'],
        other: [],
        footer: [],
        model: model,
        criteriaShow: false,
        f_filialeid: this.$login.f.orgid,
        orgid: [this.$login.f.orgid],
        //小区
        residentialArea: [],
        orgCondtionStr: '(' + this.$login.f.orgid + ')',
        //合计数据
        sumsmodel:{},
        defaultfield: [],
        config: {
          defaultPrint: ['f_residential_area', 'f_balance']
        },
      }
    },
    ready() {
      this.getaddress()
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      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);
        let date = []
        date.push(`起始时间:${this.$refs.paged.$refs.cri.model.startDate}`)
        date.push(`终止时间:${this.$refs.paged.$refs.cri.model.endDate}`)
        this.other.push(date)
      },
      async getaddress() {
        console.log('开始获取小区')
        let HttpReset = new HttpResetClass()
        var data = await HttpReset.load('POST', 'api/af-revenue/sql/manage_getarealist', {
          data: {
            condition: `1=1 and s.f_filialeid = '${this.$login.f.orgid}'`
          }
        }, {resolveMsg: null, rejectMsg: '获取小区失败!'})
        console.log('小区', data)
        let house = [{label: '全部', value: ''}]
        for (let row of data.data) {
          console.log('开始保存小区')
          house.push({label: row.f_residential_area, value: row.f_residential_area})
        }
        this.residentialArea = house
      },
      selfSearch(args) {
        // if (this.orgCondtionStr)
        //   args.condition = `${args.condition}` + 'and f_filialeid in ' + this.orgCondtionStr
        // else
        //   args.condition = `${args.condition}` + 'and f_filialeid in ' + '(' + this.$login.f.orgid + ')'
        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
      },
      getRes(condition,obj) {
        // this.orgCondtionStr = plugins.convertToIn(obj)
        this.orgCondtionStr = condition
        this.orgname = obj.orgnames[0]
        this.depname = obj.depnames[0]
      },
      clear() {
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      }
    },
    watch: {
      sumsmodel: {
        handler: function (val) {
          this.getotherfooter();
        },
        deep: true
      }
    },
    computed: {
      getCondition() {
        var con = ""
        if (this.orgCondtionStr) {
          con = `${this.$refs.paged.$refs.cri.condition} ` + this.orgCondtionStr
        } else {
          con = `${this.$refs.paged.$refs.cri.condition} and f_orgid in ('${this.$login.f.orgid}' )`
        }
        return {
          startdate: this.$refs.paged.$refs.cri.model.startDate,
          enddate: this.$refs.paged.$refs.cri.model.endDate,
          condition: con
        }
      },
      getfield() {
        return exportConfig.CommunityPaymen
      }
    }

  }
</script>
