<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search">
      <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">客户编号</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_userinfo_code"
                         condition="f_userinfo_code = '{}' " placeholder="客户编号">
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户名称</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_user_name"
                         condition="f_user_name = '{}'" placeholder="客户名称">
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户地址</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_address"
                         condition="f_address like '%{}%'" placeholder='客户地址'>
                </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"
                            v-model="model.f_residential_area"
                            style="width: 60%"
                            multiple
                            condition="f_residential_area in {}"
                            :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>

                  <export-excel :data="$parent.$parent.getCondition"
                                :field="$parent.$parent.getfield" :header="$parent.$parent.other"
                                sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="lixian_gasAnalysisQuery"
                                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">
                <res-select-group :show-component="$parent.$parent.resshow" :initres="$parent.$parent.initres"
                                  :cascade=true @re-res="$parent.$parent.getRes" v-ref:sel></res-select-group>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">气量百分比</label>
                  <input type="text" style="width:30%" class="input_search" title="大于等于"
                         v-model="model.f_gasprec_small"
                         condition="f_gasprec >= '{}' " placeholder="大于等于">
                  <input type="text" style="width:30%" class="input_search" title="小于等于"
                         v-model="model.f_gasprec_big"
                         condition="f_gasprec <= '{}' " placeholder="小于等于">
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;增量&nbsp;&nbsp;&nbsp;&nbsp;</label>
                  <input type="text" style="width:30%" class="input_search" title="大于等于"
                         v-model="model.f_gasdiff_small"
                         condition="f_gasdiff >= '{}' " placeholder="大于等于">
                  <input type="text" style="width:30%" class="input_search" title="小于等于"
                         v-model="model.f_gasdiff_big"
                         condition="f_gasdiff <= '{}' " placeholder="小于等于">
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;表号&nbsp;&nbsp;&nbsp;</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_meternumber"
                         condition="f_meternumber like '%{}%'" placeholder="表号">
                </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="f_user_type in {}"
                            multiple
                            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="f_gasproperties in {}"
                            multiple
                            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"
                            v-model="model.f_meter_brand"
                            :options='$parent.$parent.meterbrands' placeholder='请选择'
                            condition="f_meter_brand in {}"
                            multiple
                            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"
                            :options='$parent.$parent.meterStyle' placeholder='请选择' v-model="model.f_meter_style"
                            condition="f_meter_type in {}"
                            multiple
                            close-on-select></v-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">抄&nbsp;&nbsp;表&nbsp;员</label>
                  <v-select :value.sync="model.f_inputtor"
                            v-model="model.f_inputtor"
                            :options='$parent.$parent.inputtores' placeholder='请选择'
                            condition="f_inputtor in {}"
                            multiple
                            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%"
                            multiple
                            close-on-select
                            condition="f_meter_book_num in {}"></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.oldmonthstart"
                              :value.sync="model.oldmonthstart"
                              :format="'yyyy-MM-dd'"
                              :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.oldmonthend"
                              :value.sync="model.oldmonthend"
                              :format="'yyyy-MM-dd'"
                              :show-reset-button="true">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label for="startDate" class="font_normal_body">新月开始时间</label>
                  <datepicker id="startDate1" placeholder="新月开始时间" style="width:60%"
                              v-model="model.newmonthstart"
                              :value.sync="model.newmonthstart"
                              :format="'yyyy-MM-dd'"
                              :show-reset-button="true">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label for="endDate" class="font_normal_body">新月结束时间</label>
                  <datepicker id="endDate1" placeholder="新月结束时间" style="width:60%"
                              v-model="model.newmonthend"
                              :value.sync="model.newmonthend"
                              :format="'yyyy-MM-dd'"
                              :show-reset-button="true">
                  </datepicker>
                </div>

                <div class="col-sm-2 form-group" >
                  <label for="startDate" class="font_normal_body" title="建档开始日期">建档时间</label>
                  <datepicker id="startDate2" placeholder="建档开始日期" style="width:60%"
                              v-model="model.startDate"
                              :value.sync="model.startDate"
                              :format="'yyyy-MM-dd'"
                              :show-reset-button="true"
                              condition="f_gas_date >= '{} 00:00:00'">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group" >
                  <label for="endDate" class="font_normal_body" title="建档结束日期">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                  <datepicker id="endDate2" placeholder="建档结束日期" style="width:60%"
                              v-model="model.endDate"
                              :value.sync="model.endDate"
                              :format="'yyyy-MM-dd'"
                              :show-reset-button="true"
                              condition="f_gas_date <= '{} 23:59:59'">
                  </datepicker>
                </div>
              </div>
            </div>
          </criteria>

          <data-grid :model="model" partial='list' class="list_area table_sy" style="height: 95%" v-ref:grid>
            <template partial='head'>
              <tr>
                <th style="text-align: center;">
                  <nobr>用户编号</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>姓名</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>地址</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>联系方式</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>用户类型</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>用户性质</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>气表品牌</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>气表厂家</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>表号</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>抄表员</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>抄表册</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>往月用气</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>本月用气</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>增量</nobr>
                </th>
                <th style="text-align: center;">
                  <nobr>百分比</nobr>
                </th>
              </tr>
            </template>
            <template partial='body'>
              <tr style="position: relative" class="table-bordered">
                <td style="text-align: center;">{{ row.f_userinfo_code }}</td>
                <td style="text-align: center;">{{ row.f_user_name }}</td>
                <td style="text-align: center;">{{ row.f_address }}</td>
                <td style="text-align: center;">{{ row.f_user_phone }}</td>
                <td style="text-align: center;">{{ row.f_user_type }}</td>
                <td style="text-align: center;">{{ row.f_gasproperties }}</td>
                <td style="text-align: center;">{{ row.f_meter_brand }}</td>
                <td style="text-align: center;">{{ row.f_manufacturers }}</td>
                <td style="text-align: center;">{{ row.f_meternumber }}</td>
                <td style="text-align: center;">{{ row.f_inputtor }}</td>
                <td style="text-align: center;">{{ row.f_book_name }}</td>
                <td style="text-align: center;">{{ row.f_oldgas }}</td>
                <td style="text-align: center;">{{ row.f_newgas }}</td>
                <td style="text-align: center;">{{ row.f_gasdiff }}</td>
                <td style="text-align: center;">{{ row.f_gasprec }}%</td>
              </tr>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
  </div>
