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/fish_species/fish_speciesSlice'
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 EditFish_speciesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    item: null,
    

    
    
    

    

    

    

    

    

    

    

    
    rarity: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    active_time: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    preferred_weather: '',
    

    

    

    

    
    
    
    'base_bite_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    fish_images: [],
    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="Name"
    >
        <Field
            name="name"
            placeholder="Name"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Item' labelFor='item'>
        <Field
            name='item'
            id='item'
            component={SelectField}
            options={initialValues.item}
            itemRef={'items'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Rarity" labelFor="rarity">
        <Field name="rarity" id="rarity" component="select">
          
            <option value="common">common</option>
          
            <option value="uncommon">uncommon</option>
          
            <option value="rare">rare</option>
          
            <option value="epic">epic</option>
          
            <option value="legendary">legendary</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ActiveTime" labelFor="active_time">
        <Field name="active_time" id="active_time" component="select">
          
            <option value="day">day</option>
          
            <option value="night">night</option>
          
            <option value="any">any</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PreferredWeather" labelFor="preferred_weather">
        <Field name="preferred_weather" id="preferred_weather" component="select">
          
            <option value="sunny">sunny</option>
          
            <option value="rain">rain</option>
          
            <option value="storm">storm</option>
          
            <option value="cloudy">cloudy</option>
          
            <option value="fog">fog</option>
          
            <option value="heat_wave">heat_wave</option>
          
            <option value="wind">wind</option>
          
            <option value="any">any</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="BaseBiteRate"
    >
        <Field
            type="number"
            name="base_bite_rate"
            placeholder="BaseBiteRate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='FishImages'
                color='info'
                icon={mdiUpload}
                path={'fish_species/fish_images'}
                name='fish_images'
                id='fish_images'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </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('/fish_species/fish_species-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditFish_speciesPage
