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/work_shifts/work_shiftsSlice'
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 EditWork_shifts = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    work_schedule: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    staff_user: null,
    

    
    
    

    

    

    

    

    
    shift_start: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    shift_end: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    shift_role: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    shift_status: '',
    

    

    

    

    
    
    

    
    shift_notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { work_shiftsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Planning' labelFor='work_schedule'>
        <Field
            name='work_schedule'
            id='work_schedule'
            component={SelectField}
            options={initialValues.work_schedule}
            itemRef={'work_schedules'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Rôledushift" labelFor="shift_role">
        <Field name="shift_role" id="shift_role" component="select">
          
            <option value="superviseur">superviseur</option>
          
            <option value="chef_cuisinier">chef_cuisinier</option>
          
            <option value="cuisinier">cuisinier</option>
          
            <option value="caissier">caissier</option>
          
            <option value="barman">barman</option>
          
            <option value="serveur">serveur</option>
          
            <option value="agent_entretien">agent_entretien</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Statut" labelFor="shift_status">
        <Field name="shift_status" id="shift_status" component="select">
          
            <option value="planifie">planifie</option>
          
            <option value="confirme">confirme</option>
          
            <option value="annule">annule</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

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

export default EditWork_shifts
