<template>
  <div class="flex" >
    <criteria-paged :model="model" v-ref:paged @sort="sort">
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <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" :style="$parent.$parent.style" v-ref:sel></res-select-group>
            <div class="span" style = "float:right;margin: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"
                            sqlurl="api/af-revenue/logic/openapi/exportfile" sql-name="aode_lifedayReport" template-name='悦生活日报表'  :choose-col="true"
                            class="button_export button_spacing"></export-excel>
              <life-day-report-list-print :countmodel="{sumsmodel:$parent.$parent.sumsmodel,model:$parent.$parent.model,orgname:$parent.$parent.orgname,depname:$parent.$parent.depname}"></life-day-report-list-print>
              <div style="float: right"  class="button_spacing" :class="{'button_shrink_left':$parent.$parent.criteriaShow,'button_shrink_right':!$parent.$parent.criteriaShow}"    @click="$parent.$parent.show()"></div>
              <div style="float: right"  class="button_spacing"
                   :class="{'button_shrink_top':$parent.$parent.criteriaShowTwo,'button_shrink_bottom':!$parent.$parent.criteriaShowTwo}"
                   @click="$parent.$parent.hidden()"></div>
            </div>
          </div>
          <div class="row" v-show="$parent.$parent.criteriaShowTwo">
            <div :class="$parent.$parent.style" >
              <label for="startDate" class="font_normal_body">开始日期</label>
              <datepicker  placeholder="开始日期" style="width:60%"
                           v-model="model.f_startDate"
                           :value.sync="model.f_startDate"
                           :format="'yyyy-MM-dd'"
                           :show-reset-button="true"
                           class="select"
              >
              </datepicker>
            </div>
            <div :class="$parent.$parent.style" >
              <label for="endDate" class="font_normal_body">结束日期</label>
              <datepicker  placeholder="结束日期" style="width:60%"
                           v-model="model.f_endDate"
                           :value.sync="model.f_endDate"
                           :format="'yyyy-MM-dd'"
                           :show-reset-button="true"
                           class="select">
              </datepicker>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid  :model="model" :sumsmodel="sumsmodel" partial='list' class="list_area table_sy" v-ref:grid >
        <template partial='head'>
          <tr>
            <th>
              <data-order field="f_userinfo_code" name="客户编号"></data-order>
              <!--<nobr>客户编号</nobr>-->
            </th>
            <th><data-order field="f_user_name" name="客户姓名"></data-order></th>
            <th><nobr>气量</nobr></th>
            <th><nobr>金额</nobr></th>
            <th><data-order field="f_operate_date" name="写卡时间"></data-order>
              <!--<nobr>操作时间</nobr>-->
            </th>
            <th><nobr>1单价</nobr></th>
            <th><nobr>1气量</nobr></th>
            <th><nobr>2单价</nobr></th>
            <th><nobr>2气量</nobr></th>
            <th><nobr>3单价</nobr></th>
            <th><nobr>3气量</nobr></th>
            <th>
              <data-order field="f_payment" name="付款方式"></data-order>
              <!--<nobr>付款方式</nobr>-->
            </th>
            <th><nobr>交易号</nobr></th>
            <th><nobr>写卡服务机</nobr></th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center;"><nobr>{{row.f_userinfo_code}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_user_name}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_pregas}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_collection}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_operate_date}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_stair1price}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_stair1amount}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_stair2price}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_stair2amount}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_stair3price}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_stair3amount}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_payment}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_pay_id}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_terminal_num}}</nobr></td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>

  import { PagedList } from 'vue-client'
  import exportConfig from '../../../config/exportConfig'

  export default {
    title: '悦生活日报表',
    props: {
      style: {
        type: String,
        default: 'col-sm-2 form-group'
      },
    },
    data() {
      return {
        orderDefault: "'f_operate_date'",
        orderFields: {
          f_userinfo_code: 'no'
        },
        initres: {
          org:[this.$login.f.orgid],
          dep:[],
          user:[],
        },
        model: new PagedList('api/af-revenue/sql/aode_lifedayReport', 30, {orderitem: "'f_operate_date'"},{
          f_sum_count:0,
          f_shouru_gas:0,
          f_shouru_fee:0
        }),
        orgCondtionStr: '',
        criteriaShowTwo: false,
        userTypes: [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')],
        count: 0,
        sumsmodel: {},
        printshow: false,
        all: false,
        orgname: '',
        depname: '',
        criteriaShow: false,
      }
    },
    ready() {
      this.$refs.paged.$refs.cri.condition += this.orgCondtionStr
      this.$refs.paged.$refs.cri.model.f_startDate = this.$login.toStandardDateString()
      this.$refs.paged.$refs.cri.model.f_endDate = this.$login.toStandardDateString()
      // this.search()
    },
    methods: {
      hidden(){
        this.criteriaShowTwo = !this.criteriaShowTwo
      },
      search() {
        this.$refs.paged.$refs.cri.search()
      },
      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 +  ` and f_operator = '悦生活'`

        this.model.params.startDate = this.$refs.paged.$refs.cri.model.f_startDate + ' 00:00:00'
        this.model.params.endDate = this.$refs.paged.$refs.cri.model.f_endDate + ' 23:59:59'

        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) {
            console.log(key)
            this.sumsmodel[key] = 0
          }
        }
        this.$dispatch('getdata', this.sumsmodel)
      },
      getRes(condition, obj) {
        this.orgCondtionStr = condition
        this.orgname = obj.orgnames[0]
        this.depname = obj.depnames[0]
      },
      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] = []
        })
        this.$refs.paged.$refs.cri.model.f_startDate = this.$login.toStandardDateString()
        this.$refs.paged.$refs.cri.model.f_endDate = this.$login.toStandardDateString()
      },
      close() {
        this.printshow = false
      },
      show() {
        this.criteriaShow = !this.criteriaShow
        if (this.criteriaShow) {
          this.$emit('show')
        } else {
          this.$emit('close')
        }
      },
      /*sort(field, rule) {
        console.log('sort...')

      },*/
      sort (field, rule) {
        // 将所有排序方式设为不排序，实现相互排斥
        for (let key in this.orderFields) {
          if (key === field) {
            this.orderFields[key] = rule
          } else {
            this.orderFields[key] = 'no'
          }
        }
        // 如果新规则不排序，还原为默认排序
        if (rule === 'no') {
          this.model.paramSource.orderitem = `'${this.orderDefault}'`
        } else {
          this.model.paramSource.orderitem = `'${field} ${rule}'`
        }

        this.search()
      },
    },
    computed: {
      getCondition() {
          return {
            condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr + ` and f_operator = '悦生活'`,
            startDate: this.$refs.paged.$refs.cri.model.f_startDate + ' 00:00:00',
            endDate: this.$refs.paged.$refs.cri.model.f_endDate + ' 23:59:59',
            orderitem: this.model.paramSource.orderitem.split("'")[1].split("'")[0]
          }

      },
      gasproperties() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用气性质')]
      },
      getfield() {
        return exportConfig.lifeDataReportConfig
      }
    }
  }

</script>
