import "../App.css";
import React, { useState } from "react";
import HeaderAdmin from "./HeaderAdmin";
import Table from "./Table";
import Cookies from "js-cookie";

const Admin: React.FC = () => {
  // Keep token in state so UI updates immediately after login without reload
  const [token, setToken] = useState<string | null>(
    () => Cookies.get("token") ?? null
  );

  

  return (
    <>
      <HeaderAdmin
        token={token}
        onLoginSuccess={(t) => setToken(t)}
        onLogout={() => setToken(null)}
      />
      {token ? (
        <Table />
      ) : (
        <div className="p-4">Bitte als Admin einloggen. Oder gehe <a className="text-blue-500 hover:underline" href="/">zurück</a>.</div>
      )}
    </>
  );
};

export default Admin;
