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/applicants/applicantsSlice'
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 EditApplicants = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    jobRequisition: null,
    

    
    
    

    

    

    

    

    
    applicationDate: new Date(),
    

    

    

    

    

    

    

    
    
    
    'firstName': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'lastName': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'mobile': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    source: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    resumeFile: [],
    

    

    

    
    
    

    

    

    

    

    
    interviewSchedule: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    interviewResult: '',
    

    

    

    

    
    
    
    'psychTestResult': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'medicalResult': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'backgroundCheck': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    jobOfferStatus: '',
    

    

    

    

    
    
    

    

    

    

    

    
    expectedStartDate: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    convertedEmployee: null,
    

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

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

  const { applicantsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='JobRequisition' labelFor='jobRequisition'>
        <Field
            name='jobRequisition'
            id='jobRequisition'
            component={SelectField}
            options={initialValues.jobRequisition}
            itemRef={'job_requisitions'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'positionTitle'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="FirstName"
    >
        <Field
            name="firstName"
            placeholder="FirstName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="LastName"
    >
        <Field
            name="lastName"
            placeholder="LastName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Email"
    >
        <Field
            name="email"
            placeholder="Email"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Mobile"
    >
        <Field
            name="mobile"
            placeholder="Mobile"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Source" labelFor="source">
        <Field name="source" id="source" component="select">
          
            <option value="WALK_IN">WALK_IN</option>
          
            <option value="REFERRAL">REFERRAL</option>
          
            <option value="JOB_BOARD">JOB_BOARD</option>
          
            <option value="INTERNAL">INTERNAL</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='ResumeFile'
            color='info'
            icon={mdiUpload}
            path={'applicants/resumeFile'}
            name='resumeFile'
            id='resumeFile'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="InterviewResult" labelFor="interviewResult">
        <Field name="interviewResult" id="interviewResult" component="select">
          
            <option value="PASS">PASS</option>
          
            <option value="FAIL">FAIL</option>
          
            <option value="SECOND_INTERVIEW">SECOND_INTERVIEW</option>
          
            <option value="PENDING">PENDING</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="PsychTestResult"
    >
        <Field
            name="psychTestResult"
            placeholder="PsychTestResult"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="MedicalResult"
    >
        <Field
            name="medicalResult"
            placeholder="MedicalResult"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BackgroundCheckResult"
    >
        <Field
            name="backgroundCheck"
            placeholder="BackgroundCheckResult"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="JobOfferStatus" labelFor="jobOfferStatus">
        <Field name="jobOfferStatus" id="jobOfferStatus" component="select">
          
            <option value="PENDING">PENDING</option>
          
            <option value="ACCEPTED">ACCEPTED</option>
          
            <option value="DECLINED">DECLINED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditApplicants
