import Vue from 'vue'

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

  async getHasNodeByType (type) {
    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')
    let condition = Vue.$login.convertToIn(ids)
    let returnData = {}
    returnData.orgoptions=orgoptions;
    returnData.depoptions = depoptions;
    if (condition) {
      let conditions = ` and id in ${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 = Vue.$login.convertToIn(ids)
    if (condition) {
      return ` and id in ${condition}`
    } else {
      return ' and 1!=1'
    }

  },

  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){
    if (tree.resourcetype === `${type}` && tree.name != '组织机构') {
      temp.push({label:tree.name,value:tree.name})
    }
    if(tree.children.length>0){
      tree.children.forEach((res) => {
        LdapService.getorgname(temp, res, type)
      })
    }
    return temp;
  }

}

export default LdapService
