<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="true" :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">区&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;县</label>-->
<!--              <input type="text" style="width:60%" class="input_search" v-model="model.f_zones"-->
<!--                     condition="f_zones like '%{}%'" placeholder='区县'>-->
<!--            </div>-->
<!--            <div :class="$parent.$parent.style">-->
<!--              <label class="font_normal_body">小&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;区</label>-->
<!--              <input type="text" style="width:60%" class="input_search" v-model="model.f_residential_area"-->
<!--                     condition="f_residential_area like '%{}%'" placeholder='小区'>-->
<!--            </div>-->

            <div :class="$parent.$parent.style">
              <label class="font_normal_body">区&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;县</label>
              <v-select
                :value.sync="model.f_zones" :value-single="true"
                v-model="model.f_zones"
                @change="$parent.$parent.communityChange"
                :options='$parent.$parent.sliceArea' placeholder='请选择'
                condition1="f_zones = '{}'"
                close-on-select search="true"></v-select>
            </div>
            <div  :class="$parent.$parent.style">
              <label class="font_normal_body">小&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;区</label>
              <v-select :value.sync="model.f_residential_area"
                        v-model="model.f_residential_area"
                        :value-single="true"
                        :options='$parent.$parent.areaslist' placeholder='请选择'
                        condition1="ss.f_residential_area = '{}'"
                        close-on-select search="true"></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"
                        condition="f_type in {}"
                        :options='$parent.$parent.operates' placeholder='请选择'
                        close-on-select></v-select>
            </div>
            <div :class="$parent.$parent.style">
              <label class="font_normal_body">收费状态</label>
              <v-select :value.sync="model.f_state" v-model="model.f_state" multiple
                        :options='$parent.$parent.sellstate' placeholder='请选择'
                        condition="f_state in {}"
                        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="$parent.$parent.style">
              <label class="font_normal_body">付款方式</label>
              <v-select id="f_payment"
                        v-model="model.f_payment"
                        placeholder='请选择'
                        condition="f_payment ='{}'"
                        :value.sync="model.f_payment"
                        :options='$parent.$parent.payments'
                        close-on-select clear-button>
              </v-select>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">客户类型</label>
              <v-select :value.sync="model.f_user_type"
                        v-model="model.f_user_type" condition="f_user_type = '{}'"
                        @change="$parent.$parent.userTypeChange()"
                        :options='$parent.$parent.userTypes' placeholder='请选择'
                        close-on-select></v-select>
            </div>
            <div class="col-sm-2 form-group">
              <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="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"
                            :defaultselect="$parent.$parent.defaultexport"
                            sqlurl="api/af-revenue/logic/openapi/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><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><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>
            <th>
              <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: left;"><nobr>{{row.f_residential_area}}</nobr></td>
          <td style="text-align: left;"><nobr>{{row.f_zones}}</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_state}}</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: center;"><nobr>{{row.f_gasproperties}}</nobr></td>
          <td style="text-align: center;"><nobr>{{row.f_type}}</nobr></td>
          <td style="text-align: left;"><nobr>{{row.f_comments}}</nobr></td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>

  <table class="table-hover">
    <tr style="position: relative" class="table-bordered">
      <td
        style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;color: #5CB95C;font-weight: bold">
        汇总信息
      </td>
      <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
        购买量:&emsp;{{sumsmodel.f_pregas}}方
      </td>
      <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
        实收:&emsp;{{sumsmodel.f_collection}}元
      </td>
      <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
        应收:&emsp;{{sumsmodel.f_preamount}}元
      </td>
      <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
        一阶气量合计:&emsp;{{sumsmodel.f_stair1amount}}方
      </td>
      <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
        二阶气量合计:&emsp;{{sumsmodel.f_stair2amount}}方
      </td>
      <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px;font-weight: bold">
        三阶气量合计:&emsp;{{sumsmodel.f_stair3amount}}方
      </td>
    </tr>
  </table>
  <div v-if="listpage">
    <user-info-detail-manage-new :f_userinfo_id="rowdata.f_userinfo_id" @cancel-main="cancel"></user-info-detail-manage-new>
  </div>
</template>

<script>

