<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 class="font_normal_body">公&ensp;&ensp;司</label>
                  <res-select :initresid="$parent.$parent.orgid"
                              style="width:60%"
                              @res-select="$parent.$parent.getRes"

                              restype='organization'>
                  </res-select>
                </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="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'"
                              :show-reset-button="true"
                              condition="f_operate_date >= '{} 00:00:00'">
                  </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'"
                              :show-reset-button="true"
                              condition="f_operate_date <= '{} 23:59:59'">
                  </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>
                  <!--<button class="btn btn-default" @click="$parent.$parent.stamp()">打印</b utton>-->

                  <export-excel :data="$parent.$parent.getCondition"
                                :field="$parent.$parent.getfield"
                                sqlurl="rs/logic/exportfile" sql-name="areaGeneralQuery" template-name='小区综合查询'
                                :choose-col="true"></export-excel>

<!--                  <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>
          </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>
              </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_price_name}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_userfiles_id}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_gas}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_money}}</nobr>
              </td>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
      </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.$refs.paged.$refs.cri.model.startDate = self.$login.toStandardDateString()
    self.$refs.paged.$refs.cri.model.endDate = self.$login.toStandardDateString()
    // 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('rs/sql/areaGeneralQuery', 20, {f_filialeid: "'this.f_filialeid'"})
      model.f_filialeid = this.$login.f.orgid
      return {
        data: {},
        model: model,
        criteriaShow: false,
        f_filialeid: this.$login.f.orgid,
        orgid:[this.$login.f.orgid],
        //小区
        residentialArea: [],
        orgCondtionStr: '(' + this.$login.f.orgid + ')',
        //合计数据
      }
    },
    ready() {
      this.getaddress()

      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })


    },
    methods: {
      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 + ')'
        this.model.search(args.condition, args.model)
      },
      getRes (obj) {
        this.orgCondtionStr = plugins.convertToIn(obj)
      },

      async  getaddress(){
        console.log('开始获取小区')
        let HttpReset = new HttpResetClass()
        var data = await HttpReset.load('POST', 'rs/sql/address_getarealist', {
          data: {
            condition: '1=1'
          }
        }, {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

      },

      clear() {
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = ''
        })
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      }
    },
    computed: {
      getCondition () {
        var con = ""
        if (this.orgCondtionStr) {
          con = `${this.$refs.paged.$refs.cri.condition} and f_filialeid in  (${this.orgCondtionStr})`
        }
        else {
          con = `${this.$refs.paged.$refs.cri.condition}and f_filialeid in ('${this.$login.f.orgid}' )`
        }
        return {
          condition: con
        }
      },
      getfield() {
        return exportConfig.AreaGeneralQuery
      }
    }

  }
</script>
