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/account_applications/account_applicationsSlice'
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 EditAccount_applications = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    applicant: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    school: null,
    

    
    
    

    

    

    

    

    

    

    

    
    requested_role: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    application_status: '',
    

    

    

    

    
    
    

    
    application_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    reviewed_by: null,
    

    
    
    

    

    

    

    

    
    reviewed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    supporting_documents: [],
    

    

    

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

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

  const { account_applicationsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='School' labelFor='school'>
        <Field
            name='school'
            id='school'
            component={SelectField}
            options={initialValues.school}
            itemRef={'schools'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RequestedRole" labelFor="requested_role">
        <Field name="requested_role" id="requested_role" component="select">
          
            <option value="student">student</option>
          
            <option value="teacher">teacher</option>
          
            <option value="principal">principal</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ApplicationStatus" labelFor="application_status">
        <Field name="application_status" id="application_status" component="select">
          
            <option value="pending">pending</option>
          
            <option value="approved">approved</option>
          
            <option value="rejected">rejected</option>
          
            <option value="needs_more_info">needs_more_info</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="ApplicationNotes" hasTextareaHeight>
        <Field name="application_notes" as="textarea" placeholder="ApplicationNotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='SupportingDocuments'
            color='info'
            icon={mdiUpload}
            path={'account_applications/supporting_documents'}
            name='supporting_documents'
            id='supporting_documents'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></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('/account_applications/account_applications-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAccount_applications