import {HttpResetClass, PagedList} from 'vue-client'
  import exportConfig from './config/exportConfig'
  import DayReportSummary from "../../components/sale/report/aode/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'",
        gasproperties:[],
        areaslist: [],
        f_filialeids: '',
        //片区
        sliceArea: [],
        slice_area:[],
        orderFields: {
          f_userinfo_code: 'no'
        },
        initres: {
          org:[this.$login.f.orgid],
          dep:[this.$login.f.depids],
          user:[this.$login.f.id]
        },
        model: new PagedList('api/af-revenue/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_preamount: 0,
          f_stair1amount: 0,
          f_stair2amount: 0,
          f_stair3amount: 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: '',
        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.initdata()
      // this.search()
    },
    methods: {
      async initdata(){
        this.f_filialeids = this.orgCondtionStr.match(/'(\d+)'/)
        this.initAreas(this.f_filialeids[1])
        // this.initareas(` f_orgid = '${this.$login.f.orgid}'`)
      },
      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 + ''

        console.log("========")
        console.log(this.$refs.paged.$refs.cri.model.f_residential_area)
        if(this.$refs.paged.$refs.cri.model.f_residential_area != '' && this.$refs.paged.$refs.cri.model.f_zones != undefined && this.$refs.paged.$refs.cri.model.f_residential_area != undefined &&  this.$refs.paged.$refs.cri.model.f_zones != ''){
          this.$refs.paged.model.params.condition1 = `ss.f_residential_area = '${this.$refs.paged.$refs.cri.model.f_residential_area}' and f_zones = '${this.$refs.paged.$refs.cri.model.f_zones}'`
        }else if (this.$refs.paged.$refs.cri.model.f_residential_area != ''){
          this.$refs.paged.model.params.condition1 = `ss.f_residential_area = '${this.$refs.paged.$refs.cri.model.f_residential_area}'`
        }else if (this.$refs.paged.$refs.cri.model.f_zones != '') {
          this.$refs.paged.model.params.condition1 = `f_zones = '${this.$refs.paged.$refs.cri.model.f_zones}'`
        }else {
          this.$refs.paged.model.params.condition1 = `1=1`
        }

        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()
      },
      userTypeChange () {
        this.gasproperties=[]
        if(this.$refs.paged.$refs.cri.model !==null) {
          this.$refs.paged.$refs.cri.model.f_gasproperties=''
          this.gasproperties = this.$appdata.getParam(this.$refs.paged.$refs.cri.model.f_user_type[0])
        }
        else{
          this.gasproperties =[{label: '全部', value: ''}]
        }
      },
      close() {
        this.printshow = false
      },
      getfield() {
        return exportConfig.dataReportConfig
      },
      defaultexport() {
        return exportConfig.defaultDataReportConfig
      },
      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()
      },
      // 初始化片区
      async initAreas (val) {
        if (val) {
          let http = new HttpResetClass()
          let getAllArea = await http.load('POST', '/rs/search', {
            source: 'this.getParentByType($organization$).getAllChildrens().where(row.getType() == $zone$)',
            userid: this.$login.f.id
          }, {resolveMsg: null, rejectMsg: '获取片区出错！！！'})
          let arr = getAllArea.data.filter((res) => {
            if(Array.isArray(val)){
              for (let i = 0; i <val.length ; i++) {
                if(res.parentid == val[i]){
                  return true
                }
              }
            }
            return res.parentid == val
          })
          this.sliceArea = []
          this.slice_area = []
          arr.forEach((res) => {
            this.sliceArea.push({label: res.name, value: res.name})
          })
          console.log("===============")
          console.log(this.sliceArea)
        }
      },
      //初始化小区添加小区
      async initareas(pconditon){
        console.log(123)
        // if(this.usertype){
        //   pconditon=pconditon+` and  f_special='1' `
        // }
        this.areaslist = []
        let HttpReset = new HttpResetClass()
        let req = await HttpReset.load('POST', 'api/af-revenue/sql/querySingleTable?pageNo=1&pageSize=9999999', {
          data: {
            items: '*',
            tablename: 't_area',
            condition: pconditon
          }
        }, {resolveMsg: null, rejectMsg: '获取地址失败!'})
        let redata = []
        req.data.forEach((row) => {
          redata.push({
            label: row.f_residential_area,
            value: row.f_residential_area,
            data: row,
            id: row.id
          })
        })
        this.areaslist=redata
        console.log('看看modal',this.model)
        console.log('小区', redata)
      },
      async  communityChange(val){
        console.log("val")
        console.log(val)
        if(val){
          await this.initareas(` f_orgid = '${this.f_filialeids[1]}' and f_slice_area ='${val}' `)
          if(this.model.f_residential_area_id){
            // if (this.findbyid(this.areaslist,this.model.f_residential_area_id)) {
            //   let pcd_id=this.findbyid(this.areaslist,this.model.f_residential_area_id).f_community_id
            //   if(pcd_id!=val){
            //     this.model.f_residential_area_id=''
            //   }
            // } else {
            //   this.model.f_residential_area_id=''
            // }

          }
          // if(this.model.f_pcd_id.length==0){
          // if (this.findbyid(this.streetslist,this.model.f_street_id)) {
          //   this.model.f_pcd_id=this.findbyid(this.streetslist,this.model.f_street_id).f_pcd_id
          // }
          // }
        }
      },
    },
    watch: {
      // 监听 `this.$login.f.orgid` 的变化
      'orgname'() {
        // 检查是否发生了变化
          // 重新初始化数据
          this.initdata();
      }
    },
    computed: {
      getCondition() {
          return {
            condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr,
            condition1: this.$refs.paged.model.params.condition1,
            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]
          }
      },
      getfield() {
        return exportConfig.dataReportConfig
      },
      defaultexport() {
        return exportConfig.defaultDataReportConfig
      },
      payments() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('付款方式')]
      },
      userTypes() {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')]
      },
      sellstate() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('收费状态')]
      }
    }
  }

</script>

<style scoped>

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

</style>
