import React from 'react';
import { Navigate } from 'react-router-dom';

const PermissionRoute = ({ requiredRoles, children }) => {
  // 从localStorage获取用户信息
  const user = JSON.parse(localStorage.getItem('user') || '{}');
  const userRole = user.role || 'guest';
  
  // 检查用户是否有所需角色
  if (requiredRoles.includes(userRole)) {
    return children;
  }
  
  // 如果没有权限，重定向到未授权页面
  return <Navigate to="/unauthorized" replace />;
};

export default PermissionRoute;