/* eslint-disable */
export const calcute = {
  // 商贷或公积金贷款统一函数 type: 还款方式 1等额本息 2等额本金 num: 贷款金额 year: 贷款年限 lilv: 贷款基准利率
  singleDk (type, num, year, lilv) {
    if (type === 1) {
      return this.benxi(type, num, year, lilv)
    } else if (type === 2) {
      return this.benjin(type, num, year, lilv)
    } else if (type === 3) {
      return this.xianxi(type, num, year, lilv)
    }
  },
  // 组合贷款计算 type：还款方式 sdnum：商贷金额 gjjnum：公积金贷款金额, sdyear：商贷年限 gjjyear：公积金贷款年限 sdlilv：商贷利率 gjjlilv：公积金贷款利率
  zuhe (type, sdnum, gjjnum, sdyear, gjjyear, sdlilv, gjjlilv) {
    var _this = this,
      year = sdyear > gjjyear ? sdyear : gjjyear
    if (type === 1) {
      var sdObj = _this.benxi(type, sdnum, sdyear, sdlilv)
      var gjjObj = _this.benxi(type, gjjnum, gjjyear, gjjlilv)
      if (sdObj.mouthdataArray.length > gjjObj.mouthdataArray.length) {
        var mergemouthdataArray = sdObj.mouthdataArray.map(function (item, index) {
          if (index < gjjObj.mouthdataArray.length) {
            return {
              monthName: item.monthName,
              yuelixi: item.yuelixi + gjjObj.mouthdataArray[index].yuelixi,
              yuebenjin: item.yuebenjin + gjjObj.mouthdataArray[index].yuebenjin,
              leftFund: item.leftFund + gjjObj.mouthdataArray[index].leftFund
            }
          } else {
            return {
              monthName: item.monthName,
              yuelixi: item.yuelixi,
              yuebenjin: item.yuebenjin,
              leftFund: item.leftFund
            }
          }

        })
      } else {
        var mergemouthdataArray = gjjObj.mouthdataArray.map(function (item, index) {
          if (index < sdObj.mouthdataArray.length) {
            return {
              monthName: item.monthName,
              yuelixi: item.yuelixi + sdObj.mouthdataArray[index].yuelixi,
              yuebenjin: item.yuebenjin + sdObj.mouthdataArray[index].yuebenjin,
              leftFund: item.leftFund + sdObj.mouthdataArray[index].leftFund
            }
          } else {
            return {
              monthName: item.monthName,
              yuelixi: item.yuelixi,
              yuebenjin: item.yuebenjin,
              leftFund: item.leftFund
            }
          }
        })
      }
      return {
        yuegong: sdObj.yuegong + gjjObj.yuegong,
        totalLixi: sdObj.totalLixi + gjjObj.totalLixi,
        totalPrice: sdObj.totalPrice + gjjObj.totalPrice,
        mouthdataArray: mergemouthdataArray,
        totalDknum: parseFloat(sdObj.totalDknum) + parseFloat(gjjObj.totalDknum),
        year: year
      }
    } else if (type === 2) {
      var sdObj = _this.benjin(type, sdnum, sdyear, sdlilv)
      var gjjObj = _this.benjin(type, gjjnum, gjjyear, gjjlilv)
      if (sdObj.mouthdataArray.length > gjjObj.mouthdataArray.length) {
        var mergemouthdataArray = sdObj.mouthdataArray.map(function (item, index) {
          if (index < gjjObj.mouthdataArray.length) {
            return {
              monthName: item.monthName,
              yuelixi: item.yuelixi + gjjObj.mouthdataArray[index].yuelixi,
              yuebenjin: item.yuebenjin + gjjObj.mouthdataArray[index].yuebenjin,
              leftFund: item.leftFund + gjjObj.mouthdataArray[index].leftFund
            }
          } else {
            return {
              monthName: item.monthName,
              yuelixi: item.yuelixi,
              yuebenjin: item.yuebenjin,
              leftFund: item.leftFund
            }
          }

        })
      } else {
        var mergemouthdataArray = gjjObj.mouthdataArray.map(function (item, index) {
          if (index < sdObj.mouthdataArray.length) {
            return {
              monthName: item.monthName,
              yuelixi: item.yuelixi + sdObj.mouthdataArray[index].yuelixi,
              yuebenjin: item.yuebenjin + sdObj.mouthdataArray[index].yuebenjin,
              leftFund: item.leftFund + sdObj.mouthdataArray[index].leftFund
            }
          } else {
            return {
              monthName: item.monthName,
              yuelixi: item.yuelixi,
              yuebenjin: item.yuebenjin,
              leftFund: item.leftFund
            }
          }
        })
      }
      return {
        yuegong: sdObj.yuegong + gjjObj.yuegong,
        totalLixi: sdObj.totalLixi + gjjObj.totalLixi,
        totalPrice: sdObj.totalPrice + gjjObj.totalPrice,
        yuegongdijian: sdObj.yuegongdijian + gjjObj.yuegongdijian,
        totalDknum: parseFloat(sdObj.totalDknum) + parseFloat(gjjObj.totalDknum),
        year: year,
        mouthdataArray: mergemouthdataArray
      }
    } else if (type === 3) {
      var sdObj = _this.xianxi(type, sdnum, sdyear, sdlilv)
      var gjjObj = _this.xianxi(type, gjjnum, gjjyear, gjjlilv)
      return {
        yuegong: sdObj.yuegong + gjjObj.yuegong,
        totalLixi: sdObj.totalLixi + gjjObj.totalLixi
      }
    }
  },
  // 等额本息计算
  benxi (type, num, year, lilv) {
    //每月月供额=〔贷款本金×月利率×(1＋月利率)＾还款月数〕÷〔(1＋月利率)＾还款月数-1〕
    var mouth = parseInt(year) * 12,
      mouthlilv = parseFloat(lilv) / 12,
      dknum = parseFloat(num) * 10000
    //每月月供
    var yuegong = (dknum * mouthlilv * Math.pow((1 + mouthlilv), mouth)) / (Math.pow((1 + mouthlilv), mouth) - 1)
    //总利息=还款月数×每月月供额-贷款本金
    var totalLixi = mouth * yuegong - dknum
    //还款总额 总利息+贷款本金
    var totalPrice = totalLixi + dknum,
      leftFund = totalLixi + dknum

    //循环月份
    var mouthdataArray = [],
      nowmouth = new Date().getMonth(),
      realmonth = 0

    for (var i = 1; i <= mouth; i++) {
      realmonth = nowmouth + i
      var yearlist = Math.floor(i / 12)

      realmonth = realmonth - 12 * yearlist

      if (realmonth > 12) {
        realmonth = realmonth - 12
      }
      //console.log(realmonth)
      //每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
      var yuelixi = dknum * mouthlilv * (Math.pow((1 + mouthlilv), mouth) - Math.pow((1 + mouthlilv), i - 1)) / (Math.pow((1 + mouthlilv), mouth) - 1)
      //每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕
      var yuebenjin = dknum * mouthlilv * Math.pow((1 + mouthlilv), i - 1) / (Math.pow((1 + mouthlilv), mouth) - 1)
      leftFund = leftFund - (yuelixi + yuebenjin)
      if (leftFund < 0) {
        leftFund = 0
      }
      mouthdataArray[i - 1] = {
        monthName: realmonth + '月',
        yuelixi: yuelixi,
        yuebenjin: yuebenjin,
        //剩余还款
        leftFund: leftFund
      }
    }
    return {
      yuegong: yuegong,
      totalLixi: totalLixi,
      totalPrice: totalPrice,
      mouthdataArray: mouthdataArray,
      totalDknum: num,
      year: year
    }
  },
  // 等额本金计算
  benjin (type, num, year, lilv) {
    var mouth = parseInt(year) * 12,
      mouthlilv = parseFloat(lilv) / 12,
      dknum = parseFloat(num) * 10000,
      yhbenjin = 0 //首月还款已还本金金额是0
    //每月应还本金=贷款本金÷还款月数
    var everymonthyh = dknum / mouth
    //每月月供额=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率
    var yuegong = everymonthyh + (dknum - yhbenjin) * mouthlilv
    //每月月供递减额=每月应还本金×月利率=贷款本金÷还款月数×月利率
    var yuegongdijian = everymonthyh * mouthlilv
    //总利息=〔(总贷款额÷还款月数+总贷款额×月利率)+总贷款额÷还款月数×(1+月利率)〕÷2×还款月数-总贷款额
    var totalLixi = ((everymonthyh + dknum * mouthlilv) + dknum / mouth * (1 + mouthlilv)) / 2 * mouth - dknum
    //还款总额 总利息+贷款本金
    var totalPrice = totalLixi + dknum,
      leftFund = totalLixi + dknum

    //循环月份
    var mouthdataArray = [],
      nowmouth = new Date().getMonth(),
      realmonth = 0

    for (var i = 1; i <= mouth; i++) {
      realmonth = nowmouth + i
      var yearlist = Math.floor(i / 12)

      realmonth = realmonth - 12 * yearlist

      if (realmonth > 12) {
        realmonth = realmonth - 12
      }
      yhbenjin = everymonthyh * (i - 1)
      var yuebenjin = everymonthyh + (dknum - yhbenjin) * mouthlilv
      //每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率
      var yuelixi = (dknum - yhbenjin) * mouthlilv
      leftFund = leftFund - yuebenjin
      if (leftFund < 0) {
        leftFund = 0
      }
      mouthdataArray[i - 1] = {
        monthName: realmonth + '月',
        yuelixi: yuelixi,
        //每月本金
        yuebenjin: everymonthyh,
        //剩余还款
        leftFund: leftFund
      }
    }
    return {
      yuegong: yuegong,
      totalLixi: totalLixi,
      totalPrice: totalPrice,
      yuegongdijian: yuegongdijian,
      mouthdataArray: mouthdataArray,
      totalDknum: num,
      year: year
    }
  },
  // 先息后本
  xianxi (type, num, year, lilv) {
    // 月利率 = 年利率/12
    var mouth = parseInt(year) * 12
    var mouthlilv = parseFloat(lilv) / 12
    var dknum = parseFloat(num) * 10000

    // 月供=月利率x本金
    var yuegong = mouthlilv * dknum
    // 总利息=〔(总贷款额÷+总贷款额×月利率)+总贷款额÷还款月数×(1+月利率)〕÷2×还款月数-总贷款额
    // 总利息= 还款月数 * 月利率 * 总贷款额
    var totalLixi = mouth * mouthlilv * dknum
    return {
      yuegong: yuegong,
      totalLixi: totalLixi,
      totalDknum: num,
      year: year
    }
  }
}
