import Vue from 'vue'
import { SEARCH } from '@vue2-client/services/apiService'
import { post } from '@vue2-client/services/api'

/**
 * 获取片区
 * @returns {Promise<void>}
 */
// eslint-disable-next-line
async function getAllArea () {
  try {
    const getAllArea = await post(SEARCH, {
      source: 'this.getParentByType($organization$).getChildByName($片区$).getChildren()',
      userid: Vue.$login.f.id
    })
    const areaTemp = []
    for (let i = 0; i < getAllArea.length; i++) {
      areaTemp.push({ label: getAllArea[i].name, value: getAllArea[i] })
    }
    Vue.$login.f.f_allArea = areaTemp
  } catch (error) {
    Vue.$login.f.f_allArea = []
    console.log('获取片区异常', error)
  }
}
/**
 * 获取抄表员
 * @returns {Promise<void>}
 */
// eslint-disable-next-line
async function getGasman () {
  try {
    const getGasman = await post(SEARCH, {
      source: 'this.getParentByType($organization$).getChildByName($抄表员$).getUsers()',
      userid: Vue.$login.f.id
    })
    const gasman = []
    for (let i = 0; i < getGasman.length; i++) {
      gasman.push(getGasman[i])
    }
    Vue.$login.f.f_gasman = gasman
  } catch (error) {
    Vue.$login.f.f_gasman = []
    console.log('获取抄表员异常', error)
  }
}
/**
 * 获取资源服务权限
 * @returns {Promise<void>}
 */
// eslint-disable-next-line
async function getLic () {
  try {
    const getLic = await post(SEARCH, {
      data: {
        source: 'this.getRights().where(row.getType()==$function$ && row.getPath($name$).indexOf($资源操作$) != -1)',
        userid: Vue.$login.f.id
      }
    }, { resolveMsg: null, rejectMsg: null })
    const arry = []
    getLic.forEach((item) => {
      arry.push(item.name)
    })
    Vue.$login.f.lics = arry
  } catch (error) {
    Vue.$login.f.f_gasman = []
    console.log('获取资源服务权限异常', error)
  }
}

/**
 * 获取界面细节 (界面权限)
 * @returns {Promise<void>}
 */
// eslint-disable-next-line
async function getViewDetails (userid = Vue.$login.f.id) {
  try {
    const viewDetails = await post(SEARCH, {
      source: 'this.getRights().where(row.getType()==$function$ && row.getPath($name$).indexOf($功能权限$) != -1)',
      userid: userid
    }, { resolveMsg: null, rejectMsg: null })
    const r = viewDetails
    Vue.$login.r = []
    r.forEach(x => Vue.$login.r.push(x.name))
  } catch (error) {
    Vue.$login.r = []
    console.log('获取界面细节异常', error)
  }
}
/**
 * 获取资源功能
 * @returns {Promise<void>}
 */
async function getResourceFunction () {
  try {
    const resourceFunction = await post(SEARCH, {
      source: 'tool.getFullTree(this.getRights().where(row.getName() == $组织管理$||row.getName() == $功能管理$||row.getName() == $部门管理$ || row.getName() == $角色管理$ || row.getName() == $人员管理$|| row.getName() == $资源调配$ ))',
      userid: Vue.$login.f.id
    }, { resolveMsg: null, rejectMsg: null })
    return resourceFunction
  } catch (error) {
    console.log('获取资源功能异常', error)
  }
}
/**
 * 获取组织管理数据
 * @returns {Promise<void>}
 */
async function getOrganization () {
  try {
    const resourceFunction = await post(SEARCH, {
      source: 'tool.getFullTree(this.getRights().where(row.getType() == $organization$||row.getType() == $zone$))',
      userid: Vue.$login.f.id
    }, { resolveMsg: null, rejectMsg: null })
    return resourceFunction
  } catch (error) {
    console.log('获取组织管理数据异常', error)
  }
}
/**
 * 完整资源树筛选指定名称的资源
 * @param fun 要筛选的资源树
 * @param name 筛选的资源名称
 * @returns {*}
 */
function searchFun (fun, name) {
  if (fun.length > 0) {
    if (fun[0].name === name) {
      return fun[0].children
    } else {
      return searchFun(fun[0].children, name)
    }
  }
}

const loginGen = async function (response) {
  Vue.$login.f = response
  await Promise.all([Vue.$appdata.load(), getViewDetails(Vue.$login.f.id)])
  const login = {
    f: Vue.$login.f,
    jwt: Vue.$login.f.id,
    r: Vue.$login.r
  }

  Vue.$store.commit('account/setLogin', login)
}
const GetLoginInfoService = {
  install (Vue) {
    // 给vue增添对话框显示方法
    Vue.$login = Vue.prototype.$login = GetLoginInfoService
  },

  f: {

  },

  login (response) {
    return loginGen(response)
  },
  required (param) {
    if (typeof param === 'number' && param === 0) {
      return false
    }
    if (!param) {
      return true
    }
    param = param + ''
    param = param.trim()
    return !param
  },
  // 不能选择器中截取掉多余的字符
  orgName (name) {
    return name.substring(10)
  }
}

export default GetLoginInfoService
export {
  getResourceFunction,
  getOrganization,
  searchFun,
  loginGen
}
