<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':!$parent.$parent.createFile,'col-sm-3':$parent.$parent.createFile}"
                  class="form-group">
                  <label class="font_normal_body">所属公司</label>
                  <res-select
                    :initresid='$parent.$parent.curorgid'
                    @res-select="$parent.$parent.getorg"
                    is-mul="false"
                    restype='organization'
                  >
                  </res-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'
                            style="width:60%"
                            :options='$parent.$parent.yongqixing' placeholder='用气性质'
                            close-on-select
                            condition="f_gasproperties = '{}'"></v-select>
                </div>
                <div
                  :class="{'col-sm-2':!$parent.$parent.createFile,'col-sm-4':$parent.$parent.createFile}"
                  class="form-group">
                  <label class="font_normal_body">开户时间</label>
                  <datepicker placeholder="开始日期" v-model="model.startDate" :value.sync="model.startDate" style="width:60%"
                              :format="'yyyy-MM-dd'" condition="f_createfile_date >= '{}'">
                  </datepicker>
                </div>
                <div
                  :class="{'col-sm-2':!$parent.$parent.createFile,'col-sm-4':$parent.$parent.createFile}"
                  class="form-group">
                  <label class="font_normal_body">开户时间</label>
                  <datepicker placeholder="结束日期" v-model="model.endDate" :value.sync="model.endDate" style="width:60%"
                              :format="'yyyy-MM-dd'" condition="f_createfile_date <= '{}'">
                  </datepicker>
                </div>

                <div class="span" style="float:right;">
                  <button class="button_search button_spacing" @click="search()">查询</button>
                  <!--<button class="btn btn-default" @click="$parent.$parent.stamp()">打印</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="userTongjiQuery"
                                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>
            <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_gasproperties}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_zhengchang}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_chaibiao}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_banqian}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_tingbiao}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_weitongqi}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_banli}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_yichang}}</nobr>
              </td>
            </template>
            <template partial='foot'>

              <td style="text-align: center;background-color: mistyrose" ><nobr>
                <span >合计</span>
              </nobr> </td>
              <td style="text-align: center;background-color: mistyrose">
                <nobr>{{model.sums.f_zhengchang}}</nobr>
              </td>
              <td style="text-align: center;background-color: mistyrose">
                <nobr>{{model.sums.f_chaibiao}}</nobr>
              </td>
              <td style="text-align: center;background-color: mistyrose">
                <nobr>{{model.sums.f_banqian}}</nobr>
              </td>
              <td style="text-align: center;background-color: mistyrose">
                <nobr>{{model.sums.f_tingbiao}}</nobr>
              </td>
              <td style="text-align: center;background-color: mistyrose">
                <nobr>{{model.sums.f_weitongqi}}</nobr>
              </td>
              <td style="text-align: center;background-color: mistyrose">
                <nobr>{{model.sums.f_banli}}</nobr>
              </td>
              <td style="text-align: center;background-color: mistyrose">
                <nobr>{{model.sums.f_yichang}}</nobr>
              </td>

            </template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
  </div>
</template>

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

let readySomething = async function (self) {

  self.initParams()
  await self.$MagLoadParams.loadParam()
  self.sumsmodel = self.$refs.paged.$refs.grid.model.sums
}
export default {
  props:['data'],
  data() {
    return {
      data: {},
      other:[],
      footer:[],
      model: new PagedList('api/af-revenue/sql/userTongjiQuery', 20, {}, {
        f_zhengchang: 0,f_chaibiao: 0,f_banqian: 0,f_tingbiao: 0,
        f_weitongqi: 0,f_banli: 0,f_yichang: 0
      }),
      criteriaShow: false,
      orgCondtionStr: '',

      modelval: [],
      yongqixing: this.$appdata.getParam('用气性质') ? [{
        label: '全部',
        value: ''
      }, ...this.$appdata.getParam('用气性质')] : [],
      curorgid: [this.$login.f.orgid],
      f_filialeid: this.$login.f.orgid,
      printshow: false,
      all: false,
      fields: {},
      thead: '',
      tfoot: '',
      show:false,
      rowdata:{},
      meterbrands: [],
      initres: {
        org:[this.$login.f.orgid],
        dep:[],
        user:[],
      },
      selected:{},

      //合计数据
      sumsmodel: {}
    }
  },
  ready() {
    readySomething(this).then(() => {
      this.$emit('ready')
    }).catch((error) => {
      this.$emit('error', error)
    })
  },
  methods: {
    view(row){
      this.showupload = true
      this.selected= row
    },
    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);
    },
    search(){
      this.$refs.paged.$refs.cri.search()
    },
    selfSearch(args) {
      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
      args.condition = `${args.condition}` + this.orgCondtionStr
      this.model.search(args.condition, args.model)
      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] = []
      })
    },
    show() {
      this.criteriaShow = true
    },
    hidden() {
      this.criteriaShow = !this.criteriaShow
    },
    getRes(condition) {
      this.orgCondtionStr = condition
    },
    stamp() {
      this.all = false
      //默认选择要打印的列
      this.modelval = defaultPrint.config
      this.fields = this.getfield
      console.log('所有打印字段', this.fields)
      this.printshow = true
      this.put()
    },
    cancel() {
      this.show = false
    },
    dealmsg(val) {
      console.log('---------------dealmsg')
      this.rowdata=val
      this.show=true
      val.model = this.model.model
      this.$dispatch('deal-msg', val)
    },
    put() {
      // 对Modelval进行排序
      this.sortModelval()
      this.thead = `<tr><th colspan=${this.modelval.length}>补气查询统计报表</th></tr><tr>`
      for (let key of this.modelval) {
        this.thead += '<th>' + this.fields[key] + '</th>'
      }
      this.thead += '</tr>'
    },
    print() {
      this.$refs.print.PrintAsFile()
      this.printshow = false
    },
    close() {
      this.printshow = false
      this.all = false
    },
    // 对选择的列进行排序
    sortModelval() {
      let sortModel = []
      Object.keys(this.fields).forEach((key) => {
        if (this.modelval.includes(key)) {
          sortModel.push(key)
        }
      })
      this.modelval = sortModel
      console.log('选择的打印的字段', this.modelval)
    },
    async updateParams() {
      await this.$MagLoadParams.loadParam(this.f_filialeid)
      this.initParams()
    },
    getorg(val) {
      this.f_filialeid = val[0]
    },
  },
  watch: {
    'all'(val) {
      if (val) {
        this.modelval = this.bodyData
      } else {
        this.modelval = defaultPrint.config
        this.put()
      }
    },
    'modelval.length'() {
      this.put()
    },
    sumsmodel:{
      handler: function(val) {
        this.getotherfooter();
      },
      deep: true
    },
    'orgCondtionStr'(val) {
      let res = val.match(/'(.*?)'/)
      console.log('正则提取:',res && res[1])
      if (res) {
        this.f_filialeid = res[1]
        this.updateParams()
      }
    }
  },
  computed: {
    getCondition() {
      return {condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr}
    },
    getfield() {
      return exportConfig.UserTongjiQuery
    }
  }
}
</script>
