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/fishing_catches/fishing_catchesSlice'
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 EditFishing_catches = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    player_profile: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    fishing_spot: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    fish_species: null,
    

    
    
    

    

    

    

    

    
    caught_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'weight': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'length': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    quality_grade: '',
    

    

    

    

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

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

  const { fishing_catchesId } = router.query

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

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

  useEffect(() => {
      if (typeof fishing_catches === 'object') {

          const newInitialVal = {...initVals};

          Object.keys(initVals).forEach(el => newInitialVal[el] = (fishing_catches)[el])

          setInitialValues(newInitialVal);
      }
  }, [fishing_catches])

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PlayerProfile' labelFor='player_profile'>
        <Field
            name='player_profile'
            id='player_profile'
            component={SelectField}
            options={initialValues.player_profile}
            itemRef={'player_profiles'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'first_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='FishingSpot' labelFor='fishing_spot'>
        <Field
            name='fishing_spot'
            id='fishing_spot'
            component={SelectField}
            options={initialValues.fishing_spot}
            itemRef={'fishing_spots'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="CaughtAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.caught_at ?
                  new Date(
                      dayjs(initialValues.caught_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'caught_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Weight"
    >
        <Field
            type="number"
            name="weight"
            placeholder="Weight"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Length"
    >
        <Field
            type="number"
            name="length"
            placeholder="Length"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="QualityGrade" labelFor="quality_grade">
        <Field name="quality_grade" id="quality_grade" component="select">
          
            <option value="standard">standard</option>
          
            <option value="fresh">fresh</option>
          
            <option value="premium">premium</option>
          
        </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('/fishing_catches/fishing_catches-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditFishing_catches
