import React, { useState } from 'react';

export const EdtAthlete = () => {
    const [formData, setFormData] = useState({
        name: '',
        prenom: '',
        id_keycloak: '',
        categorie: '',
        niveau: ''
    });

    const handleSubmit = async (e: React.FormEvent) => {
        e.preventDefault();
        try {
            const response = await fetch("http://localhost:8081/api/athlete/create", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify(formData),
            });

            if (response.ok) {
                alert("Athlete created successfully in PostgreSQL!");
                setFormData({ name: '', prenom: '', id_keycloak: '', categorie: '', niveau: '' });
            } else {
                alert("Failed to create athlete. Status: " + response.status);
            }
        } catch (error) {
            console.error("Error creating athlete:", error);
            alert("Error: Check console");
        }
    };

    return (
        <div style={{ padding: '20px', border: '1px solid #ccc', margin: '10px' }}>
            <h3>Test Create Athlete (PostgreSQL)</h3>
            <form onSubmit={handleSubmit}>
                <div>
                    <label>Nom: </label>
                    <input type="text" value={formData.name} onChange={(e) => setFormData({...formData, name: e.target.value})} />
                </div>
                <div>
                    <label>Prénom: </label>
                    <input type="text" value={formData.prenom} onChange={(e) => setFormData({...formData, prenom: e.target.value})} />
                </div>
                <div>
                    <label>Keycloak ID: </label>
                    <input type="text" value={formData.id_keycloak} onChange={(e) => setFormData({...formData, id_keycloak: e.target.value})} />
                </div>
                <div>
                    <label>Catégorie: </label>
                    <input type="text" value={formData.categorie} onChange={(e) => setFormData({...formData, categorie: e.target.value})} />
                </div>
                <div>
                    <label>Niveau: </label>
                    <input type="text" value={formData.niveau} onChange={(e) => setFormData({...formData, niveau: e.target.value})} />
                </div>
                <button type="submit" style={{ marginTop: '10px' }}>Créer l'athlète</button>
            </form>
        </div>
    );
};

export default EdtAthlete;
