<template>
  <div class="flex" style="overflow-y: auto!important;">
    <div  class="auto" style="height: 50%; overflow: auto">
      <div  v-for="owe of list" >
        <owe-list :row='owe' :floor="config.floor" :charge="config.charge" @refresh-center="refreshCenter"></owe-list>
      </div>
    </div>
    <div class="span" v-if="chargeView">
      <p v-if="row.f_totalsplit_type === '总表'"><strong>分表承担总量 (单位: 方): {{splitAllgas}}</strong></p>
      <machine-meter-center :data='model' :owelist="list[0].rows" :charge="config.charge" :row='row' :floor="config.floor" :ceil="config.ceil" v-ref:mainche></machine-meter-center>
    </div>
    <div class="span" v-if="!chargeView">
      <work-busy-small :is-busy="!chargeView"></work-busy-small>
    </div>
  </div>
  <upload :blodid="blodid" v-if="config.showupload" isremark="true" fusetype="机表收费"></upload>
</template>
<script>
  import OweList from './OweList'
  import WorkBusySmall from './WorkBusySmall'
  import {HttpResetClass} from 'vue-client'

  let loadGen = async function (self) {
    await self.$getConfig(self, 'MachineCharge')
    let http = new HttpResetClass()
    let res = await http.load('POST', 'rs/logic/sale_getOwe', {data: {f_userinfo_id: self.row.f_userinfo_id}}, {resolveMsg: null, rejectMsg: '获取欠费出错！！', newly: true})
    self.list = res.data

    let owes = []
    for (let i = 0; i < res.data.length; i++) {
      for (let j = 0; j < res.data[i].rows.length; j++) {
        owes.push(res.data[i].rows[j])
      }
    }
    self.refreshCenter(owes)
    // 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, WorkBusySmall},
    data () {
      return {
        config: {
          showupload: false, // 控制附件显示
          floor: false, // 向下取整
          ceil: false, // 向上取整
          charge: true  // 是否需要缴清欠费
        },
        blodid: '',
        oweArr: [],
        model: {},
        list: [],
        splitAllgas: 0,
        chargeView: false
      }
    },
    props: ['row'],
    ready () {
      this.blodid = this.row.f_userinfo_id
      loadGen(this)
    },
    methods: {
      refreshCenter (val, rows, f_userfiles_id) {
        console.log(f_userfiles_id)
        if (f_userfiles_id) {
          console.log(f_userfiles_id)
          this.oweArr[f_userfiles_id] = val
        }
        this.initData(this.row)
        let arr = Object.values(this.oweArr).reduce((acc, val) => acc.concat(val), [])
        this.getRows(arr)
      },
      initData (row) {
        let temp = {
          f_payment: ['现金缴费'],
          f_user_id: row.f_user_id,
          f_userinfo_id: row.f_userinfo_id,
          f_userfiles_id: row.f_userfiles_id,
          f_balance: row.f_balance,
          otherprice: '',
          f_pregas: 0,
          f_preamount: 0,
          f_totalcost: 0,
          f_curbalance: 0,
          f_collection: '',
          f_garbage_fee: 0,
          f_overdue: 0,
          f_lowest: 0,
          f_print: ['普通收据'],
          f_privilege_id: 0,
          f_privilege_money: 0,
          f_privilege_gas: 0,
          f_after_discount: 0
        }
        this.model = Object.assign({}, this.model, temp)
      },
      getRows (val) {
        this.model.selectedids = []
        for (let i = 0; i < val.length; i++) {
          if (val[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_garbage_fee = this.model.f_garbage_fee + val[i].f_garbage_fee
            this.model.selectedids.push({id: val[i].handplan_id})
          }
        }
        if (val.length == 0) {
          this.model.f_overdue = 0
          this.model.f_totalcost = 0
          this.model.f_collection = 0
          this.model.f_pregas = 0
          this.model.f_preamount = 0
          this.model.f_garbage_fee = 0
        }
        this.model.f_overdue = this.model.f_overdue.toFixed(2)
        this.model.f_totalcost = (this.model.f_preamount - 0) + (this.model.f_overdue - 0)
        /* if (this.model.f_totalcost === 0) {
          this.model.f_totalcost = this.row.f_balance < 0 ? (0 - this.row.f_balance) : 0
        } */
        this.model.f_totalcost += this.model.f_garbage_fee
        if (this.config.floor) {
          this.model.f_collection = Math.floor((this.model.f_totalcost - 0) - (this.model.f_balance - 0))
        } else if (this.config.ceil) {
          this.model.f_collection = Math.ceil((this.model.f_totalcost - 0) - (this.model.f_balance - 0))
        } else {
          this.model.f_collection = (this.model.f_totalcost - 0) - (this.model.f_balance - 0)
        }
        this.model.f_collection = this.model.f_collection < 0 ? 0 : this.model.f_collection
      },
      // 获取欠费里面有违约金的最大的一笔欠费的角标
      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>
