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/employee_documents/employee_documentsSlice'
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 EditEmployee_documents = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    employee: null,
    

    
    
    

    

    

    

    

    

    

    

    
    documentType: '',
    

    

    

    

    
    
    
    'fileName': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    filePath: [],
    

    

    

    
    
    

    

    

    

    

    
    uploadedAt: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    uploadedByUser: null,
    

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

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

  const { employee_documentsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Employee' labelFor='employee'>
        <Field
            name='employee'
            id='employee'
            component={SelectField}
            options={initialValues.employee}
            itemRef={'employees'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'employeeCode'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DocumentType" labelFor="documentType">
        <Field name="documentType" id="documentType" component="select">
          
            <option value="NBI_CLEARANCE">NBI_CLEARANCE</option>
          
            <option value="MEDICAL_CERTIFICATE">MEDICAL_CERTIFICATE</option>
          
            <option value="SSS_FORM">SSS_FORM</option>
          
            <option value="PHILHEALTH_MDR">PHILHEALTH_MDR</option>
          
            <option value="PAGIBIG_MMID">PAGIBIG_MMID</option>
          
            <option value="BIR_FORM">BIR_FORM</option>
          
            <option value="EMPLOYMENT_CONTRACT">EMPLOYMENT_CONTRACT</option>
          
            <option value="JOB_OFFER">JOB_OFFER</option>
          
            <option value="DIPLOMA">DIPLOMA</option>
          
            <option value="TRAINING_CERTIFICATE">TRAINING_CERTIFICATE</option>
          
            <option value="PERFORMANCE_APPRAISAL">PERFORMANCE_APPRAISAL</option>
          
            <option value="CONSENT_FORM">CONSENT_FORM</option>
          
            <option value="OTHER">OTHER</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="FileName"
    >
        <Field
            name="fileName"
            placeholder="FileName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditEmployee_documents
