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 { SelectField } from "../../components/SelectField";
import { SelectFieldMany } from "../../components/SelectFieldMany";
import { SwitchField } from '../../components/SwitchField'
import {RichTextField} from "../../components/RichTextField";

import { update, fetch } from '../../stores/menus/menusSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'

const EditMenus = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {

    parent: null,

    'label': '',

    'url': '',

    'active_url': '',

    'permission': '',

    'icon': '',

    url: null,

    group: null,

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

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

  const { menusId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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

    <FormField label='Parent' labelFor='parent'>
        <Field
            name='parent'
            id='parent'
            component={SelectField}
            options={initialValues.parent}
            itemRef={'menus'}

            showField={'label'}

        ></Field>
    </FormField>

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

    <FormField
        label="URL"
    >
        <Field
            name="url"
            placeholder="URL"
        />
    </FormField>

    <FormField
        label="ActiveURL"
    >
        <Field
            name="active_url"
            placeholder="ActiveURL"
        />
    </FormField>

    <FormField
        label="Permission"
    >
        <Field
            name="permission"
            placeholder="Permission"
        />
    </FormField>

    <FormField
        label="Icon"
    >
        <Field
            name="icon"
            placeholder="Icon"
        />
    </FormField>

    <FormField label='URL' labelFor='url'>
        <Field
            name='url'
            id='url'
            component={SelectField}
            options={initialValues.url}
            itemRef={'urls'}

            showField={'uri'}

        ></Field>
    </FormField>

    <FormField label='Group' labelFor='group'>
        <Field
            name='group'
            id='group'
            component={SelectField}
            options={initialValues.group}
            itemRef={'group_menus'}

            showField={'name'}

        ></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('/menus/menus-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

EditMenus.getLayout = function getLayout(page: ReactElement) {
  return (
      <LayoutAuthenticated>
          {page}
      </LayoutAuthenticated>
  )
}

export default EditMenus
