<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 class="font_normal_body">组织机构</label>
                  <right-tree @re-res="$parent.$parent.getRes"></right-tree>
                </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 HH:mm:ss'"
                              :show-reset-button="true"
                              condition="h.f_hand_date >= '{}'">
                  </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"
                              condition="h.f_hand_date <= '{}'">
                  </datepicker>


                </div>    <!-- 选择传入统计参数-->
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">统计类型</label>

                  <v-select :value.sync="model.group1"
                            v-model="model.group1"
                            :options='$parent.$parent.groups'
                            @change="$parent.$parent.Statistical_TypeChange()"
                            close-on-select></v-select>
                </div>
<!--                  <v-select :value.sync="$parent.$parent.group1" -->
<!--                  v-model="$parent.$parent.group1"-->
   <!--               placeholder=model.group1-->
<!--                :placeholder='model.group1'-->
<!--                @change="$parent.$parent.leixinChange()"-->
                <div class="span" style="float:right;">
                  <!--<read-idcard></read-idcard>-->
                  <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="MeterRateQuery"
                                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 class="row" v-show="$parent.$parent.criteriaShow">

                <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">客户类型</label>
                  <v-select :value.sync="model.f_user_type"
                            @change="$parent.$parent.userTypeChange()"
                            :options='$parent.$parent.usertypes' placeholder='请选择' v-model="model.f_user_type"
                            condition="uf.f_user_type = '{}'"
                            close-on-select></v-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">用气性质</label>
                  <v-select :value.sync="model.f_gasproperties" v-model="model.f_gasproperties"
                            :options='$parent.$parent.gasproperties' placeholder='请选择'
                            condition="uf.f_gasproperties = '{}'"
                            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='抄表员'-->
                <!--close-on-select-->
                <!--v-el:cc></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"
                            :options='$parent.$parent.inputtor' placeholder='请选择'
                            multiple = "true"
                            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_meter_book" v-model="model.f_meter_book"-->
                <!--:options='$parent.$parent.meterbooks' placeholder='抄表册'-->
                <!--style="width:60%"-->
                <!--close-on-select-->
                <!--condition="uf.f_meter_book_num = '{}'"></v-select>-->
                <!--</div>-->
                <!--<div class="col-sm-2 form-group">-->
                <!--<label class="font_normal_body">气价类型</label>-->
                <!--<v-select :value.sync="model.pricetype" v-model="model.pricetype"-->
                <!--placeholder='气价类型' :options="$parent.$parent.pricetypes"-->
                <!--close-on-select v-ref:type>-->
                <!--</v-select>-->
                <!--</div>-->
                <!--<div class="col-sm-2 form-group">-->
                <!--<label class="font_normal_body">气价名称</label>-->
                <!--<v-select :value.sync="model.f_price_name"-->
                <!--v-model="model.f_price_name"-->
                <!--:options="$parent.$parent.getPricenames"-->
                <!--condition="f_price_name = '{}'"-->
                <!--close-on-select></v-select>-->
                <!--</div>-->

                <!--<div class="col-sm-2 form-group">-->
                <!--<label class="font_normal_body">气表类型</label>-->
                <!--<v-select :value.sync="model.f_meter_type" multiple-->
                <!--:options='$parent.$parent.metertypes' placeholder='请选择' v-model="model.f_meter_type"-->
                <!--condition="f_meter_type in {}"-->
                <!--close-on-select></v-select>-->
                <!--</div>-->
                <!--<div class="col-sm-2 form-group">-->
                <!--<label class="font_normal_body">气表品牌</label>-->
                <!--<v-select :value.sync="model.f_meter_brand" multiple-->
                <!--v-model="model.f_meter_brand"-->
                <!--:options='$parent.$parent.meterbrands' placeholder='请选择'-->
                <!--condition="f_meter_brand in {}"-->
                <!--close-on-select></v-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>-->
                  <div v-if="model.model.group1[0]==='f_inputtor'">
                    <nobr >抄表员</nobr>
                  </div>
                  <div v-if="model.model.group1[0]==='f_meter_book_num'">
                    <nobr >抄表册</nobr>
                  </div>
                  <div  v-if="model.model.group1[0]==='f_gasproperties'">
                    <nobr >用气类型</nobr>
                  </div>

                </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>
                <th>
                  <nobr>期间实收气费金额</nobr>
                </th>
                <th>
                  <nobr>已收户数</nobr>
                </th>
                <th>
                  <nobr>收费率</nobr>
                </th>
              </tr>
            </template>
            <template partial='body'>
