<template>
  <div   v-if="!listpage"  @keyup.enter="search">
    <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 :style="$parent.$parent.style" :initres="$parent.$parent.initres" @re-res="$parent.$parent.getRes" :mul="false" :style="$parent.$parent.style" v-ref:sel></res-select-group>
            <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'"
                           class="datepicker"
                           :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'"
                           class="datepicker"
                           :show-reset-button="true"
                           class="select"
              >
              </datepicker>
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">用气性质</label>
              <v-select :value.sync="model.f_gasproperties" v-model="model.f_gasproperties"
                        :options='$parent.$parent.gasproperties' placeholder='请选择'
                        condition="f_gasproperties = '{}'"
                        close-on-select></v-select>
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">操作类型</label>
              <v-select :value.sync="model.f_type" v-model="model.f_type"
                        :options='$parent.$parent.operates' placeholder='请选择'
                        close-on-select></v-select>
            </div>
            <div :class="$parent.$parent.style">
              <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="button-range" 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"
                            sqlurl="rs/logic/exportfile" sql-name="aode_dayReport" template-name='日清单'  :choose-col="true"
                            class="button_export button_spacing"></export-excel>
              <day-report-list-print :countmodel="{sumsmodel:$parent.$parent.sumsmodel,model:$parent.$parent.model,orgname:$parent.$parent.orgname,depname:$parent.$parent.depname}"></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>

        </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>
              <!--<nobr>客户姓名</nobr>-->
            </th>
            <th><nobr>地址</nobr></th>
            <th>
              <data-order field="f_meter_brand" name="气表厂家"></data-order>
            </th>
            <th><data-order field="f_operate_date" name="操作时间"></data-order>
              <!--<nobr>操作时间</nobr>-->
            </th>
            <th>
              <data-order field="f_stair1price" name="1单价"></data-order>
            </th>
            <th>
              <data-order field="f_stair1amount" name="1气量"></data-order>
            </th>
            <th>
              <data-order field="f_stair2price" name="2单价"></data-order>
            </th>
            <th>
              <data-order field="f_stair2amount" name="2气量"></data-order>
            </th>
            <th>
              <data-order field="f_stair3price" name="3单价"></data-order>
            </th>
            <th>
              <data-order field="f_stair3amount" name="3气量"></data-order>
            </th>
            <th>
              <data-order field="f_pregas" name="气量"></data-order>
            </th>
            <th>
              <data-order field="f_preamount" name="应收金额"></data-order>
            </th>
            <th>
              <data-order field="f_collection" name="实收金额"></data-order>

            </th>
            <th><nobr>上期余额</nobr></th>
            <th><nobr>本期余额</nobr></th>
            <th>
              <data-order field="f_payment" name="付款方式"></data-order>
              <!--<nobr>付款方式</nobr>-->
            </th>
            <th>
              <data-order field="f_type" name="收费类型"></data-order>
              <!--<nobr>收费类型</nobr>-->
            </th>
            <th>
              <data-order field="f_operator" name="操作员"></data-order>
            </th>
            <th>
              <data-order field="f_user_type" name="客户类型"></data-order>
              <!--<nobr>客户类型</nobr>-->
            </th>
            <th><nobr>备注</nobr></th>

          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center;"><nobr>
            <span @click="$parent.$parent.$parent.showmsg(row)"><a>{{row.f_userinfo_code}}</a></span>
          </nobr> </td>
          <td style="text-align: left;"><nobr>{{row.f_user_name}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_address}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_meter_brand}}</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_pregas}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_preamount}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_collection}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_balance}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_curbalance}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_payment}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_type}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_operator}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_user_type}}</nobr></td>
          <td style="text-align: left;"><nobr>{{row.f_comments}}</nobr></td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
  <div v-if="listpage">
    <user-info-detail-manage :f_userinfo_id="rowdata.f_userinfo_id" @cancel-main="cancel"></user-info-detail-manage>
  </div>
</template>

<script>

  import { PagedList } from 'vue-client'
  import exportConfig from '../../config/exportConfig'
  import DayReportSummary from "./DayReportSummary";
  import getLodop from '../../../../plugins/LodopFuncs'

  export default {
    components: {DayReportSummary},
    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:[this.$login.f.depids],
          user:[this.$login.f.id]
        },
        model: new PagedList('rs/sql/aode_dayReport', 30, {orderitem: "'f_operate_date'"},{
          f_pregas: 0,
          f_collection: 0,
          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_bk: 0,
          f_pregas_bk: 0,
          f_collection_bk: 0,
          f_count_bq: 0,
          f_pregas_bq: 0,
          f_collection_bq: 0,
          f_count_hb: 0,
          f_pregas_hb: 0,
          f_collection_hb: 0,
          f_count_cz: 0,
          f_pregas_cz: 0,
          f_collection_cz: 0,
          f_sum_count:0,
          f_shouru_gas:0,
          f_shouru_fee:0
        }),
        listpage:false,
        rowdata: {},
        orgCondtionStr: '',
        userTypes: [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')],
        count: 0,
        sumsmodel: {},
        printshow: false,
        all: false,
        orgname: '',
        depname: '',
        criteriaShow: false,
        operates: [{label: '全部', value: ''}, {label: '发卡售气', value: "发卡售气"},{label: '卡表收费', value: "卡表收费"},{label: '补气', value: "补气"},{label: '补卡', value: "补卡"},{label: '换表', value: "换表"},{label: '冲正', value: "冲正"}],
        hasapps: [{label: '包含APP', value: ''}, {label: '不包含APP', value: "!= '悦生活'"}],
      }
    },
    ready() {
      this.$refs.paged.$refs.cri.condition += this.orgCondtionStr
      this.$refs.paged.$refs.cri.model.f_hasapp = [`!= '悦生活'`]
      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: {
      showmsg(obj){
        this.criteriaShow = false
        this.$emit('close')
        this.rowdata=obj
        this.listpage=true
      },
      cancel() {
        this.listpage = false
      },
      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

        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'

        this.model.params.operatorState = this.$refs.paged.$refs.cri.model.f_type + ''

        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)
      },
      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
      },
      getfield() {
        return exportConfig.dataReportConfig
      },
      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,
            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]
          }


        // return {condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr}
        //   condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr,
        //   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] !=null?this.model.paramSource.orderitem.split("'")[1].split("'")[0]:this.orderDefault

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

</script>

<style scoped>

  .col {
    width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

</style>
