import React, { useState } from "react";

function Login() {
    const [username, setUsername] = useState("");
    const [password, setPassword] = useState("");
    const [users, setUsers] = useState([]);

    function handleLogin(e) {
        e.preventDefault();

        fetch("http://localhost:5000/login", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ username, password })
        })
        .then(res => res.text())
        .then(data => {
            alert(data);
            handleView();
        });
    }

    function handleUpdate() {
        fetch("http://localhost:5000/update", {
            method: "PUT",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ username, password })
        })
        .then(res => res.text())
        .then(data => {
            alert(data);
            handleView();
        });
    }

    function handleDelete() {
        fetch("http://localhost:5000/delete", {
            method: "DELETE",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ username })
        })
        .then(res => res.text())
        .then(data => {
            alert(data);
            handleView();
        });
    }

    function handleView() {
        fetch("http://localhost:5000/users")
        .then(res => res.json())
        .then(data => setUsers(data));
    }

    return (
        <div align="center">
            <h2>Student Login</h2>

            <form onSubmit={handleLogin}>
                Username:
                <input
                    value={username}
                    onChange={e => setUsername(e.target.value)}
                    required
                />

                <br /><br />

                Password:
                <input
                    type="password"
                    value={password}
                    onChange={e => setPassword(e.target.value)}
                    required
                />

                <br /><br />

                <button type="submit">Login</button>
                <button type="button" onClick={handleUpdate}>
                    Update
                </button>
                <button type="button" onClick={handleDelete}>
                    Delete
                </button>
                <button type="button" onClick={handleView}>
                    View
                </button>
            </form>

            <br />

            <table border="1">
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Password</th>
                    </tr>
                </thead>

                <tbody>
                    {users.map(user => (
                        <tr key={user.username}>
                            <td>{user.username}</td>
                            <td>{user.password}</td>
                        </tr>
                    ))}
                </tbody>
            </table>
        </div>
    );
}

export default Login;