import Vue from 'vue'

let LdapService = {
  install(Vue, options) {
    // 给vue增添对话框显示方法
    Vue.LdapService = Vue.prototype.$LdapService = LdapService
  },

  async getHasNodeByType (type,parent) {
    let getTree = await Vue.resetpost('/rs/search', {data: {
        source: `tool.getFullTree(this.getRights().where(row.getType() == $${type}$))`, userid: Vue.$login.f.id
      }}, {resolveMsg: null, rejectMsg: '获取当前登录人所具有的权限的人员出错！！'})
    // 获取其中所有的user id
    let temp = []
    let ids = LdapService.orgData(temp, getTree.data[0], type)
    //
    let orgoptions = [] ;
    orgoptions = LdapService.getorgname(orgoptions,getTree.data[0],'organization')
    let depoptions = [] ;
    depoptions = LdapService.getorgname(depoptions,getTree.data[0],'department',parent || '')
    let condition = LdapService.convertToOrIn(ids,'id')
    let returnData = {}
    returnData.orgoptions = orgoptions;
    returnData.depoptions = depoptions;
    if (condition) {
      let conditions = ` and ${condition}`
      returnData.condition = conditions
      return returnData
    } else {
      let conditions = ' and 1!=1'
      returnData.condition = conditions
      return returnData
    }

  },

  async getOrgByType (type,orgid) {
    let getTree = await Vue.resetpost('/rs/search', {data: {
        source: `tool.getFullTree(this.getAllChildrens().where(row.getType() == $${type}$))`,
        userid: orgid
      }}, {resolveMsg: null, rejectMsg: '获取当前登录人所具有的权限的人员出错！！'})
    // 获取其中所有的user id
    let temp = []
    let ids = LdapService.orgData(temp, getTree.data[0], type)
    let condition = LdapService.convertToOrIn(ids,'id')
    if (condition) {
      return ` and ${condition}`
    } else {
      return ' and 1!=1'
    }
  },
  convertToOrIn(arrays, field) {
    let temp = field  + " in ('')"
    let isMax = false
    if (arrays instanceof Array) {
      if (arrays.length >= 1) {
        temp = field + ' in ('
        for (let i = 0; i < arrays.length; i++) {
          const value = arrays[i]
          if ( i!== 0 && i % 800 === 0 && arrays.length - 1 > i) {
            isMax = true
            temp = temp.substr(0, temp.length - 1) + ')'
            temp += ' or ' + field + ' in ('
          }
          temp += `'${value}',`
        }
        temp = temp.substr(0, temp.length - 1) + ')'
      }
    }
    if (isMax) {
      return "(" + temp + ")";
    } else {
      return temp
    }
  },
  orgData (temp, tree, type) {
    if (tree.resourcetype === `${type}`) {
      temp.push(tree.id)
    }
    if (tree.children.length > 0) {
      tree.children.forEach((res) => {
        LdapService.orgData(temp, res, type)
      })
    }
    return temp
  },
  getorgname(temp,tree,type,parent=''){
    if (tree.resourcetype === `${type}` && tree.name != '组织机构') {
      if (parent && parent !== ''){
        if (type==='department') {
          if (tree.fullname.indexOf(parent) !== -1){
            temp.push({label:tree.name,value:tree.name})
          }
        }else {
          temp.push({label:tree.name,value:tree.name})
        }
      }else {
        temp.push({label:tree.name,value:tree.name})
      }
    }
    if(tree.children.length>0){
      tree.children.forEach((res) => {
        LdapService.getorgname(temp, res, type,parent)
      })
    }
    return temp;
  }

}

export default LdapService
