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/staff_assignments/staff_assignmentsSlice'
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 EditStaff_assignments = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    staff_user: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    camp_session: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    age_group: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    subject: null,
    

    
    
    

    

    

    

    

    

    

    

    
    assignment_role: '',
    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { staff_assignmentsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CampSession' labelFor='camp_session'>
        <Field
            name='camp_session'
            id='camp_session'
            component={SelectField}
            options={initialValues.camp_session}
            itemRef={'camp_sessions'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='AgeGroup' labelFor='age_group'>
        <Field
            name='age_group'
            id='age_group'
            component={SelectField}
            options={initialValues.age_group}
            itemRef={'age_groups'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Subject' labelFor='subject'>
        <Field
            name='subject'
            id='subject'
            component={SelectField}
            options={initialValues.subject}
            itemRef={'curriculum_subjects'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="AssignmentRole" labelFor="assignment_role">
        <Field name="assignment_role" id="assignment_role" component="select">
          
            <option value="teacher">teacher</option>
          
            <option value="counselor">counselor</option>
          
            <option value="shift_lead">shift_lead</option>
          
            <option value="driver">driver</option>
          
            <option value="volunteer">volunteer</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

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

export default EditStaff_assignments
