import React, {FC, FormEvent, useState} from "react"
import Field from "../Field"
import {timeTracker} from "../../utilities/types"

const TimeTrackerEdit: FC<TimeTrackerEditProps> = ({timeTracker}) => {

    const [trackerForm, setTrackerForm] = useState<timeTracker>(timeTracker)

    const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        setTrackerForm({...trackerForm, [event.target.name]: event.target.value.replace('T', ' ')})
    }

    const onSubmit = (event: FormEvent) => {
        console.log('submit', trackerForm, event)
    }

    return <form onSubmit={onSubmit}>
        <Field name="start_at"
               type="datetime-local"
               value={trackerForm.start_at}
               onChange={handleChange}/>
        <Field name="end_at"
               type="datetime-local"
               value={trackerForm.end_at}
               onChange={handleChange}/>

        <button type="submit" onClick={onSubmit}>Valider</button>
    </form>
}

export default TimeTrackerEdit

interface TimeTrackerEditProps {
    timeTracker: timeTracker,
}
