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/weather_states/weather_statesSlice'
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 EditWeather_states = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    weather_type: '',
    

    

    

    

    
    
    
    'temperature_c': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'wind_speed': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'rain_intensity': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    has_thunder: false,
    

    

    

    

    

    

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

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

  const { weather_statesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="WeatherType" labelFor="weather_type">
        <Field name="weather_type" id="weather_type" 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>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TemperatureC"
    >
        <Field
            type="number"
            name="temperature_c"
            placeholder="TemperatureC"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="WindSpeed"
    >
        <Field
            type="number"
            name="wind_speed"
            placeholder="WindSpeed"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RainIntensity"
    >
        <Field
            type="number"
            name="rain_intensity"
            placeholder="RainIntensity"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='HasThunder' labelFor='has_thunder'>
        <Field
            name='has_thunder'
            id='has_thunder'
            component={SwitchField}
        ></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('/weather_states/weather_states-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditWeather_states
