function All() {
}

All.prototype = {
  timer: '',
  debounce(fn, delay = 500) {
    var _this = this
    return function (arg) {
      //获取函数的作用域和变量
      let that = this
      let args = arg
      clearTimeout(_this.timer) // 清除定时器
      _this.timer = setTimeout(function () {
        fn.call(that, args)
      }, delay)
    }
  },
  setCookie(val) { //cookie设置[{key:value}]、获取key、清除['key1','key2']
    for (var i = 0, len = val.length; i < len; i++) {
      for (var key in val[i]) {
        document.cookie = key + '=' + encodeURIComponent(val[i][key]) + '; path=/'
      }
    }
  },
  getCookie(name) {
    var strCookie = document.cookie
    var arrCookie = strCookie.split('; ')
    for (var i = 0, len = arrCookie.length; i < len; i++) {
      var arr = arrCookie[i].split('=')
      if (name == arr[0]) {
        return decodeURIComponent(arr[1])
      }
    }
  },
  clearCookie(name) {
    var myDate = new Date()
    myDate.setTime(-1000) //设置时间
    for (var i = 0, len = name.length; i < len; i++) {
      document.cookie = '' + name[i] + "=''; path=/; expires=" + myDate.toGMTString()
    }
  },
  arrToStr(arr) {
    if (arr) {
      return arr.map(item => {
        return item.name
      }).toString()
    }
  },
  toggleClass(arr, elem, key = 'id') {
    return arr.some(item => {
      return item[key] == elem[key]
    })
  },
  toChecked(arr, elem, key = 'id') {
    var isIncludes = this.toggleClass(arr, elem, key)
    !isIncludes ? arr.push(elem) : this.removeEle(arr, elem, key)
  },
  removeEle(arr, elem, key = 'id') {
    var includesIndex
    arr.map((item, index) => {
      if (item[key] == elem[key]) {
        includesIndex = index
      }
    })
    arr.splice(includesIndex, 1)
  },
  replaceJsonKey(obj, old_key, new_key) {
    // debugger
    let str = JSON.stringify(obj)
    let reg = new RegExp(old_key, 'g')
    let newStr = str.replace(reg, new_key)
    return JSON.parse(newStr)
  },
  setApproverStr(nodeConfig) {
    // debugger
    if (nodeConfig.nodeObject && nodeConfig.nodeObject.name) {
      return nodeConfig.nodeObject.name
    }
    if (nodeConfig.nodeObjectList.length == 1) {
      return nodeConfig.nodeObjectList[0].name
    } else {
      if (nodeConfig.examineMode == 1) {
        return this.arrToStr(nodeConfig.nodeObjectList)
      } else if (nodeConfig.examineMode == 2) {
        return nodeConfig.nodeObjectList.length + '人会签'
      }
    }
  },
  dealStr(str, obj) {
    let arr = []
    let list = str.split(',')
    for (var elem in obj) {
      list.map(item => {
        if (item == elem) {
          arr.push(obj[elem].value)
        }
      })
    }
    return arr.join('或')
  },
  operatorStr(operator){
    if(!operator){
      return ""
    }
    if(operator=="||"){
      return "或";
    }
    if(operator=="&&"){
      return "且";
    }
    if(operator=="in"){
      return "属于";
    }
    if(operator=="notIn"){
      return "不属于";
    }
    return operator
  },
  handlePrecent(it){
    if(it.leftFieldsName.indexOf('比例')>-1||it.leftFieldsName.indexOf('比率')>-1){
      return '%'
    }
    return ''
  },
  conditionStr(nodeConfig, index) {
    let conditionString = ''
    let conditionStringName = ''
    let {conditionList, nodeObjectList} = nodeConfig.conditionNodes[index]
    // debugger
    if (conditionList.length == 0) {
      return nodeConfig.conditionNodes[index].default ? '其他条件进入此流程' : '请设置条件'
    } else {
      if (conditionList.length > 0) {

        conditionList.forEach((item, indx) => {
          if (conditionList.length > 1) {
            conditionString = conditionString + '('
            conditionStringName = conditionStringName + '('
          }
          if (item.conditionChildrenNodes && item.conditionChildrenNodes.length > 0
            && item.conditionChildrenNodes[0].leftFields
            && item.conditionChildrenNodes[0].centerFields
            && (item.conditionChildrenNodes[0].rightFields || item.conditionChildrenNodes[0].rightFields===0)) {
            item.conditionChildrenNodes.forEach((it, ind) => {
              conditionString = conditionString + (it.conditionOperator===undefined||ind==0?'':this.operatorStr(it.conditionOperator)) + it.leftFields + this.operatorStr(it.centerFields) + it.rightFields
              conditionStringName = conditionStringName + (it.conditionOperator===undefined||ind==0?'':this.operatorStr(it.conditionOperator)) + it.leftFieldsName + this.operatorStr(it.centerFields) + it.rightFieldsName+this.handlePrecent(it)
            })
          }
          if (conditionList.length > 1 ) {
            conditionString = conditionString + ')'
            conditionStringName = conditionStringName + ')'
          }
          if(conditionList.length-1>indx){
            conditionString = conditionString + this.operatorStr(item.conditionGroupOperator)
            conditionStringName = conditionStringName + this.operatorStr(item.conditionGroupOperator)
          }
        })
      }
      // console.log('conditionString', conditionString)
      // console.log('conditionStringName', conditionStringName)
      return conditionStringName || '请设置条件'
    }
  },
  //   conditionStr(item, index) {
  //     console.log('conditionStr',JSON.stringify(item))
  //     // debugger
  //   let {
  //     conditionList, //条件组
  //     conditionString, //条件数据
  //     conditionStringName, //条件显示
  //   } = item
  //     console.log('conditionList',JSON.stringify(conditionList))
  //   let arr = [true] //判断条件是否有值
  //   if (conditionList.length === 0 || conditionString === '') {
  //     if (item.nodeName === '默认') {
  //       return '其他情况'
  //     }
  //     return '请设置条件'
  //   }
  //   if (conditionList.length !== 0) {
  //
  //     console.log('conditionList有值',conditionList)
  //     if (conditionList.length === 1) {
  //       //当条件组为一个
  //       conditionList[0].conditionChildrenNodes &&
  //       conditionList[0].conditionChildrenNodes.forEach(
  //         (item, index) => {
  //           if (item.leftFields == '' || item.centerFields == '' || item.rightFields == '') {
  //             arr.push(false)
  //           }
  //           if (index !== 0 && item.conditionOperator == '') {
  //             arr.push(false)
  //           }
  //         }
  //       )
  //     } else {
  //       //当条件组为多个（判断是否有运算符）
  //       conditionList.forEach((item, index) => {
  //         if (index != conditionList.length - 1) {
  //           //条件组不为最后一个.校验是否有条件运算符
  //           if (item.conditionGroupOperator == '')
  //             arr.push(false)
  //         }
  //         if (
  //           item.conditionChildrenNodes &&
  //           item.conditionChildrenNodes.length > 0
  //         ) {
  //           item.conditionChildrenNodes.forEach((it, ind) => {
  //             if (
  //               it.leftFields === '' ||
  //               it.centerFields === '' ||
  //               it.rightFields == ''
  //             )
  //               arr.push(false)
  //             if (ind !== 0 && it.conditionOperator == '')
  //               arr.push(false)
  //           })
  //         } else if (
  //           item.conditionChildrenNodes &&
  //           item.conditionChildrenNodes.length == 0
  //         ) {
  //           arr.push(false)
  //         }
  //       })
  //     }
  //     if (arr.includes(false)) {
  //       return '请设置条件'
  //     }
  //   }
  //   return conditionStringName
  // },
  copyerStr(nodeConfig) {
    // if (nodeConfig.nodeObjectList.length != 0) {
    return this.arrToStr(nodeConfig.nodeObjectList)
    // } else {
    //     if (nodeConfig.ccSelfSelectFlag == 1) {
    //         return '发起人自选'
    //     }
    // }
  },
  toggleStrClass(item, key) {
    let a = item.zdy1 ? item.zdy1.split(',') : []
    return a.some(item => {
      return item == key
    })
  },
}

export default new All()
