<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search" v-if="!show">
      <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">
                <res-select-group :initres="$parent.$parent.initres"  @re-res="$parent.$parent.getRes" v-ref:sel></res-select-group>
                <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">&nbsp;至&nbsp;&nbsp;&nbsp;&nbsp;&ensp;</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">小区名称</label>
                  <v-select :value.sync="model.f_residential_area"
                            class="select_list select"
                            enter-push
                            multiple = "true"
                            v-model="model.f_residential_area"
                            style="width: 60%"
                            :options='$parent.$parent.residentialArea' placeholder='选择小区'
                            close-on-select>

                  </v-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">抄&ensp;表&ensp;员</label>
                  <v-select :value.sync="model.f_inputtor" v-model='model.f_inputtor'
                            style="width:60%"
                            multiple="true"
                            :options='$parent.$parent.inputtores' placeholder='抄表员'
                            condition=""
                            close-on-select
                            v-el:cc></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" :footer="$parent.$parent.footer"
                                :field="$parent.$parent.getfield" :header="$parent.$parent.other"
                                sqlurl="rs/logic/saleExport" sql-name="salehandplanRate" template-name='抄表率查询导出'
                                :choose-col="true"></export-excel>
                </div>
              </div>
            </div>
          </criteria>

          <data-grid :model="model" partial='list' class="list_area table_sy"  v-ref:grid :classname="$parent.classname">
            <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>
                <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_user_number}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_no_hand}}</nobr>
              </td>
<!--              <td style="text-align: center;">-->
<!--                <nobr>{{row.f_no_hand}}</nobr>-->
<!--              </td>-->
              <td style="text-align: center;">
                <nobr>{{row.f_out_hand}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_hand_num}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_oughtamount}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_oughtfee}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_hand_lv}}</nobr>
              </td>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>

        <table class="table-hover">
          <tr style="position: relative;margin-top: -16px;" 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_oughtamount}}
            </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_user_number}}
            </td>
            <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
              实抄户数:&emsp;{{sumsmodel.f_hand_num}}
            </td>
          </tr>
        </table>
      </div>
    </div>
    <work-busy :is-busy="iswork" v-show="iswork"></work-busy>
  </div>
</template>

<script>

import {PagedList, HttpResetClass} from 'vue-client'

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'
  await self.$getConfig(self, 'HandplanQuery')
  await self.$LoadParams.loadParam()
  console.log('开始查询')
  self.sumsmodel = self.$refs.paged.$refs.grid.model.sums
}
export default {
  props: ['data'],
  title: '机表抄表率查询',
  data () {
    return {
      iswork: false,
      other: [],
      footer: [],
      data: {},
      model: new PagedList('rs/sql/salehandplanRate', 20, {startDate: 'this.model.startDate', endDate: 'this.model.endDate'}, {
        f_oughtamount: 0,
        f_oughtfee: 0,
        f_user_number: 0,
        f_hand_num: 0
      }),
      initres: {
        org: [this.$login.f.orgid],
        dep: [],
        user: []
      },
      show: false,
      rowdata: {},
      criteriaShow: false,
      residentialArea: [],
      orgCondtionStr: '',
      modelval: [],
      inputtouPerson: [],
      f_filialeid: this.$login.f.f_orgid,
      // 合计数据
      sumsmodel: {},
      config: {
        defaultPrint: [],
        excelHeaders: {
          'f_residential_area': '小区名称',
          'f_user_number': '应抄户数',
          'f_no_hand': '未抄户数',
          'f_out_hand': '已抄无用量',
          'f_hand_num': '实抄户数',
          'f_oughtamount': '用气量',
          'f_oughtfee': '用气金额',
          'f_hand_lv': '抄表率'
        }
      },
      inputtores: [{label: '全部', value: ''}],  // 抄表员
      condition: ' 1 = 1'
    }
  },
  ready () {
    this.getaddress()
    readySomething(this).then(() => {
      this.$emit('ready')
    }).catch((error) => {
      this.$emit('error', error)
    })
  },
  methods: {
    async getRes (condition, obj) {
      console.log('condition:----------', condition)
      console.log('obj:----------', obj)
      this.orgCondtionStr = condition
    },
    getotherfooter () {
      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)
    },
    async  getaddress () {
      let HttpReset = new HttpResetClass()
      var data = await HttpReset.load('POST', 'rs/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) {
        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.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
      }
      this.area = this.$refs.paged.$refs.cri.model.f_residential_area
      if (this.area.length !== 0) {
        let str = JSON.stringify(this.area)
        str = str.replace(/"/g, `'`)
        str = str.replace(/\[/g, ``)
        str = str.replace(/\]/g, ``)
        // 查询多个小区时条件
        args.condition += ` and f_residential_area in  ( ${str} )`
      }
      // 抄表员查询条件
      this.inputtouPerson = this.$refs.paged.$refs.cri.model.f_inputtor
      if (this.inputtouPerson.length !== 0) {
        let str = JSON.stringify(this.inputtouPerson)
        str = str.replace(/"/g, `'`)
        str = str.replace(/\[/g, ``)
        str = str.replace(/\]/g, ``)
        // 查询多个抄表员时条件
        args.condition += ` and f_inputtor in  ( ${str} )`
      }

      args.condition = `${args.condition} and f_meter_classify = '机表' ` + this.orgCondtionStr
      this.condition = args.condition
      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] = []
      })
    },
    show () {
      this.criteriaShow = true
    },
  },
  watch: {
    'model.rows' (val) {
      if (val.length === 0) {
        this.sumsmodel = {}
      }
    },
    'modelval.length' () {
      this.put()
    },
    sumsmodel: {
      handler: function (val) {
        this.getotherfooter()
      },
      deep: true
    }
  },
  computed: {
    getCondition () {
      return {
        startDate: this.$refs.paged.$refs.cri.model.startDate,
        endDate: this.$refs.paged.$refs.cri.model.endDate,
        condition: this.condition
      }
    },
    getfield () {
      return this.config.excelHeaders
    },
    // inputtor() {
    //   return [{label: '全部', value: ''}, ...this.$appdata.getParam('抄表员')]
    // },
    getPricenames () {
      let f_filialeid = this.f_filialeid
      let result = []
      this.$GetSaleParam.prices.forEach((item) => {
        if (item.f_state === '有效' && f_filialeid == item.f_filialeid) {
          let value = {
            label: item.f_price_name,
            value: item
          }
          result.push(value)
        }
      })
      return Array.from(new Set(result))
    },
    inputtores () {
      // 获取抄表员
      console.log('获取抄表员', this.$login.f)
      let rs = []
      if (this.$login.f.f_gasman.length > 0) {
        for (let i = 0; i < this.$login.f.f_gasman.length; i++) {
          let temp = {
            label: this.$login.f.f_gasman[i].name,
            value: this.$login.f.f_gasman[i].name
          }
          rs.push(temp)
        }
      }
      return [...rs]
    }
  },
  events: {
  }
}
</script>