<!--              <td style="text-align: center;"  v-if="model.group1=='f_inputtor'">-->
<!--                <nobr >{{row.f_inputtor}}</nobr>-->
<!--              </td>-->

                <td style="text-align: center;"  >

                  <div v-if="model.group1[0]='f_inputtor'">
                    <nobr >{{row.f_inputtor}}</nobr>
                  </div>
                  <div  v-if="model.group1[0]='f_gasproperties'">
                    <nobr >{{row.f_gasproperties}}</nobr>
                  </div>
                  <div v-if="model.group1[0]='f_meter_book_num'">
                    <nobr >{{row.f_meter_book_num}}</nobr>
                  </div>
                </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_out_hand}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_hand_num}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_hand_lv}}</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_debt_money}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_sell_hand}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_sell_lv}}</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_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_no_hand}}
            </td>
            <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
              已抄无用量合计:&emsp;{{sumsmodel.f_out_hand}}
            </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>
            <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_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.f_sell_hand}}
            </td>
          </tr>
        </table>
      </div>
      <!--<div v-if="show">-->
      <!--<user-info-detail-manage :f_userinfo_id="rowdata.f_userinfo_id" @cancel-main="cancel"></user-info-detail-manage>-->
      <!--</div>-->
    </div>
  </div>
</template>

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

  let readySomething = async function (self) {
    //group1必须使用数组传入
    self.$refs.paged.$refs.cri.model.group1=['f_inputtor']
    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'
    // self.$refs.paged.$refs.cri.search()
    self.defaultfield = [...self.defaultfield, ...self.config.defaultPrint]
    await self.$MagLoadParams.loadParam()
    self.initParams()
    self.sumsmodel = self.$refs.paged.$refs.grid.model.sums
  }
  export default {
    data() {
      return {

        config: {
          defaultPrint: []
        },
        //group1:['f_inputtor'],
        groups:[{label:'抄表员',value:'f_inputtor'},{label:'用气性质',value:'f_gasproperties'},{label:'抄表册',value:'f_meter_book_num'}],
        other: [],
        footer: [],
        //f_price_name:[],
        gasproperties:[],
        showinfo: false,
        data: {},
        show:false,
        rowdata:{},
        initres: {
          org:[this.$login.f.orgid],
          dep:[],
          user:[]
        },
        //在此传参数到后台服务
        //抄表员字段  f_inputtor
        //用气属性    f_gasproperties
        //抄表册字段  f_meter_book_num
        model: new PagedList('api/af-revenue/sql/MeterRateQuery', 20,{
          grouping:'this.model.group1[0]'
        }, {
          f_user_number: 0,
          f_no_hand: 0,
          f_out_hand: 0,
          f_hand_num :0,
          f_oughtamount: 0,
          f_oughtfee: 0,
          f_debt_money:0,
          f_sell_hand:0
        }),
        criteriaShow: false,
        orgCondtionStr: '',

        bbb:'',
        // 下拉框
        meterbrands: [],
        meterbooks:[{label: '全部',value: ''}],
        pricenames: [],
        prices: [],
        area: [],
        //合计数据
        defaultfield: [],
        //小区
        residentialArea: [],
        sumsmodel: {},
        inputtouPerson: [],
        inputtores2: [], // 下发时使用自定义抄表员,
        f_filialeid: this.$login.f.f_orgid,
        f_orgid: this.$login.f.orgid,
        f_depid: this.$login.f.depids,
        f_operatorid: this.$login.f.id
      }
    },
    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);
      },
      async  getaddress(){
        // await self.$LoadParams.loadMeterBook(self.f_filialeid)
        // self.loadMeterBooks()
        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: '获取小区失败!'})
        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
      },




      //把数据库查询数据转换为下拉数据
      calculate(rows){
        let data = []
        rows.forEach((row, n) => {
          data[n] = {label: row.f_residential_area, value: row.id}
        })
        return data
      },

      search(){
        this.$refs.paged.$refs.cri.search()
      },
      cancel() {
        this.show = false
      },
      showmsg(obj){
        this.rowdata=obj
        this.show=true
      },
      userTypeChange () {
        this.gasproperties=[]
        if(this.$refs.paged.model.model !==null) {
          this.$refs.paged.model.model.f_gasproperties=''

          console.log(this.$refs.paged.model.model.f_user_type[0])
          this.gasproperties = this.$appdata.getParam(this.$refs.paged.model.model.f_user_type[0])
          // this.gasproperties.push({label: '全部', value: ''})
        }
        else{
          this.gasproperties =[{label: '全部', value: ''}]
        }
      },
      //Statistical_TypeChange ()根据选择的统计类型改变统计分组条件，只改变，不传递。
      Statistical_TypeChange () {
        this.model.group1=''
        //console.log("44444444444444444444444444444444")
        this.group1 = this.$refs.paged.$refs.cri.model.group1
        //console.log("44444444444444444444444444444444")
        // document.getElementById("Statistical_type").innerHTML="<nobr>{{row."+this.group1+"}}</nobr>"
        //console.log(getElementById("Statistical_type").value)
          // this.gasproperties.push({label: '全部', value: ''})
        console.log("555555555555555555555")
        console.log(this.group1)
        console.log("666666666666666666666")
        console.log(this.$refs.paged.$refs.cri.model.group1)
      },

      initParams() {
        // 初始化气表品牌
        let brandArr = []
        this.$MagGetSaleParam.getGasbrand().forEach((item) => {
          let temp = {}
          temp.label = item.label
          temp.value = item.value.f_meter_brand
          brandArr.push(temp)
        })
        this.meterbrands = [{label: '全部', value: ''}, ...brandArr]
        //初始化气表价格
        this.prices = this.$MagGetSaleParam.getPrices();
      },
      selfSearch(args) {
        //抄表员查询条件
        console.log("1111111111111111111")
        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 h.f_inputtor in  ( ${str} )`
        }

        console.log("2222222222222222222")
        //小区查询条件
        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 ua.f_residential_area in  ( ${str} )`
        }
        console.log("33333333333333333333")
        let f_orgstr = this.orgCondtionStr + 'and uf.f_orgid = ' + this.f_orgid
        args.condition = `${args.condition} ` + f_orgstr
        console.log("==========================")
        //console.log(this.$refs.paged.$refs.cri.model.group1)
        //console.log(this.group1)
        //this.model.paramSource = {group: `${this.group1[0]}`}
        this.model.search(args.condition, args.model)
        console.log("3333333333333")
        this.sumsmodel = this.$refs.paged.$refs.grid.model.sums
      },
      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] = []
        })

      },
      // loadMeterBooks(){
      //   this.meterbooks = [...this.meterbooks, ...this.$GetSaleParam.getMeterBooks()]
      // },
      dealmsg(val) {
        val.model = this.model.model
        this.$dispatch('deal-msg', val)
      },
      show() {
        this.criteriaShow = true
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      getRes (obj) {
        this.orgname = obj.res[0]
        this.depresid = obj.resids
        this.f_orgid = obj.resids
      },
      async updateParams() {
        await this.$MagLoadParams.loadParam(this.f_filialeid)
        this.initParams()
      }

    },
    events:{
      'getidcard'(IdCard){
        // console.log(param)
        this.$refs.paged.$refs.cri.model.f_info_idnumber=IdCard.strID
      }
    },
    watch: {
      'model'(val) {
      },
      'orgCondtionStr'(val) {
        let res = val.match(/'(.*?)'/)
        console.log('正则提取:',res && res[1])
        if (res) {
          this.f_filialeid = res[1]
          this.updateParams()
        }
      },
      sumsmodel: {
        handler: function (val) {
          this.getotherfooter();
        },
        deep: true
      }
    },
    computed: {
      getPricenames() {
        let f_user_type = this.$refs.paged.model.model.f_user_type;
        let f_gasproperties = this.$refs.paged.model.model.f_gasproperties;
        let pricetype = this.$refs.paged.model.model.pricetype;
        if(this.$refs.paged.model.model !==null) {
          let rs = []
          let priceArr = []
          if (f_user_type.length > 0 && f_gasproperties.length > 0 && pricetype.length >0) {
            let params = {
              f_user_type: f_user_type[0],
              f_gasproperties: f_gasproperties[0],
              f_price_type: pricetype[0],
              filter: this.f_filialeid,
              prices: this.prices
            }
            this.$MagGetSaleParam.getPrice(params).forEach((item) => {
              let temp = {}
              temp.label = item.label
              temp.value = item.value.f_price_name
              priceArr.push(temp)
            })
            rs = [{label: '全部', value: ''}, ...priceArr]
          }
          if (rs.length === 0) {
            this.$refs.paged.model.model.f_price_name = ''
          }
          return rs
        }
      },
      //,grouping: 'this.model.group1[0]'
      getCondition() {
        return {condition: `${this.$refs.paged.$refs.cri.condition}`,grouping: `${this.$refs.paged.$refs.cri.model.group1[0]}` + this.orgCondtionStr}
      },
      inputtor() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('抄表员')]
      },
      getfield() {
        if (this.$refs.paged.$refs.cri.model.group1[0]==='f_inputtor') {
          return exportConfig.f_inputtorConfig
        }
        else if (this.$refs.paged.$refs.cri.model.group1[0]==='f_gasproperties') {
          return exportConfig.handGasPropertiesConfig
        }
        else if (this.$refs.paged.$refs.cri.model.group1[0]==='f_meter_book_num') {
          return exportConfig.f_meter_book_numConfig
        }
      },
      pricetypes() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('气价类型')]
      },
      usertypes() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用户类型')]
      }
    }
  }
</script>
