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/adbuilder_templates/adbuilder_templatesSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'

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

    'name': '',

    'created_by': '',

    created_date: new Date(),

    'last_modified_by': '',

    last_modified_date: new Date(),

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

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

  const { adbuilder_templatesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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

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

    <FormField
        label="Created By"
    >
        <Field
            name="created_by"
            placeholder="Created By"
        />
    </FormField>

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

    <FormField
        label="Last Modified By"
    >
        <Field
            name="last_modified_by"
            placeholder="Last Modified By"
        />
    </FormField>

      <FormField
          label="Last Modified Date"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.last_modified_date ?
                  new Date(
                      dayjs(initialValues.last_modified_date).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'last_modified_date': date})}
          />
      </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('/adbuilder_templates/adbuilder_templates-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAdbuilder_templates
