<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" :mul="false" v-ref:sel></res-select-group>-->
                <div class="col-sm-2 form-group">
                  <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 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" v-show="$parent.$parent.oneShow">
                  <label for="endDate" class="font_normal_body">统计月份</label>
                  <datepicker placeholder="统计月份" style="width:60%"
                              v-model="model.f_operate_date"
                              :value.sync="model.f_operate_date"
                              :format="'yyyy-MM'"
                              :show-reset-button="true"
                              :select-month="true"
                  >
                  </datepicker>
                </div>
                <div class="col-sm-2" v-show="!$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" v-show="!$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>
              <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" class="button_export button_spacing"
                                :field="$parent.$parent.getfield"  :sumname="$parent.$parent.sumName"
                                sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="aode_LifemonthByChargeType" template-name='悦生活月报表'
                                :choose-col="true"></export-excel>

                  <!--<print-data :model="$parent.model" :field="$parent.$parent.getfield"-->
                  <!--:defaultfield="$parent.$parent.defaultfield" print-name="悦生活月报"></print-data>-->

                  <life-month-print :countmodel="{sumsmodel:$parent.$parent.model.sums,model:$parent.$parent.model,orgname:$parent.$parent.orgname}"></life-month-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 defaultPrint from '../config/DefaultPrint'
  import exportConfig from '../../../config/exportConfig'
  import LifeMonthPrint from "../widget/LifeMonthPrint";

  let readySomething = async function (self) {
    self.$refs.paged.$refs.cri.model.f_operate_date = self.$login.toStandardYearMonth()
    // await self.$refs.paged.$refs.cri.search()
  }

  export default {
    components: {LifeMonthPrint},
    title: '悦生活月报',
    data() {
      return {
        model: new PagedList('api/af-revenue/sql/aode_LifemonthByChargeType', 35, {f_operate_date: 'this.model.f_operate_date',f_begin_operate_date: 'this.model.f_begin_operate_date',f_end_operate_date: 'this.model.f_end_operate_date'}, {
          f_count: 0,
          f_pregas: 0,
          f_collection: 0
        }),
        oneShow: true,
        typeInfo: '选择月份',

        DateType:[{label:'选择月份',value:'选择月份'},{label:'选择区间',value:'选择区间'}],
        initres: {
          org:[this.$login.f.orgid]
        },
        criteriaShow: true,
        orgname: '',
        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} ` + f_orgstr+  ` and f_operator = '悦生活'`
        this.model.search(args.condition, args.model)
      },
      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.f_operate_date = this.$login.toStandardYearMonth()
          this.oneShow =true
        }else{
          this.$refs.paged.$refs.cri.model.f_operate_date =''
          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
      },
      getorg (obj) {
        this.orgname = obj.res
        this.orgCondtionStr = `and f_orgid in ${this.$login.convertToIn(obj.resids)}`
      },
      stamp() {
        this.all = false
        //默认选择要打印的列
        this.modelval = defaultPrint.config
        this.fields = this.getfield
        console.log('所有打印字段', this.fields)
        this.printshow = true
        this.put()
      },
      put() {
        // 对Modelval进行排序
        this.sortModelval()
        this.thead = `<tr><th colspan=${this.modelval.length}>客户查询统计报表</th></tr><tr>`
        for (let key of this.modelval) {
          this.thead += '<th>' + this.fields[key] + '</th>'
        }
        this.thead += '</tr>'
      },
      print() {
        this.$refs.print.PrintAsFile()
        this.printshow = false
      },
      close() {
        this.printshow = false
      },
      // 对选择的列进行排序
      sortModelval() {
        let sortModel = []
        Object.keys(this.fields).forEach((key) => {
          if (this.modelval.includes(key)) {
            sortModel.push(key)
          }
        })
        this.modelval = sortModel
        console.log('选择的打印的字段', this.modelval)
      }
    },
    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 = '悦生活'`,f_operate_date:`${this.$refs.paged.$refs.cri.model.f_operate_date}`
           ,f_begin_operate_date:`${this.$refs.paged.$refs.cri.model.f_begin_operate_date}`,f_end_operate_date:`${this.$refs.paged.$refs.cri.model.f_end_operate_date}`}

      //  return {condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr+  ` and f_operator = '悦生活'`,f_operate_date:`${this.$refs.paged.$refs.cri.model.f_operate_date}`
      //    + 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.lifeMonthByChargeConfig
      },
      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>

<style>
  .datapanel {
    color: #333;
    background-color: white;
    box-shadow: darkgrey 0.5px 0.5px 0.5px 0.5px;
    padding: 10px 10px 5px 10px;
    border-radius: 10px;
  }
</style>
