import Store from '@/store'
import Api from '@/api'
export default {
  //专利数据库权限
  // hasRole(projectId, roles){
  //   if (!projectId) {
  //     return true
  //   }
  //   const permissions = Store.getters && Store.getters.permissions
  //   const p = permissions[projectId]
  //   return p === 0 || roles.indexOf(p) !== -1
  // },
 
  //专利数据库权限
  // hasPermission(sign){
  //   return (Store.state.admin.permission).indexOf(sign) !== -1
  // },
  //报告权限
  reportPermission(reportId, roles){
    if(!reportId){
        return true
    }
  const role = Store.state.user.userinfo
    if(role.roleType === 1){
        return true
    }
    const permissions = Store.getters && Store.getters.permissions
    if(Object.keys(permissions).length==0){
        return true
    }
    const reportRole = permissions[reportId]
    if(!reportRole){
        return true
    }
        var result = false
        for(var item of reportRole){
            if(roles.includes(item)){
                result = true
                break
            }
        }
    return result
  },

   /**
   * 项目权限
   * @param {*} projectId 项目ID
   */
  async projectPermission(projectId){
    var data = {}
    if(sessionStorage.getItem('projectPermission')){
      data = JSON.parse(sessionStorage.getItem('projectPermission'))
    }
    var obj = data[projectId]
    if(!obj){

    }
    if(!obj){
      return false
    }
    return true
  },
  /**
   * 功能权限
   * @param {String} str 
   */
  FunPermissions(str){
    const user = Store.state.user.userinfo
    if(user.roleType){
      return true
    }
    const permissionList = Store.state.permission.permission
    return permissionList.includes(str)
  },
  /**
   * 产品权限
   * @param {*} id //产品id或者产品类别id
   * @param {*} type //类型，1是产品类别，2是产品
   */
  async producePermission(id,type){
    if(!id || !type){
      return false
    }
    const user = Store.state.user.userinfo
    var data = {}
    if(type == 2){
      var str = sessionStorage.getItem('product')
      if(str){
        data = JSON.parse(str)
      }
    }else if(type == 1){
      var str = sessionStorage.getItem('productCategory')
      if(str){
        data = JSON.parse(str)
      }
    }
    var obj = data[id]
    if(!obj){
      
      var searchQuery = ''
      var api = ''
      if(type == 2){
        api = 'queryProduct'
        searchQuery = `product_id=${id}`
      }else if(type == 1){
        api = 'queryProductCategory'
        searchQuery = `category_id=${id}`
      }
      var params = {
        searchQuery:searchQuery
      }
      await Api[api](params).then(response=>{
        if(response.code == 200){
          var d = response.data.data
          if(d && d.length>0){
            data[d[0].id] = d[0]
            if(type == 2){
              sessionStorage.setItem('product',JSON.stringify(data))
            }else if(type == 1){
              sessionStorage.setItem('productCategory',JSON.stringify(data))
            }
            obj = d[0]
          }
        }
      })
    }
    if(!obj){
      return false
    }
    if(obj.createId == user.id){
      return true
    }
    if(obj.adminIds && obj.adminIds.includes(user.id)){
      return true
    }

    return false
  }
}
