'use client'

import * as React from 'react';
import { Dialog, Button, TextField, FormLabel, RadioGroup, FormControlLabel, Radio, DialogTitle, DialogContent, DialogActions } from '@mui/material';

export default function EditDialog({ show, values, onClose, onSubmit, id = null }) {

    const [name_, setName] = React.useState('');
    const [infos_, setInfos] = React.useState('');
    const [shared_, setShared] = React.useState('');
    const [infosType_, setInfosType] = React.useState('infos_normal');

    React.useEffect(() => {
        if (values) {
            setName(values.NAME || '');
            setInfos(values.INFOS || '');
            setInfosType(values.INFOS_TYPE || 'infos_normal');
            setShared(values.SHARED || 0);
        }
    }, [values]);

    const handleSubmit = () => {
        onSubmit({ name: name_, infos: infos_, infosType: infosType_, shared: shared_, id: id });
    }

    return (
        <Dialog open={show} onClose={onClose} fullWidth maxWidth='sm'>
            <DialogTitle>Modifier la gares</DialogTitle>
            <DialogContent>
                <TextField fullWidth label='Nom de la gare' value={name_} onChange={(e) => setName(e.target.value)} />
                <TextField fullWidth multiline rows={4} label="Texte d'informations" value={infos_} onChange={(e) => setInfos(e.target.value)} />
                <FormLabel id='type_infos_label'>Type d&apos;informations</FormLabel>
                <RadioGroup row name='type_infos' aria-labelledby='infos_type_label' value={infosType_} onChange={(e) => setInfosType(e.target.value)}>
                    <FormControlLabel value='infos_normal' control={<Radio />} label='Informations' />
                    <FormControlLabel value='infos_flash' control={<Radio />} label='Flash circulation' />
                </RadioGroup>
                <FormLabel id='shared_label'>Partagée</FormLabel>
                <RadioGroup row name='shared' aria-labelledby='shared_label' value={shared_} onChange={(e) => setShared(e.target.value)}>
                    <FormControlLabel value={1} control={<Radio />} label='Partager' />
                    <FormControlLabel value={0} control={<Radio />} label='Ne pas partager' />
                </RadioGroup>
            </DialogContent>
            <DialogActions>
                <Button variant='text' onClick={onClose}>Annuler</Button>
                <Button variant='text' onClick={handleSubmit}>Valider</Button>
            </DialogActions>
        </Dialog>
    )
}