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/attendance_events/attendance_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 EditAttendance_eventsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    restaurant: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    staff_user: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    work_shift: null,
    

    
    
    

    

    

    

    

    

    

    

    
    event_type: '',
    

    

    

    

    
    
    

    

    

    

    

    
    event_time: new Date(),
    

    

    

    

    

    

    

    
    
    
    'latitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'longitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'distance_meters': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    out_of_geofence: false,
    

    

    

    

    

    

    
    
    

    
    comment: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit attendance_events')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit attendance_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='Personnel' labelFor='staff_user'>
        <Field
            name='staff_user'
            id='staff_user'
            component={SelectField}
            options={initialValues.staff_user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Shift' labelFor='work_shift'>
        <Field
            name='work_shift'
            id='work_shift'
            component={SelectField}
            options={initialValues.work_shift}
            itemRef={'work_shifts'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'shift_notes'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Typed'évènement" labelFor="event_type">
        <Field name="event_type" id="event_type" component="select">
          
            <option value="check_in">check_in</option>
          
            <option value="check_out">check_out</option>
          
            <option value="absence">absence</option>
          
            <option value="retard">retard</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="Dateetheure"
      >
          <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="Latitude"
    >
        <Field
            type="number"
            name="latitude"
            placeholder="Latitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Longitude"
    >
        <Field
            type="number"
            name="longitude"
            placeholder="Longitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Distance(mètres)"
    >
        <Field
            type="number"
            name="distance_meters"
            placeholder="Distance(mètres)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Horszoneautorisée' labelFor='out_of_geofence'>
        <Field
            name='out_of_geofence'
            id='out_of_geofence'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

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

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

export default EditAttendance_eventsPage
