<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 class="col-sm-2 ">
                  <label class="font_normal_body" for="startDate">开始年份:</label>
                  <datepicker id="startDate" placeholder="开始日期" style="width: 60%"
                              v-model="model.f_begin_operate_date"
                              :value.sync="model.f_begin_operate_date"
                              :format="'yyyy'"
                              :show-reset-button="true"
                              :select-year="true">
                  </datepicker>
                </div>
                <div class="col-sm-2 " >
                  <label  class="font_normal_body" for="endDate">结束年份:</label>
                  <datepicker id="endDate" placeholder="结束日期" style="width: 60%"
                              v-model="model.f_end_operate_date"
                              :value.sync="model.f_end_operate_date"
                              :format="'yyyy'"
                              :show-reset-button="true"
                              :select-year="true">
                  </datepicker>
                </div>
                <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_YearByUserType" template-name='年报（客户类型）'  :choose-col="true"></export-excel>
                  <year-by-user-print :countmodel="{sumsmodel:$parent.$parent.sumsmodel,model:$parent.$parent.model,orgname:$parent.$parent.orgname,depname:$parent.$parent.depname}"></year-by-user-print>


                  <!--<export-excel :data="$parent.$parent.getCondition"-->
                  <!--:field="$parent.$parent.getExportField"-->
                  <!--sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="filesQuery" template-name='用户查询导出' :choose-col="true"></export-excel>-->
                </div>
              </div>
            </div>
          </criteria>
          <data-grid  :model="model" partial='list' :sumsmodel="sumsmodel" 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>
                <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_open_my}}</nobr></td>
              <td style="text-align: center;"><nobr>{{row.f_count_my}}</nobr></td>
              <td style="text-align: center;"><nobr>{{row.f_pregas_my}}</nobr></td>
              <td style="text-align: center;"><nobr>{{row.f_collection_my}}</nobr></td>

              <td style="text-align: center;"><nobr>{{row.f_open_sy}}</nobr></td>
              <td style="text-align: center;"><nobr>{{row.f_count_sy}}</nobr></td>
              <td style="text-align: center;"><nobr>{{row.f_pregas_sy}}</nobr></td>
              <td style="text-align: center;"><nobr>{{row.f_collection_sy}}</nobr></td>

              <td style="text-align: center;"><nobr>{{row.f_open_gy}}</nobr></td>
              <td style="text-align: center;"><nobr>{{row.f_count_gy}}</nobr></td>
              <td style="text-align: center;"><nobr>{{row.f_pregas_gy}}</nobr></td>
              <td style="text-align: center;"><nobr>{{row.f_collection_gy}}</nobr></td>

              <td style="text-align: center;"><nobr>{{row.f_open}}</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_open_my}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_count_my}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_pregas_my}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_collection_my}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_open_sy}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_count_sy}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_pregas_sy}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_collection_sy}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_open_gy}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_count_gy}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_pregas_gy}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_collection_gy}}</nobr></td>
              <td style="text-align: center;background-color: mistyrose"><nobr>{{model.sums.f_open}}</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.f_hasapp = [`!= '悦生活'`]
    self.$refs.paged.$refs.cri.model.YeadDate = year+""
    self.$refs.paged.$refs.cri.model.f_end_operate_date = year+""
    self.$refs.paged.$refs.cri.model.f_begin_operate_date = year+""
    // 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_YearByUserType', 35,{YeadDate: 'this.model.YeadDate',f_end_operate_date:'this.model.f_end_operate_date',f_begin_operate_date:'this.model.f_begin_operate_date'},{
          f_open_my:0,
          f_count_my:0,
          f_pregas_my:0,
          f_collection_my:0,
          f_open_sy:0,
          f_count_sy:0,
          f_pregas_sy:0,
          f_collection_sy:0,
          f_open_gy:0,
          f_count_gy:0,
          f_pregas_gy:0,
          f_collection_gy:0,
          f_open: 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]
        },
        criteriaShow: true,
        orgCondtionStr:'',
        sumsmodel: {},
        orgname: '',
        depname: '',
        hasapps: [{label: '包含APP', value: ''}, {label: '不包含APP', value: "!= '悦生活'"}],
        defaultfield:[],
        sumName:['f_open_my','f_count_my','f_pregas_my','f_collection_my',
          'f_open_sy','f_count_sy','f_pregas_sy',
          'f_collection_sy','f_open_gy','f_count_gy','f_pregas_gy',
          ' f_collection_gy','f_open','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.yearByUserConfig
      },
      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>
