<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 form-group">
                  <label class="font_normal_body">时间类型</label>
                  <v-select
                    :value.sync="$parent.$parent.typeInfo" v-model="$parent.$parent.typeInfo"
                    :options='$parent.$parent.DateType' placeholder='选择月份'
                    close-on-select
                    @change="$parent.$parent.ThePolesReverse()"
                  >
                  </v-select>
                </div>
                <div class="col-sm-2 form-group">
                  <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="col-sm-2 form-group" v-if="$parent.$parent.oneShow">
                  <label for="YeanMouthDate" class="font_normal_body" >统计月份</label>
                  <datepicker placeholder="统计月份" style="width:60%"
                              v-model="model.YeanMouthDate"
                              :value.sync="model.YeanMouthDate"
                              :format="'yyyy-MM'"
                              :show-reset-button="true"
                              :select-month="true">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group" v-if="!$parent.$parent.oneShow">
                  <label for="endDate" class="font_normal_body">开始时间</label>
                  <datepicker placeholder="开始时间" style="width:60%"
                              v-model="model.f_begin_operate_date"
                              :value.sync="model.f_begin_operate_date"
                              :format="'yyyy-MM-dd'"
                              :show-reset-button="true"

                  >
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group" v-if="!$parent.$parent.oneShow">
                  <label for="endDate" class="font_normal_body">结束时间</label>
                  <datepicker placeholder="结束时间" style="width:60%"
                              v-model="model.f_end_operate_date"
                              :value.sync="model.f_end_operate_date"
                              :format="'yyyy-MM-dd'"
                              :show-reset-button="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_monthByUserType" template-name='月报（客户类型）'
                                :choose-col="true"></export-excel>

                  <!--<print-data :model="$parent.model" :field="$parent.$parent.getfield"-->
                              <!--:defaultfield="$parent.$parent.defaultfield" print-name="月报（客户类型）"></print-data>-->
                  <month-by-user-print :countmodel="{sumsmodel:$parent.$parent.sumsmodel,model:$parent.$parent.model,orgname:$parent.$parent.orgname,depname:$parent.$parent.depname}"></month-by-user-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>
                <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>
</template>

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

  let readySomething = async function (self) {
    //console.log("拿到的默认时间是",self.$login.toStandardYearMonth())
    self.$refs.paged.$refs.cri.model.YeanMouthDate = self.$login.toStandardYearMonth()
    self.$refs.paged.$refs.cri.model.f_hasapp = [`!= '悦生活'`]
    // await self.$refs.paged.$refs.cri.search()
  }

  export default {
    title: '月报（客户类型）',
    data() {
      return {

        model: new PagedList('api/af-revenue/sql/aode_monthByUserType', 35,{YeanMouthDate: 'this.model.YeanMouthDate',f_begin_operate_date: 'this.model.f_begin_operate_date',f_end_operate_date: 'this.model.f_end_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
        }),

        oneShow: true,
        typeInfo: '选择月份',
        DateType:[{label:'选择月份',value:'选择月份'},{label:'选择区间',value:'选择区间'}],
        hasapps: [{label: '包含APP', value: ''}, {label: '不包含APP', value: "!= '悦生活'"}],
        criteriaShow: true,
        orgCondtionStr: '',
        initres: {
          org:[this.$login.f.orgid],
          dep:[this.$login.f.depids],
          user:[this.$login.f.id]
        },
        sumsmodel: {},
        orgname: '',
        depname: '',
        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
        }
        //由于此处底层组件默认前面加了个and,所以此处前面硬补位一个1=1 等后面加condition了可以替换
        this.condition = `${args.condition} ` + f_orgstr
        this.condition = this.condition.replace(/f_zones/, "f_slice_area")
        await this.model.search(this.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)
      },

      ThePolesReverse(){
        if(this.typeInfo[0] === '选择月份'){
          this.$refs.paged.$refs.cri.model.f_begin_operate_date =''
          this.$refs.paged.$refs.cri.model.f_end_operate_date =''
          this.$refs.paged.$refs.cri.model.YeanMouthDate = this.$login.toStandardYearMonth()
          this.oneShow =true
        }else{
          this.$refs.paged.$refs.cri.model.YeanMouthDate =''
          this.$refs.paged.$refs.cri.model.f_begin_operate_date = this.$login.toStandardDateString()
          this.$refs.paged.$refs.cri.model.f_end_operate_date = this.$login.toStandardDateString()
          this.oneShow =false
        }
      },


      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,YeanMouthDate:`${this.$refs.paged.$refs.cri.model.YeanMouthDate}`
          + this.orgCondtionStr,f_begin_operate_date:`${this.$refs.paged.$refs.cri.model.f_begin_operate_date}` + this.orgCondtionStr,f_end_operate_date:`${this.$refs.paged.$refs.cri.model.f_end_operate_date}`}
      },
      getfield() {
        return exportConfig.monthByUserConfig
      },
      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>
