import { useSelector } from "react-redux";
import { Navigate, Outlet, useLocation } from "react-router-dom"
import AdminHeader from "./AdminHeader";
import AdminMenu from "./AdminMenu";

const LayoutAdmin = ({children}) => {
    const location = useLocation()
    const fromPage = location.state?.from?.pathname || '/'
    console.log(fromPage)

    const user = useSelector(state => state.auth.user)
    console.log('RequireAuthIsAdmin user' , user)
    const role = user?.sub?.acl[2]
    console.log('RequireAuthIsAdmin role', role)

    if (role !== "admin"){
         return <Navigate to='/login' state={{from: location}} />
     }
    return <>
    <AdminHeader/>
    <Outlet/>
    <AdminMenu/>
    </>
}

export default LayoutAdmin