import { mdiChartTimelineVariant } from '@mdi/js'
import Head from 'next/head'
import React, { ReactElement, useEffect, useState } from 'react'

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 { update, fetch } from '../../stores/workers_comp_classes/workers_comp_classesSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'

const EditWorkers_comp_classesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  
  const initVals = {
    name: '',
    percentage: '',
  }
  
  const [initialValues, setInitialValues] = useState(initVals)

  const { workers_comp_classes } = useAppSelector((state) => state.workers_comp_classes)
  const { id } = router.query

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit Workers Comp Class')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit Workers Comp Class'} main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            enableReinitialize
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>
              <FormField label="Name" labelFor="name">
                <Field name="name" id="name" type="text" />
              </FormField>
              <FormField label="Percentage" labelFor="percentage">
                <Field name="percentage" id="percentage" type="number" step="0.01" />
              </FormField>
              <BaseDivider />
              <BaseButtons>
                <BaseButton type="submit" color="info" label="Submit" />
                <BaseButton type="reset" color="info" outline label="Reset" />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditWorkers_comp_classesPage