</template>

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

let readySomething = async function (self) {
  await self.$MagLoadParams.loadParam()
  self.initParams()
  //获取新的抄表册,先去清理所有的抄表册
  self.$GetSaleParam.meterbooks = [];
  //开始加载新的抄表册
  await self.$LoadParams.loadMeterBook(self.f_filialeid)
  self.initmeterbook()
}
export default {
  props: ['data'],
  title: '气量年比查询',
  data () {
    return {
      model: new PagedList('api/af-revenue/sql/lixian_gasAnalysisQuery', 20, {
        oldmonthstart: 'this.model.oldmonthstart',
        oldmonthend: 'this.model.oldmonthend',
        newmonthstart: 'this.model.newmonthstart',
        newmonthend: 'this.model.newmonthend',
      }),
      other: [],
      orgCondtionStr: '',
      resshow: ['company'],
      initres: {
        org: [this.$login.f.orgid]
      },
      meterbrands: [],
      meterbooks:[],
      gasproperties : [],
      inputtores: [{label: '全部',value: ''}],
      criteriaShow: false,
      condition: '',
      //小区
      residentialArea: []
    }
  },
  ready () {
    this.$refs.paged.$refs.cri.model.oldmonthend = this.$login.toStandardDateString()
    this.$refs.paged.$refs.cri.model.newmonthend = this.$login.toStandardDateString()
    console.log('this.$refs.paged.$refs.cri.model.oldmonthend',this.$refs.paged.$refs.cri.model.oldmonthend)
    console.log('this.$refs.paged.$refs.cri.model.newmonthend',this.$refs.paged.$refs.cri.model.newmonthend)
    //加载抄表册
    this.$MagGetSaleParam.initinputtor()
    this.getinputtores()
    this.getaddress()
    readySomething(this).then(() => {
      this.$emit('ready')
    }).catch((error) => {
      this.$emit('error', error)
    })
  },
  methods: {
    async initmeterbook(){
      this.meterbooks = [{label: '全部', value: ''}, ...this.$GetSaleParam.getMeterBooks()]
    },
    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 = []
      let 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', 'api/af-revenue/sql/manage_getarealist', {
        data: {
          condition: `1=1 and s.f_orgid = '${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
    },
    async getinputtores () {
      // 获取抄表员
      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)
        }
      }
      this.inputtores = [{label: '全部', value: ''}, ...rs]
    },
    cancel () {
      this.show = false
    },
    userTypeChange () {
      this.gasproperties = []
      if (this.$refs.paged.$refs.cri.model !== null) {
        this.$refs.paged.$refs.cri.model.f_gasproperties = []
        this.gasproperties = this.$appdata.getParam('用气性质')
      } else {
        this.gasproperties = [{label: '全部', value: ''}]
      }
    },
    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()
    },
    search () {
      this.$refs.paged.$refs.cri.search()
    },
    selfSearch (args) {
      let f_orgstr = this.orgCondtionStr
      args.condition = `${args.condition} ` + f_orgstr
      this.condition = args.condition
      this.model.search(args.condition, args.model)
    },
    clear () {
      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
    },
    async getRes (condition, obj) {
      this.orgCondtionStr = condition
      this.orgname = obj.orgnames[0]
    },
  },
  events: {},
  watch: {},
  computed: {
    getfield () {
      return exportConfig.useGasyearRatioConfig
    },
    getCondition () {
      return {
        oldmonthstart: this.$refs.paged.$refs.cri.model.oldmonthstart,
        oldmonthend: this.$refs.paged.$refs.cri.model.oldmonthend,
        newmonthstart: this.$refs.paged.$refs.cri.model.newmonthstart,
        newmonthend: this.$refs.paged.$refs.cri.model.newmonthend,
        condition: this.condition
      }
    },
    usertypes () {
      return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用户类型')]
    },
    meterStyle () {
      return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('燃气表型号')]
    },


  }
}
</script>

