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/notification_events/notification_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 EditNotification_eventsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    restaurant: null,
    

    
    
    

    

    

    

    

    

    

    

    
    event_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    audience: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    order_nature: '',
    

    

    

    

    
    
    

    

    

    
    quantity_total: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    message: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    event_time: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_read: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    target_user: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Restaurant' labelFor='restaurant'>
        <Field
            name='restaurant'
            id='restaurant'
            component={SelectField}
            options={initialValues.restaurant}
            itemRef={'restaurants'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Typed'évènement" labelFor="event_type">
        <Field name="event_type" id="event_type" component="select">
          
            <option value="commande">commande</option>
          
            <option value="paiement">paiement</option>
          
            <option value="reservation">reservation</option>
          
            <option value="avis">avis</option>
          
            <option value="stock">stock</option>
          
            <option value="geofence">geofence</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Audience" labelFor="audience">
        <Field name="audience" id="audience" component="select">
          
            <option value="admins">admins</option>
          
            <option value="personnel">personnel</option>
          
            <option value="client">client</option>
          
            <option value="role_cible">role_cible</option>
          
            <option value="utilisateur_cible">utilisateur_cible</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Naturecommande" labelFor="order_nature">
        <Field name="order_nature" id="order_nature" component="select">
          
            <option value="nourriture">nourriture</option>
          
            <option value="boisson">boisson</option>
          
            <option value="mixte">mixte</option>
          
            <option value="na">na</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Quantitétotale"
    >
        <Field
            type="number"
            name="quantity_total"
            placeholder="Quantitétotale"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Titre"
    >
        <Field
            name="title"
            placeholder="Titre"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Message" hasTextareaHeight>
        <Field name="message" as="textarea" placeholder="Message" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Lu' labelFor='is_read'>
        <Field
            name='is_read'
            id='is_read'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Utilisateurcible' labelFor='target_user'>
        <Field
            name='target_user'
            id='target_user'
            component={SelectField}
            options={initialValues.target_user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/notification_events/notification_events-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditNotification_eventsPage
