import {MessageBox} from 'element-ui'
import store from '../store'
import tools from 'dvcp-ui/lib/js/utils'

const addChildParty = (parent, pending) => {
  let doBeforeCount = pending.length
  parent["children"] = parent["children"] || []
  pending.map((e, index, arr) => {
    if (e.partyOrgParentId == parent.partyOrgId) {
      parent.children.push(e)
      arr.splice(index, 1)
      addChildParty(parent, arr)
    }
  })
  if (parent.children.length == 0) {
    delete parent.children
  }
  if (pending.length > 0 && doBeforeCount > pending.length) {
    parent.children.map(c => addChildParty(c, pending))
  }
}
/**
 * 封装提示框
 */


const $confirm = (content, options) => {
  return MessageBox.confirm(content, {
    type: "warning",
    confirmButtonText: "确认",
    center: true,
    title: "提示",
    dangerouslyUseHTMLString: true,
    ...options
  })
}

/**
 * 封装权限判断方法
 */


const $permissions = flag => {
  const buttons = store.state.user.info.buttons
  if (buttons) return buttons.some(b => b.id == flag || b.permission == flag)
  else return false
}

const $decimalCalc = (...arr) => {
  //确认提升精度
  let decimalLengthes = arr.map(e => {
    let index = ("" + e).indexOf(".")
    return ("" + e).length - index
  })
  let maxDecimal = Math.max(...decimalLengthes), precision = Math.pow(10, maxDecimal)
  //计算
  let intArr = arr.map(e => (Number(e) || 0) * precision)
  //返回计算值
  return intArr.reduce((t, a) => t + a) / precision
}
/**
 * @param { function } func

 * @param { number } wait 延迟执行毫秒数

 * @param { boolean } immediate  true 表立即执行，false 表非立即执行
 */



export default {
  ...tools,
  addChildParty,
  $confirm,
  $permissions,
  $decimalCalc
}


