<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">
            <res-select-group :initres="$parent.$parent.initres" @re-res="$parent.$parent.getRes" :cascade =true  :mul="false" v-ref:sel></res-select-group>
            <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 class="col-sm-2">
              <label class="font_normal_body">包含APP</label>
              <v-select :value.sync="model.f_hasapp" v-model="model.f_hasapp"
                        :options='$parent.$parent.hasapps' placeholder='请选择'
                        condition="f_operator {} "
                        close-on-select></v-select>
            </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_YearByChargeType" template-name='年报（缴费类型）'  :choose-col="true"></export-excel>
              <year-by-charge-print :countmodel="{sumsmodel:$parent.$parent.sumsmodel,model:$parent.$parent.model,orgname:$parent.$parent.orgname,depname:$parent.$parent.depname}"></year-by-charge-print>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid  :model="model" :sumsmodel="sumsmodel" 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>
            <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_operate_date}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_count_open}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_pregas_open}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_collection_open}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_count_zg}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_pregas_zg}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_collection_zg}}</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_open}}</nobr></td>
          <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_pregas_open}}</nobr></td>
          <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_collection_open}}</nobr></td>
          <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_count_zg}}</nobr></td>
          <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_pregas_zg}}</nobr></td>
          <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_collection_zg}}</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 defaultPrint from '../config/DefaultPrint'
  import exportConfig from '../../config/exportConfig'
  let readySomething = async function (self) {
    let year = new Date().getFullYear()
    self.$refs.paged.$refs.cri.model.YeadDate = year+""
    self.$refs.paged.$refs.cri.model.f_hasapp = [`!= '悦生活'`]
    // await self.$refs.paged.$refs.cri.search()
    // self.sumsmodel = self.$refs.paged.$refs.grid.model.sums
  }

  export default {
    title: '年报（缴费类型）',
    data() {
      return {
        model: new PagedList('api/af-revenue/sql/aode_YearByChargeType', 35, {YeadDate: 'this.model.YeadDate'},{
          f_count_open:0,
          f_pregas_open:0,
          f_collection_open:0,
          f_count_zg:0,
          f_pregas_zg:0,
          f_collection_zg:0,
          f_count: 0,
          f_pregas: 0,
          f_collection: 0
        }),
        initres: {
          org:[this.$login.f.orgid],
          dep:[this.$login.f.depids],
          user:[this.$login.f.id]
        },
        sumsmodel: {},
        orgname: '',
        depname: '',
        criteriaShow: true,
        hasapps: [{label: '包含APP', value: ''}, {label: '不包含APP', value: "!= '悦生活'"}],
        orgCondtionStr:'',
        defaultfield:["f_operate_date","f_count_open","f_pregas_open","f_collection_open","f_count_zg","f_pregas_zg","f_collection_zg","f_count","f_pregas","f_collection"],
        sumName:[
        'f_count_open','f_pregas_open','f_collection_open',
        'f_count_zg', 'f_pregas_zg','f_collection_zg',
        'f_count','f_pregas','f_collection']
      }
    },
    ready(){
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      async selfSearch (args) {
        let f_orgstr = this.orgCondtionStr
        if(!this.orgCondtionStr){
          f_orgstr = 'and f_orgid = ' + this.$login.f.orgid
        }
        args.condition = `${args.condition} ` + f_orgstr
        args.condition = args.condition.replace(/f_zones/, "f_slice_area")
        await this.model.search(args.condition, args.model)
        this.sumsmodel = this.$refs.paged.$refs.grid.model.sums
        console.log('计算合计。', this.sumsmodel)
        if (this.$refs.paged.$refs.grid.model.rows.length === 0) {
          for (let key in this.sumsmodel) {
            this.sumsmodel[key] = 0
          }
        }
        this.$dispatch('getdata', this.sumsmodel)
      },
      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
      },
      getRes(condition,obj) {
        this.orgCondtionStr = condition
        this.orgname = obj.orgnames[0]
        this.depname = obj.depnames[0]
      },
    },
    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,YeadDate:`${this.$refs.paged.$refs.cri.model.YeadDate}`}
      },
      getfield(){
        return exportConfig.yearByChargeConfig
      },
      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>
