<template>
  <div class="flex">
    <charge-user-list @selected="goCharge" @research="research" v-ref:list></charge-user-list>
    <div class="span form-horizontal" v-if="chargeView">
      <div class="col-sm-6">
        <div class="auto" v-for="singleMeter in allMeters">
          <hand-plan-list :row='singleMeter'></hand-plan-list>
        </div>
        <!--<charge-user-meters></charge-user-meters>-->
      </div>
      <div class="col-sm-6">
        <charge-user-center :data="model" @research="research"></charge-user-center>
      </div>
    </div>
  </div>
</template>

<script>
  import ChargeUserCenter from './ChargeUserCenter'
import ChargeUserList from './ChargeUserlist'
import co from 'co'

  let getAllMetersOwe = function * (self, row) {
    // 获取这户下面的所有表编号
    let getAllUserfiles = yield self.$resetpost('rs/sql/singleTable_OrderBy', {data: {items: 'f_user_id', tablename: 't_userfiles', condition: `f_userinfo_id = '${row.f_userinfo_id}'`, orderitem: 'f_user_id'}}, {resolveMsg: null, rejectMsg: `按户收费: 获取户编号为${row}下的表编号出错`})
    console.log(`获取${row.f_userinfo_id}下的所有表编号`, getAllUserfiles.data)
    // 循环获取每个表编号对应的欠费信息
    // let res = yield self.$refs.pv.load('rs/sql/getOverdue', {data: {f_user_id: id}})
    for (let i = 0; i < getAllUserfiles.data.length; i++) {
      let res = yield self.$resetpost('rs/sql/getOverdue', {data: {f_user_id: getAllUserfiles.data[i].f_user_id}}, {resolveMsg: null, rejectMsg: `按户收费: 获取表编号为${getAllUserfiles.data[i].f_user_id}下的欠费信息出错`})
      console.log(`获取到表编号${getAllUserfiles.data[i].f_user_id}下的欠费`, res.data)
      let result = {
        metertitle: '',
        rows: []
      }
      result.metertitle = getAllUserfiles.data[i].f_user_id
      result.rows = [...result.rows, ...res.data]
      self.allMeters.push(result)
    }
    // 计算收费界面
    self.initModel(row)
    self.calChargeView()
    self.chargeView = true
  }

  export default {
    components: {ChargeUserList, ChargeUserCenter},
    title: '按户收费',
    data () {
      return {
        chargeView: false,
        allMeters: [],
        model: {
          f_userinfo_id: '',
          f_payment: ['现金缴费'],
          f_print: ['专用收据'],
          f_balance: '',
          f_pregas: 0,
          f_preamount: 0,
          f_overdue: 0,
          f_totalcost: 0,
          rows: []
        }
      }
    },
    methods: {
      goCharge (row) {
        console.log('选中的用户数据。。。', row)
        this.allMeters = []
        co(getAllMetersOwe(this, row))
      },
      calChargeView () {
        this.model.rows = []
        for (let i = 0; i < this.allMeters.length; i++) {
          for (let j = 0; j < this.allMeters[i].rows.length; j++) {
            this.model.f_pregas += this.allMeters[i].rows[j].f_oughtamount
            this.model.f_preamount += this.allMeters[i].rows[j].f_oughtfee
            this.model.f_overdue += this.allMeters[i].rows[j].overdue
            this.model.rows.push(this.allMeters[i].rows[j])
          }
        }
        this.model.f_totalcost = (this.model.f_preamount - 0) + (this.model.f_overdue - 0) - (this.model.f_balance - 0) > 0 ? (this.model.f_preamount - 0) + (this.model.f_overdue - 0) - (this.model.f_balance - 0) : 0
        this.model.f_collection = Math.trunc(this.model.f_totalcost)
        console.log('计算收款。。', this.model.f_collection)
      },
      initModel (row) {
        this.model = {
          f_userinfo_id: row.f_userinfo_id,
          f_payment: ['现金缴费'],
          f_print: ['专用收据'],
          f_balance: row.f_balance,
          f_pregas: 0,
          f_preamount: 0,
          f_overdue: 0,
          f_totalcost: 0
        }
      },
      research () {
        console.log('接受事件')
        this.chargeView = false
        this.allMeters = []
        this.$refs.list.$refs.paged.$refs.criteria.search()
      }
    },
    events: {
      'success' () {
        this.research()
      }
    }
  }
</script>

