<template>
  <div id="unit" class="flex-row">
    <div class="basic-main">
    <div class="flex">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri v-show="$parent.criteriaShow">
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <div class="col-sm-2">
              <label class="font_normal_body">公&emsp;&emsp;司</label>
              <right-tree @re-res="$parent.$parent.getorg"
                          :initresid='initres.org'></right-tree>
            </div>
            <div class="col-sm-2" >
              <label for="YeadDate" class="font_normal_body">统计年份</label>
              <datepicker  placeholder="结束日期" style="width:60%"
                           v-model="model.YeadDate"
                           :value.sync="model.YeadDate"
                           :format="'yyyy'"
                           :show-reset-button="true"
                           :select-year="true">
              </datepicker>
            </div>
          </div>
          <div class="row">
            <div class="span" style = "float:right;margin-top:10px">
              <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"  :sumname="$parent.$parent.sumName"
                            sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="aode_LifeYearByChargeType" template-name='悦生活年报表'  :choose-col="true"></export-excel>

              <life-year-print :countmodel="{sumsmodel:$parent.$parent.model.sums,model:$parent.$parent.model,orgname:$parent.$parent.orgname}"></life-year-print>

            </div>
          </div>
        </div>
      </criteria>
      <data-grid  :model="model" partial='list' class="list_area table_sy"  @dblclick="$parent.dblclick" v-ref:grid>
        <template partial='head'>
          <tr>
            <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_operate_date}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_count}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_pregas}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_collection}}</nobr></td>
        </template>
        <template partial='foot'>
          <td style="text-align: center;background-color: mistyrose"><nobr>合计：</nobr></td>
          <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_count}}</nobr></td>
          <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_pregas}}</nobr></td>
          <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_collection}}</nobr></td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
  </div>
  </div>
</template>

<script>
  import { PagedList } from 'vue-client'
  import exportConfig from '../../../config/exportConfig'
  import LifeYearPrint from "../widget/LifeYearPrint";

  let readySomething = async function (self) {
    let year = new Date().getFullYear()
    self.$refs.paged.$refs.cri.model.YeadDate = year+""
  }

  export default {
    title: '悦生活年报',
    components: {
      LifeYearPrint
    },
    data() {
      return {
        model: new PagedList('api/af-revenue/sql/aode_LifeYearByChargeType', 35, {YeadDate: 'this.model.YeadDate'},{
          f_count: 0,
          f_pregas: 0,
          f_collection: 0
        }),
        initres: {
          org:[this.$login.f.orgid]
        },
        orgname: '',
        criteriaShow: true,
        orgCondtionStr:'',
        defaultfield:["f_operate_date","f_count","f_pregas","f_collection"],
        sumName:[
        'f_count','f_pregas','f_collection']
      }
    },
    ready(){
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      selfSearch (args) {
        let f_orgstr = this.orgCondtionStr
        if(!this.orgCondtionStr){
          f_orgstr = 'and f_orgid = ' + this.$login.f.orgid
        }
        args.condition = `${args.condition} and f_operator = '悦生活' ` + f_orgstr
        this.model.search(args.condition, args.model)
      },
      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 = false
      },
      getorg (obj) {
        this.orgname = obj.res
        this.orgCondtionStr = `and f_orgid in ${this.$login.convertToIn(obj.resids)}`
      },
    },
    watch:{
      'all' (val) {
        if (val) {
          this.modelval = this.bodyData
        } else {
          this.modelval = defaultPrint.config
          this.put()
        }
      },
      'modelval.length' () {
        this.put()
      },
    },
    computed:{
      getCondition(){
        return {condition: `${this.$refs.paged.$refs.cri.condition}`+this.orgCondtionStr+  ` and f_operator = '悦生活'`,YeadDate:`${this.$refs.paged.$refs.cri.model.YeadDate}`}
      },
      getfield(){
        return exportConfig.lifeYearByChargeConfig
      },
      userstates () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户状态')]
      },
      sliceArea () {
        let rs = new Array()
        for (let i = 0; i < this.$login.f.f_allArea.length; i++) {
          let temp = {
            label: this.$login.f.f_allArea[i].label,
            value: this.$login.f.f_allArea[i].label
          }
          rs.push(temp)
        }
        return [{label: '全部', value: ''}, ...rs]
      }
    }
  }
</script>
