import { mdiChartTimelineVariant, mdiUpload } from '@mdi/js'
import Head from 'next/head'
import React, { ReactElement, useEffect, useState } from 'react'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";

import CardBox from '../../components/CardBox'
import LayoutAuthenticated from '../../layouts/Authenticated'
import SectionMain from '../../components/SectionMain'
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton'
import { getPageTitle } from '../../config'

import { Field, Form, Formik } from 'formik'
import FormField from '../../components/FormField'
import BaseDivider from '../../components/BaseDivider'
import BaseButtons from '../../components/BaseButtons'
import BaseButton from '../../components/BaseButton'
import FormCheckRadio from '../../components/FormCheckRadio'
import FormCheckRadioGroup from '../../components/FormCheckRadioGroup'
import FormFilePicker from '../../components/FormFilePicker'
import FormImagePicker from '../../components/FormImagePicker'
import { SelectField } from "../../components/SelectField";
import { SelectFieldMany } from "../../components/SelectFieldMany";
import { SwitchField } from '../../components/SwitchField'
import {RichTextField} from "../../components/RichTextField";

import { update, fetch } from '../../stores/recipes/recipesSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";



const EditRecipesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'recipe_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    photo: [],
    

    

    
    
    

    

    

    
    prep_time_minutes: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    cook_time_minutes: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    servings: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    difficulty: '',
    

    

    

    

    
    
    
    'tags_csv': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    ingredients_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    instructions_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    nutrition_per_serving_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_favourite: false,
    

    

    

    

    

    

    
    
    
    'rating': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'source_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
  }
  const [initialValues, setInitialValues] = useState(initVals)

  const { recipes } = useAppSelector((state) => state.recipes)
  

  const { id } = router.query

  useEffect(() => {
    dispatch(fetch({ id: id }))
  }, [id])

  useEffect(() => {
    if (typeof recipes === 'object') {
      setInitialValues(recipes)
    }
  }, [recipes])

  useEffect(() => {
      if (typeof recipes === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (recipes)[el])
          setInitialValues(newInitialVal);
      }
  }, [recipes])

  const handleSubmit = async (data) => {
    await dispatch(update({ id: id, data }))
    await router.push('/recipes/recipes-list')
  }

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit recipes')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit recipes'} main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            enableReinitialize
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='User' labelFor='user'>
        <Field
            name='user'
            id='user'
            component={SelectField}
            options={initialValues.user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="RecipeName"
    >
        <Field
            name="recipe_name"
            placeholder="RecipeName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='Photo'
                color='info'
                icon={mdiUpload}
                path={'recipes/photo'}
                name='photo'
                id='photo'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  
    <FormField
        label="PrepTime(minutes)"
    >
        <Field
            type="number"
            name="prep_time_minutes"
            placeholder="PrepTime(minutes)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CookTime(minutes)"
    >
        <Field
            type="number"
            name="cook_time_minutes"
            placeholder="CookTime(minutes)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Servings"
    >
        <Field
            type="number"
            name="servings"
            placeholder="Servings"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Difficulty" labelFor="difficulty">
        <Field name="difficulty" id="difficulty" component="select">
          
            <option value="beginner">beginner</option>
          
            <option value="intermediate">intermediate</option>
          
            <option value="chef">chef</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Tags"
    >
        <Field
            name="tags_csv"
            placeholder="Tags"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Ingredients" hasTextareaHeight>
        <Field name="ingredients_text" as="textarea" placeholder="Ingredients" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Instructions" hasTextareaHeight>
        <Field name="instructions_text" as="textarea" placeholder="Instructions" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="NutritionPerServingJSON" hasTextareaHeight>
        <Field name="nutrition_per_serving_json" as="textarea" placeholder="NutritionPerServingJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Favourite' labelFor='is_favourite'>
        <Field
            name='is_favourite'
            id='is_favourite'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="Rating(1-5)"
    >
        <Field
            type="number"
            name="rating"
            placeholder="Rating(1-5)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SourceURL"
    >
        <Field
            name="source_url"
            placeholder="SourceURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  


  

              <BaseDivider />
              <BaseButtons>
                <BaseButton type="submit" color="info" label="Submit" />
                <BaseButton type="reset" color="info" outline label="Reset" />
                <BaseButton type='reset' color='danger' outline label='Cancel' onClick={() => router.push('/recipes/recipes-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

EditRecipesPage.getLayout = function getLayout(page: ReactElement) {
  return (
      <LayoutAuthenticated
        
          permission={'UPDATE_RECIPES'}
        
      >
          {page}
      </LayoutAuthenticated>
  )
}

export default EditRecipesPage
