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 "http://127.0.0.1:8081/images/"+name+".gif"
}

export var f
import {HttpResetClass} from "vue-client";
let gen = function * (self, util) {
  console.log('112userid-->' + self.userid)
  yield new HttpResetClass().load('POST', '/rs/search', {data: {
    source: 'tool.getFullTree(this.getRights())',
    userid: self.$login.f.id
  }}).then(ret => {
    self.functions = util.f = ret.data
    console.log(self.functions)
  }).catch(ret => {
    console.log(JSON.stringify(ret))
    self.err('通过资源服务获取数据失败')
  })
}

let licgen = function * (self, util) {
  yield new HttpResetClass().load('POST', '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))
    self.err('通过资源服务获取数据失败')
  })
}

// 加载有权处理的资源
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 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
}
