<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  @sort="sort">
          <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 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 00:00:00'"
                              :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.endDate"
                              :value.sync="model.endDate"
                              :format="'yyyy-MM-dd 23:59:59'"
                              :show-reset-button="true">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;公司&nbsp;&nbsp;&nbsp; </label>
                  <right-tree @re-res="$parent.$parent.getRes"></right-tree>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;部门&nbsp;&nbsp;&nbsp; </label>
                  <res-select restype='department'
                              @res-select="$parent.$parent.getdep"
                              :parentresid="$parent.$parent.depresid"
                              :initresid='$parent.$parent.depid'>
                  </res-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;人员&nbsp;&nbsp;&nbsp; </label>
                  <res-select restype='user'
                              is-mul ="false"
                              @res-select="$parent.$parent.getuser"
                              :parentresid="$parent.$parent.userresid"
                              :initresid='$parent.$parent.operatorid'>
                  </res-select>
                </div>
                <div class="span" style="float:right;">
                  <button class="button_search button_spacing" @click="search()">查询</button>
                </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>
              </tr>
            </template>
            <template partial='body'>
              <td style="text-align: center;">
                <nobr>{{row.f_devices_type}}</nobr>
              </td>
              <td style="text-align: center;"><nobr>
                <nobr>{{row.f_number}}</nobr>
              </nobr> </td>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
    </div>
</template>
<script>
import {PagedList} from 'vue-client'
import {methods} from 'babel-plugin-transform-runtime/lib/definitions'

let readySomething = async function (self) {
  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.sumsmodel = self.$refs.paged.$refs.grid.model.sums
}
export default {
  title: '用气统计报表',
  props: ['data'],
  data () {
    return {
      model: new PagedList('api/af-revenue/sql/gasDeviceStatistics', 20,{startDate: 'this.model.startDate', endDate: 'this.model.endDate', f_orgid: 'this.model.f_orgid'}),
      show: false,
      depresid: [],
      userresid: [],
      f_orgid: this.$login.f.orgid,
      f_depid: this.$login.f.depids,
      f_operatorid: this.$login.f.id,
      operatorid: [],
      depid: [],
      orgname: '',
      depname: [],
      operatorname: '',
      orgCondtionStr: '1=1',
      startDate: '',
      endDate: '',
      resshow: ['company', 'department', 'operator', 'slicearea'],
      orderitem: 'f_operate_date desc'
    }
  },
  ready() {
    readySomething(this).then(() => {
      this.$emit('ready')
    }).catch((error) => {
      this.$emit('error', error)
    })
  },
  methods: {
    search(){
      this.$refs.paged.$refs.cri.search()
    },
    selfSearch(args) { // 调用sql文件
      let f_dep=this.f_depid
      let f_org=this.f_orgid
      let f_oper = this.f_operatorid
      let orgcondition = ''
      if (f_org[0]) {
        orgcondition += "f_orgid in('"+f_org[0]+"'"
        for( let i=1;i<f_org.length;i++){
          orgcondition += ",'"+f_org[i]+"'"
        }
        orgcondition +=') '
      }else if (f_org[0]==='' || f_org[0]===undefined) {
        orgcondition = ' 1=1 '
      }else {
        orgcondition = ' f_orgid = ' + this.f_orgid
      }
      if (f_dep[0]) {
        orgcondition += " and f_depid in('"+f_dep[0]+"'"
        for( let i=1;i<f_dep.length;i++){
          orgcondition += ",'"+f_dep[i]+"'"
        }
        orgcondition +=') '
      }
      if(f_oper[0]) {
        orgcondition += "and uf.f_operatorid in('"+f_oper[0]+"'"
        for( let i=1;i<f_oper.length;i++){
          orgcondition += ",'"+f_oper[i]+"'"
        }
        orgcondition +=') '
      }
      this.$refs.paged.$refs.cri.model.f_orgid = orgcondition
      args.condition = orgcondition + 'and'  + args.condition
      console.log("condition:" + args.condition)
      this.startDate=this.$refs.paged.$refs.cri.model.startDate.substring(0,10)
      this.endDate=this.$refs.paged.$refs.cri.model.endDate.substring(0,10)
      this.model.search(args.f_orgid, args.model, args.condition)
    },
    getRes (obj) {
      this.orgname = obj.res[0]
      this.depresid = obj.resids
      this.f_orgid = obj.resids
    },
    getdep (obj, val) {
      this.depname = val
      this.userresid = obj
      this.f_depid = obj
    },
    getuser ( obj, val) {
      this.operatorname = val[0]
      this.f_operatorid = obj
    },
  },
  computed: {

  }
}
</script>
