import { mdiChartTimelineVariant } from '@mdi/js'
import Head from 'next/head'
import React, { ReactElement } 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 { create } from '../../stores/workers_comp_classes/workers_comp_classesSlice'
import { useAppDispatch } from '../../stores/hooks'
import { useRouter } from 'next/router'

const initialValues = {
    name: '',
    percentage: '',
}

const Workers_comp_classesNew = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()

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

  return (
    <>
      <Head>
        <title>{getPageTitle('New Workers Comp Class')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title="New Workers Comp Class" main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            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>
    </>
  )
}

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

export default Workers_comp_classesNew
