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 FormFilePicker from '../../components/FormFilePicker'
import FormImagePicker from '../../components/FormImagePicker'
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/stripe_events/stripe_eventsSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";



const EditStripe_eventsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'event_ref': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    event_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    membership: null,
    

    
    
    

    

    

    

    

    
    received_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    processed: false,
    

    

    

    

    

    

    
    
    

    
    processing_notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="EventRef"
    >
        <Field
            name="event_ref"
            placeholder="EventRef"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EventType" labelFor="event_type">
        <Field name="event_type" id="event_type" component="select">
          
            <option value="checkout.session.completed">checkout.session.completed</option>
          
            <option value="customer.subscription.created">customer.subscription.created</option>
          
            <option value="customer.subscription.updated">customer.subscription.updated</option>
          
            <option value="customer.subscription.deleted">customer.subscription.deleted</option>
          
            <option value="invoice.paid">invoice.paid</option>
          
            <option value="invoice.payment_failed">invoice.payment_failed</option>
          
            <option value="payment_intent.succeeded">payment_intent.succeeded</option>
          
            <option value="payment_intent.payment_failed">payment_intent.payment_failed</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='User' labelFor='user'>
        <Field
            name='user'
            id='user'
            component={SelectField}
            options={initialValues.user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Membership' labelFor='membership'>
        <Field
            name='membership'
            id='membership'
            component={SelectField}
            options={initialValues.membership}
            itemRef={'memberships'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'stripe_subscription_ref'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Processed' labelFor='processed'>
        <Field
            name='processed'
            id='processed'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="ProcessingNotes" hasTextareaHeight>
        <Field name="processing_notes" as="textarea" placeholder="ProcessingNotes" />
    </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('/stripe_events/stripe_events-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditStripe_eventsPage
