import co from 'co'

export function getNowDate () {
  // 求取当前时间的工具
  let myweekday = ''
  let year = ''
  let mydate = new Date()
  myweekday = mydate.getDay()
  let mymonth = mydate.getMonth() + 1
  let myday = mydate.getDate()
  let myyear = mydate.getYear()
  year = (myyear > 200) ? myyear : 1900 + myyear
  let week = ['星期日','星期一','星期二','星期三','星期四','星期五','星期日','星期六']
  let weekday = week[myweekday]

  // document.write("<font color=#ffffff>今天是 "+year+"年"+mymonth+"月"+myday+"日 "+weekday+"</font>");
  return  year + '年' + mymonth + '月' + myday + '日 ' + weekday
}

export function getImgsrc (name) {
  return "/images/"+name+".gif"
}

export var f

let gen = function * (self, util) {
  console.log('112userid-->' + self.userid)
  let getFullTree = yield self.$resetpost('/rs/search', {data: {
      source: 'tool.getFullTree(this.getRights())',
      userid: self.$login.f.id
    }})
  console.log('获取functions', getFullTree.data)
  if (self.$login.f.name != '超级管理员') {
    // 获取超管的id
    let getSuper = yield self.$resetpost('/rs/sql/ldap_getAllUser', {data: {condition: "name = '超级管理员'"}})
    console.log('获取超级管理员', getSuper)
    let getSuperId = getSuper.data[0].id
    let getManage = yield self.$resetpost('/rs/search', {data: {
        source: 'tool.getFullTree(this.getRights().where(row.getName() == $部门管理$ || row.getName() == $角色管理$ || row.getName() == $人员管理$ ))',
        userid: getSuperId
      }})
    // 将获取到的节点进行排序
    let after = getManage.data[0].children.sort((x, y) => {
      return (x.position - 0) - (y.position - 0)
    })
    after.forEach((res) => {
      getFullTree.data[0].children.push(res)
    })
  }
  self.functions = util.f = getFullTree.data
}

let licgen = function * (self, util) {
  yield self.$resetpost('rs/license/getData', {parentid: self.$login.f.id}).then(ret => {
    self.licensepolicy = self.$login.f.licensepolicy = ret.data
  }).catch(ret => {
    console.log(JSON.stringify(ret))
  })
}
let fungen = function * (self, util) {
  yield self.$resetpost('/rs/search', {data: {
      source: 'tool.getFullTree(this.getRights().where(row.getType() == $function$))',
      userid: self.$login.f.id
    }}).then(ret => {
    util.f.functions = ret.data
    console.log(self.functions)
  }).catch(ret => {
    console.log(JSON.stringify(ret))
  })
}
let ldapResgen = function * (self, util) {
  yield self.$resetpost('/rs/search', {data: {
      source: 'this.getRights().where(row.getType()==$function$ && row.getPath($name$).indexOf($资源权限$) != -1)',
      userid: self.$login.f.id
    }}).then(ret => {
    let arry=[]
    ret.data.forEach((item) => {
      arry.push(item.name)
    })
    util.f.ldapReslist=arry
  }).catch(ret => {
    console.log(JSON.stringify(ret))
  })
}
// 加载有权处理的功能
export function loadFuntionResource (self) {
  let g = fungen(self, this)
  return co(g)
}

// 加载有权处理的资源
export function loadRightResource (self) {
  let g = gen(self, this)
  return co(g)
}
//加载授权策略数据
export function loadlicensepolicy(self) {
  let g = licgen(self, this)
  return co(g)
}
//加载授权策略数据
export function loadLdapRes(self) {
  let g = ldapResgen(self, this)
  return co(g)
}

// 从资源中获得给定编号的资源
export function getResourceById (id) {
  // 资源库里找到了，返回找到的
  for (let value of this.f) {
    let res = getInnerResourceById(value, id)
    if (res) {
      return res
    }
  }

  return null
}

// 根据id号取资源，自己不是，递归从子中找
function getInnerResourceById (res, id) {
  if (res.id === id) {
    return res
  }

  // 子中有，返回子里的
  for (let value of res.children) {
    let ret = getInnerResourceById(value, id)
    if (ret) {
      return ret
    }
  }

  return null
}
