<template>
  <div class="auto">
    <partial-view v-ref:pv>
      <owe-list :row='list' @refresh-center="refreshCenter"></owe-list>
    </partial-view>
    <div v-if="chargeView">
      <p v-if="row.f_totalsplit_type === '总表'"><strong>分表承担总量 (单位: 方): {{splitAllgas}}</strong></p>
      <machine-meter-center :data='model' :row='row' v-ref:mainche></machine-meter-center>
    </div>
  </div>
</template>
<script>
  import OweList from './OweList'

  let loadGen = async function (self) {
    let res = await self.$refs.pv.load('rs/sql/getOverdue', {data: {f_user_id: self.row.f_user_id}})
    if (res.data) {
      for (let i = 0; i < res.data.length; i++) {
        res.data[i].f_totalsplit_type = self.row.f_totalsplit_type
        res.data[i].f_user_id = res.data[i].f_user_id + ''
        let getSplit = await self.$resetpost('rs/logic/splitInfo', {data: {totalInfo: res.data[i]}}, {resolveMsg: null, rejectMsg: '获取分表抄表信息出错!!!'})
        if (getSplit.data.length > 0) {
          for (let j = 0; j < getSplit.data.length; j++) {
            self.splitAllgas = self.splitAllgas + (getSplit.data[j].f_oughtamount - 0)
          }
        }
      }
      self.list.metertitle = self.row.f_user_id
      self.list.rows = self.model.rows = [...self.model.rows, ...res.data]
      // 控制欠费列表承担量的显示
      self.model.f_totalsplit_type = self.row.f_totalsplit_type
    }
    self.chargeView = true
  }

  /**
   *售气收费
   *机表收费组合组件，包括抄表列表以及机表收费主组件
   */
  export default {
    title: '收费',
    components: {OweList},
    data () {
      return {
        model: {
          rows: []
        },
        list: {
          metertitle: '',
          rows: []
        },
        splitAllgas: 0,
        chargeView: false
      }
    },
    props: ['row'],
    ready() {
      loadGen(this)
    },
    methods: {
      refreshCenter (val) {
        this.initData(this.row)
        this.getRows(val)
      },
      initData (row) {
        let temp = {
          f_payment: ['现金缴费'],
          f_user_id: row.f_user_id,
          f_userinfo_id: row.f_userinfo_id,
          f_balance: (row.f_balance - 0) > 0 ? row.f_balance : 0,
          f_pregas: 0,
          f_preamount: 0,
          f_totalcost: 0,
          f_curbalance: 0,
          f_collection: '',
          f_overdue: 0,
          f_lowest: 0,
          f_print: ['普通收据']
        }
        this.model = Object.assign({}, this.model, temp)
      },
      getRows (val) {
        for (let i = 0; i < val.length; i++) {
          this.model.f_pregas = this.model.f_pregas + val[i].f_oughtamount
          this.model.f_preamount = this.model.f_preamount + val[i].f_oughtfee
          this.model.f_overdue = this.model.f_overdue + val[i].overdue
        }
        this.model.f_overdue = this.model.f_overdue.toFixed(2)
        this.model.f_totalcost = (this.model.f_preamount - 0) + (this.model.f_overdue - 0)
        this.model.f_collection = this.model.f_totalcost
      },
      // 获取欠费里面有滞纳金的最大的一笔欠费的角标
      getMaxIndex(val) {
        if (val.findIndex((item) => {return item.f_oughtfee < 0}) !== -1) {
          return val.findIndex((item) => {return item.f_oughtfee < 0})
        } else {
          let firstOverdue = val.findIndex((item) => {return item.overdue > 0})
          if (firstOverdue === -1) {
            return 0
          } else {
            let lastOverdue = val.slice(firstOverdue).findIndex((item) => {return item.overdue === 0}) - 1
            return lastOverdue < 0 ? val.length - 1 : firstOverdue + lastOverdue
          }
        }
      }
    },
    watch: {
      chargeView () {
        this.$refs.mainche && this.$refs.mainche.checkOverdue()
      }
    }
  }
</script>
<style lang="less">
  #tooltip div {
    height: auto;
  }
</style>
