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/classroom_memberships/classroom_membershipsSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



const EditClassroom_memberships = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    classroom: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    student: null,
    

    
    
    

    

    

    

    

    
    joined_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    membership_status: '',
    

    

    

    

    
    
    
    'join_method': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    schools: null,
    

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

  const { classroom_memberships } = useAppSelector((state) => state.classroom_memberships)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { classroom_membershipsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Classroom' labelFor='classroom'>
        <Field
            name='classroom'
            id='classroom'
            component={SelectField}
            options={initialValues.classroom}
            itemRef={'classrooms'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'classroom_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="MembershipStatus" labelFor="membership_status">
        <Field name="membership_status" id="membership_status" component="select">
          
            <option value="pending">pending</option>
          
            <option value="active">active</option>
          
            <option value="removed">removed</option>
          
            <option value="blocked">blocked</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="JoinMethod"
    >
        <Field
            name="join_method"
            placeholder="JoinMethod"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Notes" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Notes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditClassroom_memberships
