//
// Copyright 2026 James Pace
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0.
//
import { Container, Row, Col, Card, Button, Form } from "react-bootstrap";
import { atom, useAtomValue } from "jotai";
import { useNavigate } from "react-router-dom";

export function Login() {
  return (
    <div>
      <Container className="vert-padded">
        <LoginForm />
      </Container>
    </div>
  );
}

function LoginForm() {
  const navigate = useNavigate();

  const onSubmit = async (event) => {
    event.preventDefault();

    console.log(event.target);
    const formEntries = new FormData(event.target);
    const formData = Object.fromEntries(formEntries);
    console.log(formData);

    const resp = await fetch("/api/login", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(formData),
    });
    if (resp.redirected) {
      navigate("/");
    }
  };
  return (
    <Form onSubmit={onSubmit}>
      <Form.Group controlId="passwordForm">
        <Form.Label>Password</Form.Label>
        <Form.Control name="password" type="password" />
      </Form.Group>

      <Button type="submit">Submit</Button>
    </Form>
  );
}
