import { Outlet, Link as RouterLink, useLocation } from "react-router-dom";
import { Box, Stack, Button } from "@mui/material";
import { setAdminToken } from "../../lib/adminAuth";
import { ADMIN_BASE } from "../../lib/config";

export default function AdminLayout() {
    const loc = useLocation();
    const is = (s: string) => loc.pathname.endsWith(s);
    return (
        <Box p={2}>
            <Stack direction="row" spacing={1} mb={2}>
                <Button component={RouterLink} to="boards" variant={is(`${ADMIN_BASE}/boards`) ? "contained" : "outlined"}>게시판관리</Button>
                <Button component={RouterLink} to="users"  variant={is(`${ADMIN_BASE}/users`)  ? "contained" : "outlined"}>회원관리</Button>
                <Box flex={1} />
                <Button color="error" onClick={()=>{ setAdminToken(null); location.href = location.pathname.replace(/\/[^\/]*$/, `${ADMIN_BASE}/login`); }}>
                    로그아웃
                </Button>
            </Stack>
            <Outlet />
        </Box>
    );
}
