import React, { useState, useEffect } from "react";
import { MDBRow, MDBCol, MDBInput, MDBBtn, MDBCard, MDBCardBody, MDBModalFooter, MDBIcon, MDBContainer } from 'mdbreact';
import axios from 'axios';
import eventData from "../../eventData/eventData.json";

function Submit(props) {

    /*var [participants,setParticipants] = useState([]);
        useEffect(() => {
            const interval = setInterval(() => {
            axios.get('/api/participants_by_id',{
                id: props.participant.value
            }).then((response) => setParticipants(response.data));
            }, 500);
        return () => clearInterval(interval);
        }, []);*/


    function addScore() {

        /*if (participants[0] === undefined) {
            axios.post('/api/staff/new_participant_with_id',
        {
            id: props.participant.value,
            name: props.participant.label
        })
        }*/

        const timenow = Date.now();
        var realId;
        var realName;

        if (timenow < 0/*eventData.dateFin*/) {
            realId = props.participant.value;
            realName = props.participant.label
        }
        else {
            realId = 'sprint';
            realName = 'Sprint'
        }

        axios.post('/api/staff/new_score',
            {
                score: Math.max(0, props.score),
                equipe: props.equipe.value,
                participant: realId//props.participant.value
            })
            .then(() => {
                axios.post('/api/staff/new_participant_with_id',
                    {
                        id: realId,//props.participant.value,
                        name: realName//props.participant.label
                    })
            })
            .then(() => {
                alert("Score ajouté");
                window.location = '/Input';
            }, (error) => {
                console.log(error);
            })
    };








    const onSubmit = () => { addScore() };

    return (
        <MDBBtn type="submit" icon="send" gradient="blue" rounded disabled={props.unlock} className="btn-block z-depth-1a" onClick={onSubmit}><MDBIcon icon="paper-plane" className="ml-1" /> Envoyer</MDBBtn>
    )
}

export default Submit;