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

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

    event_order: null,

    ticket_type: null,

    'code': '',

    status: '',

    'qr_svg': '',

    'issued_to_name': '',

    'issued_to_email': '',

    properties: null,

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

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

  const { event_ticketsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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

    <FormField label='EventOrder' labelFor='event_order'>
        <Field
            name='event_order'
            id='event_order'
            component={SelectField}
            options={initialValues.event_order}
            itemRef={'event_orders'}

            showField={'stripe_payment_intent_id'}

        ></Field>
    </FormField>

    <FormField label='TicketType' labelFor='ticket_type'>
        <Field
            name='ticket_type'
            id='ticket_type'
            component={SelectField}
            options={initialValues.ticket_type}
            itemRef={'event_ticket_types'}

            showField={'name'}

        ></Field>
    </FormField>

    <FormField
        label="Code"
    >
        <Field
            name="code"
            placeholder="Code"
        />
    </FormField>

    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">

            <option value="valid">valid</option>

            <option value="used">used</option>

            <option value="refunded">refunded</option>

        </Field>
    </FormField>

    <FormField
        label="QRSVG"
    >
        <Field
            name="qr_svg"
            placeholder="QRSVG"
        />
    </FormField>

    <FormField
        label="IssuedToName"
    >
        <Field
            name="issued_to_name"
            placeholder="IssuedToName"
        />
    </FormField>

    <FormField
        label="IssuedToEmail"
    >
        <Field
            name="issued_to_email"
            placeholder="IssuedToEmail"
        />
    </FormField>

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

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

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

export default EditEvent_tickets
