<template>
  <div>
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <validator name='v' partial>
          <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
            <div class="row">
              <div class="col-sm-3">
                <label class="font_normal_body" style="line-height: 23px;">小区名称</label>
                <input type="text" class="form-control" v-model="model.f_residential_area"
                       placeholder='小区名称' style="width:60%"
                       condition="areaend like '%{}%'">
              </div>
              <div class="col-sm-3">
                <label class="font_normal_body">&nbsp;公&nbsp;&nbsp;&nbsp;&nbsp;司&nbsp;</label>
                <right-tree :userid.sync='$parent.$parent.userid' style="width: 60%;"
                            :source.sync='$parent.$parent.source' @re-res="$parent.$parent.getRes" ></right-tree>
              </div>
              <div class="col-sm-3" style="float: right;">
                <div class="span" style="text-align: center;">
                  <button class="button_search" @click="search()">查询</button>
                  <export-excel   :data="$parent.$parent.searchData"
                                  :field="$parent.$parent.excelHeaders"
                                  sqlurl="api/af-safecheck/logic/exportfile" sql-name="safeCheckHanderAreaStatistics"
                                  template-name='移动抄表小区统计'></export-excel>
                  <div class="button_spacing" style="margin-right: 10px;float: right;"
                       :class="{'button_shrink_left':$parent.$parent.$parent.showItem,'button_shrink_right':!$parent.$parent.$parent.showItem}"
                       @click="$parent.$parent.hiddenr()">
                  </div>
                  <div class="button_spacing"  style="margin-right: 10px;float: right;"
                       :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                       @click="$parent.$parent.hidden()">
                  </div>
                </div>
              </div>
            </div>
            <div class="row" v-show="$parent.$parent.criteriaShow">
              <div class="col-sm-3">
                <label class="font_normal_body" style="line-height: 23px;">开始时间</label>
                <datepicker id="startdate"
                            v-model='$parent.$parent.startDate'
                            :value.sync="$parent.$parent.startDate"
                            :disabled-days-of-Week="[]"
                            placeholder="开始时间"
                            :format="'yyyy-MM-dd'">
                </datepicker>
              </div>
              <div class="col-sm-3">
                <label class="font_normal_body" style="line-height: 23px;">结束时间</label>
                <datepicker id="enddate"
                            v-model='$parent.$parent.endDate'
                            :value.sync="$parent.$parent.endDate"
                            :disabled-days-of-Week="[]"
                            placeholder="结束时间"
                            :format="'yyyy-MM-dd'">
                </datepicker>
              </div>
            </div>
            <div class="row">
              <label class="font_normal_body" style="margin: 0px">
                户均用气量:{{$parent.$parent.familyAVGGasConsumption}}&nbsp;&nbsp;&nbsp;
                户均剩余气量:{{$parent.$parent.familyAVGRemainingGas}}&nbsp;&nbsp;&nbsp;
              </label>
              <label class="font_normal_body" style="margin: 0px">
                合计用气量:{{$parent.$parent.totalGasConsumption}}&nbsp;&nbsp;&nbsp;
                合计剩余气量:{{$parent.$parent.totalRemainingAmount}}&nbsp;&nbsp;&nbsp;
                已抄表户数:{{$parent.$parent.countResLeanth}}
              </label>
            </div>
          </div>
        </validator>
      </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>
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align:center">{{ $index + 1 }}</td>

            <td style="text-align:center">
              <nobr>{{ row.areaend }}</nobr>
            </td>
            <td style="text-align:center">
              <nobr>{{ row.areacount }}</nobr>
            </td>
            <td style="text-align:center">
              <nobr>{{ row.handcount }}</nobr>
            </td>
            <td style="text-align:center">
              <nobr>{{ row.nohandcount }}</nobr>
            </td>
          </tr>
        </template>
        <template partial='foot'>
          <tr>
            <td colspan="2" style="text-align:center">合计</td>
            <td style="text-align:center">
              <nobr>{{ $parent.$parent.$parent.areaSumCount}}</nobr>
            </td>
            <td style="text-align:center">
              <nobr>{{ $parent.$parent.$parent.areaSumHandCount}}</nobr>
            </td>
            <td style="text-align:center">
              <nobr>{{ $parent.$parent.$parent.areaSumHandNoCount }}</nobr>
            </td>
          </tr>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
  import {PagedList, HttpResetClass} from 'vue-client'
  import * as Util from '../Util'
  export default {
    title: "安检抄表按小区统计",
    data () {
      let model = new PagedList('api/af-safecheck/sql/safeCheckHanderAreaStatistics', 20, {
        f_filialeid_id: 'this.f_filialeid_id',
        startDate: 'this.startDate',
        endDate: 'this.endDate',
      })
      model.f_filialeid_id = `('${this.$login.f.orgid}')`
      model.startDate = ''
      model.endDate = ''
      return {
        startDate: Util.toStartDateString(),
        endDate: Util.toEndDateString(),
        f_filialeid_id:`('${this.$login.f.orgid}')`, //分公司id
        model: model,
        totalGasConsumption: 0,//合计用气量
        totalRemainingAmount: 0, //合计剩余量
        countRes: null, //统计数据的结果集
        excelHeaders:{
          'areaend': '小区名称',
          'areacount': '总户数',
          'handcount': '已抄表户数',
          'nohandcount': '未抄表户数',
        },
        searchData: {
          condition:"1=1",
          f_filialeid_id: '',
          startDate:'',
          endDate:'',
        },
        criteriaShow:false,
        userid: this.$login.f.id,
        source: `tool.getFullTree(this.getRights().where(row.getType() != $zone$).where(row.getType() == $organization$))`,
      }
    },
    ready () {
      this.model.startDate = this.startDate
      this.model.endDate = this.endDate
    },
    methods: {
      getRes(obj) {
        this.f_filialeid_id = this.$login.convertToIn(obj.resids);
        // this.model.f_filialeid_id = this.$login.convertToIn(obj.resids);
      },
      hidden(){
        this.criteriaShow = !this.criteriaShow;
      },
      selfSearch (args) {
        this.model.f_filialeid_id = this.f_filialeid_id; //只有选择了
        this.model.startDate = this.startDate ? this.startDate+' 00:00:00' : this.startDate
        this.model.endDate = this.endDate ? this.endDate+' 23:59:59' : this.endDate
        this.searchData.condition = args.condition; // 拼接导出的条件
        this.searchData.f_filialeid_id = this.model.f_filialeid_id; // 拼接导出的条件
        this.searchData.startDate = this.model.startDate; // 拼接导出的条件
        this.searchData.endDate = this.model.endDate; // 拼接导出的条件
        this.$emit("clearright")
        this.searchCountCheckHandPlan() //点击查询后 同时去查询统计
        return this.model.search(args.condition, args.model)
      },
      hiddenr () {
        this.$parent.showItem = !this.$parent.showItem
      },
      // 查询每个用户 在指定时间内的开始表底数，结束表底数
      searchCountCheckHandPlan () {
        let http = new HttpResetClass()
        let requestDate = {startDate:this.startDate ? this.startDate+' 00:00:00' : this.startDate,endDate:this.endDate ? this.endDate+' 23:59:59' : this.endDate,f_filialeid_id: `${this.$login.f.orgid}`}
        http.load("POST","/api/af-safecheck/sql/safeCheckHanderToCount", {data:requestDate},{resolveMsg: null, rejectMsg: null}).then((res)=>{
          this.countRes = res.data
        })
      }
    },
    computed: {
      selected() {
        return this.$refs.paged.$refs.grid.selected
      },
      // 户均用气量
      familyAVGGasConsumption() {
        if (this.countRes && this.countRes!=0) {
          let resCount = 0;  // 所有已统计用户的用气量
          let countResLeanthOne = this.countRes.length;
          for (let i = 0; i < countResLeanthOne; i++) {
            // 最后一次抄表底数 - 第一次的超表底数 = 已用气量
            resCount+= this.countRes[i].maxdate_tablebase - this.countRes[i].mindate_tablebase
          }
          this.totalGasConsumption = resCount.toFixed(3) //合计用气量
          return (resCount / countResLeanthOne).toFixed(3); //户均用气量=这段时间内所有用户的已用气量  比 这段时间内统计的用户数
        }else {
          this.totalGasConsumption = 0 //合计用气量
          return 0
        }
      },
      countResLeanth(){
        return 0 + this.countRes.length;
      },
      // 户均剩余气量
      familyAVGRemainingGas() {
        if (this.countRes && this.countRes!=0) {
          let resCount = 0;  // 所有最后一次统计的用户剩余气量
          let countResLeanthOne = this.countRes.length;
          for (let i = 0; i < countResLeanthOne; i++){// 最后一次统计的剩余气量之和 -比
            resCount += this.countRes[i].f_endsurplus_gas
          }
          this.totalRemainingAmount = resCount.toFixed(3) //合计剩余气量
          return  (resCount / countResLeanthOne).toFixed(3); // 户均剩余气量= 所选时间内用户最后一次统计所计算出来的剩余量  比 这段时间内统计的用户数
        }else {
          this.totalRemainingAmount = 0 //合计剩余气量
          return 0
        }
      },
      //总户数 areacount
      areaSumCount() {
        let sum = 0
        if (this.model.rows.length === 0) {
          return sum;
        }else {
          for (let i = 0; i < this.model.rows.length; i++) {
            sum += this.model.rows[i].areacount
          }
          return sum;
        }
      },
      // 已抄表户数 handcount
      areaSumHandCount() {
        let sum = 0
        if (this.model.rows.length === 0) {
          return sum;
        }else {
          for (let i = 0; i < this.model.rows.length; i++) {
            sum += this.model.rows[i].handcount
          }
          return sum;
        }
      },

      // 未抄表户数 handnocount
      areaSumHandNoCount() {
        let sum = 0
        if (this.model.rows.length === 0) {
          return sum;
        }else {
          for (let i = 0; i < this.model.rows.length; i++) {
            sum += this.model.rows[i].nohandcount
          }
          return sum;
        }
      }


    },
    watch: {

    }
  }
</script>

<style scoped>
  #startdate {
    width: 60%;
  }
  #enddate {
    width: 60%;
  }
</style>
