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

    

    

    

    

    

    

    

    

    

    

    
    employee: null,
    

    
    
    
    'contract_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    hire_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    contract_type: '',
    

    

    

    

    
    
    
    'position': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'work_schedule': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    probation_days: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'salary': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'daily_allowance': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'bonuses': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'deductions': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'bank_account_iban': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'bank_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'insurance_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tax_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tax_category': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    vacation_days: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    used_vacation_days: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    employment_status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    termination_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    termination_reason: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { employee_hr_recordsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit employee_hr_records')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit employee_hr_records'} 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={'last_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="ContractNumber"
    >
        <Field
            name="contract_number"
            placeholder="ContractNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ContractType" labelFor="contract_type">
        <Field name="contract_type" id="contract_type" component="select">
          
            <option value="PERMANENT">PERMANENT</option>
          
            <option value="FIXED">FIXED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Position"
    >
        <Field
            name="position"
            placeholder="Position"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="WorkSchedule"
    >
        <Field
            name="work_schedule"
            placeholder="WorkSchedule"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ProbationDays"
    >
        <Field
            type="number"
            name="probation_days"
            placeholder="ProbationDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Salary"
    >
        <Field
            type="number"
            name="salary"
            placeholder="Salary"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DailyAllowance"
    >
        <Field
            type="number"
            name="daily_allowance"
            placeholder="DailyAllowance"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Bonuses"
    >
        <Field
            type="number"
            name="bonuses"
            placeholder="Bonuses"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Deductions"
    >
        <Field
            type="number"
            name="deductions"
            placeholder="Deductions"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BankAccountIBAN"
    >
        <Field
            name="bank_account_iban"
            placeholder="BankAccountIBAN"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BankName"
    >
        <Field
            name="bank_name"
            placeholder="BankName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="InsuranceNumber"
    >
        <Field
            name="insurance_number"
            placeholder="InsuranceNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TaxNumber"
    >
        <Field
            name="tax_number"
            placeholder="TaxNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TaxCategory"
    >
        <Field
            name="tax_category"
            placeholder="TaxCategory"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="VacationDays"
    >
        <Field
            type="number"
            name="vacation_days"
            placeholder="VacationDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="UsedVacationDays"
    >
        <Field
            type="number"
            name="used_vacation_days"
            placeholder="UsedVacationDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EmploymentStatus" labelFor="employment_status">
        <Field name="employment_status" id="employment_status" component="select">
          
            <option value="ACTIVE">ACTIVE</option>
          
            <option value="SICK">SICK</option>
          
            <option value="VACATION">VACATION</option>
          
            <option value="TERMINATED">TERMINATED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="TerminationReason" hasTextareaHeight>
        <Field name="termination_reason" as="textarea" placeholder="TerminationReason" />
    </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_hr_records/employee_hr_records-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditEmployee_hr_records
