<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search">
      <div class="flex" v-if="!show">
        <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="col-sm-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;小区&nbsp;&nbsp;&nbsp;</label>
                  <v-select :value.sync="model.f_residential_area"
                            class="select_list select"
                            enter-push
                            multiple
                            condition="a.f_residential_area in {}"
                            v-model="model.f_residential_area"
                            style="width: 60%"
                            :options='$parent.$parent.residentialArea' placeholder='选择小区'
                            close-on-select>
                  </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>
                  <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="col-sm-2 form-group">
                  <label class="font_normal_body">组织机构</label>
                  <res-select
                    restype='organization'
                    @res-select="$parent.$parent.getorg"
                    is-mul="false"
                    :initresid='$parent.$parent.curorgid'
                  >
                  </res-select>
                </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>{{row.f_residential_area}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_building}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.useriddata}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.jibdata}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.kadata}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.wuliandata}}</nobr>
              </td>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
  </div>
</template>

<script>
  import {HttpResetClass, PagedList} from 'vue-client'

  let readySomething = async function (self) {
    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'
  }
  export default {
    title: '小区楼栋统计',
    data() {
      return {
        showinfo: false,
        show:false,
        model: new PagedList('api/af-revenue/sql/areaGeneralQuery', 20, {startDate: 'this.model.startDate', endDate: 'this.model.endDate'}),
        criteriaShow: false,
        //小区
        residentialArea: [],
        // 公司下拉
        curorgid: [this.$login.f.orgid],
        f_orgid: '',
        f_filialeids: this.$login.f.f_orgid
      }
    },
    ready() {
      this.getaddress()
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeids = val[0]
      },
      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_orgid = '${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
      },
      search(){
        this.$refs.paged.$refs.cri.search()
      },
      selfSearch(args) {
        if (this.$refs.paged.$refs.cri.model.startDate === '' || this.$refs.paged.$refs.cri.model.endDate === ''){
          this.$showAlert('请先选择开始时间、结束时间，再进行查询操作！', 'warning', 3000)
        } else {
          if (!this.f_orgid) {
            this.getorg([this.$login.f.orgid])
          }
          args.condition =  `${args.condition} and a.f_filialeid = '${this.f_filialeids}'`
          this.model.search(args.condition, args.model)
        }
      },
      clear() {
        //清空部门和人员
        this.$refs.paged.$refs.cri.$refs.sel.$children[1].selectres = []
        //部门和人员变为全选
        this.$refs.paged.$refs.cri.$refs.sel.$children[1].$children[0].isSelectAll = false
        this.$refs.paged.$refs.cri.$refs.sel.$children[2].$children[0].isSelectAll = false
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })
      },
      show() {
        this.criteriaShow = true
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      }
    },
    events:{
    },
    watch: {
      'model'(val) {
      }
    },
    computed: {
    }
  }
</script>
