import React from 'react'
import * as Yup from 'yup';
// import { useState, useEffect } from 'react';
import { useFormik, Form, FormikProvider } from 'formik';

import {
    Box, Button, Stack, Checkbox,
    TextField, Autocomplete,
} from '@mui/material';

import {
    CheckBox as CheckBoxIcon,
    CheckBoxOutlineBlank as CheckBoxOutlineBlankIcon
} from '@mui/icons-material';

const delete_icon = <CheckBoxOutlineBlankIcon fontSize="small" />;
const checkedIcon = <CheckBoxIcon fontSize="small" />;


export function StepTree(props) {

    const PlazaScheme = Yup.object().shape({
        puesto: 
        Yup.array()
        .required('Escoge un puesto valido')
        .min(1,"Debes escoger por lo menos una prueba")
    });

    let {  handleNext, handleBack, password, setPassword } = props

    const formik = useFormik({
        initialValues: {
            puesto: 
            password.index_pruebas
        },
        onSubmit: (fields) => {
            console.log('PRUEBAS :: -> ',fields)
            setPassword({
                ...password,
                index_pruebas : fields.puesto
            })
            handleNext()
        },
        validationSchema: PlazaScheme,
    });

    const { errors, touched,handleSubmit, getFieldProps,setValues } = formik;

    return (
        <FormikProvider style={{ padding : 25 }} value={formik}>
            <Form autoComplete="off" noValidate onSubmit={handleSubmit}>
                <Stack spacing={2}>
                    <Autocomplete
                        {...getFieldProps('puesto')}
                        onChange={(_a,current_list,_c,_individual,_f) => {
                            setValues({
                                puesto:current_list
                            })
                        }}
                        isOptionEqualToValue={(option, value) => option.label === value.label}
                        multiple
                        id="checkboxes-tags-demo"
                        options={top100Films}
                        disableCloseOnSelect
                        getOptionLabel={(option) => { 
                            return option.label 
                        }}
                        renderOption={(props, option, { selected }) => (
                            <li {...props}>
                                <Checkbox
                                    icon={delete_icon}
                                    checkedIcon={checkedIcon}
                                    style={{ marginRight: 8 }}
                                    checked={selected}
                                    />
                                {option.label}
                            </li>
                        )}
                        renderInput={(params) => (
                            <TextField 
                                // {...getFieldProps('puesto')}
                                error={Boolean(touched.puesto && errors.puesto)}
                                helperText={touched.puesto && errors.puesto}
                                {...params} 
                                label="Escribe el nombre de la prueba" 
                                placeholder="Prueba" 
                            />
                        )}
                    />

                    <Box sx={{ mb: 2 }}>
                        <div style={{ paddingTop  : 15}}>
                            <Button
                                type="submit"
                                className="registerBtn" 
                                variant="contained"
                                sx={{ mt: 1, mr: 1 }}
                            >
                                {'Siguiente'}
                            </Button>
                            <Button
                                disabled={false}
                                onClick={handleBack}
                                sx={{ mt: 1, mr: 1 }}
                            >
                                Regresar
                            </Button>
                        </div>
                    </Box>

                </Stack>
            </Form>
        </FormikProvider>
    );
}
const top100Films = [
    { label: 'City of God', year: 2002 },
    { label: 'Se7en', year: 1995 },
    { label: 'The Silence of the Lambs', year: 1991 },
    { label: "It's a Wonderful Life", year: 1946 },
    { label: 'Life Is Beautiful', year: 1997 },
    { label: 'The Usual Suspects', year: 1995 },
    { label: 'Grave of the Fireflies', year: 1988 },
    { label: 'Paths of Glory', year: 1957 },
    { label: 'Django Unchained', year: 2012 },
    { label: 'The Shining', year: 1980 },
    { label: 'WALL·E', year: 2008 },
    { label: 'American Beauty', year: 1999 },
    { label: 'The Dark Knight Rises', year: 2012 },
];


