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/time_records/time_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 EditTime_recordsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    employee: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    shift: null,
    

    
    
    

    

    

    

    

    
    workDate: new Date(),
    

    

    

    

    

    

    

    
    
    
    'scheduledTimeIn': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'scheduledTimeOut': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    actualTimeIn: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    actualTimeOut: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    tardinessMinutes: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    undertimeMinutes: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    overtimeMinutes: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'nightDiffHours': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    workType: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    isAbsent: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    isHalfDay: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    halfDayType: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    source: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    modifiedByUser: null,
    

    
    
    

    
    modificationReason: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    approvedByUser: null,
    

    
    
    

    

    

    

    

    
    approvedAt: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    payrollRun: null,
    

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

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

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof time_records === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (time_records)[el])
          setInitialValues(newInitialVal);
      }
  }, [time_records])

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

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Shift' labelFor='shift'>
        <Field
            name='shift'
            id='shift'
            component={SelectField}
            options={initialValues.shift}
            itemRef={'shifts'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="ScheduledTimeIn"
    >
        <Field
            name="scheduledTimeIn"
            placeholder="ScheduledTimeIn"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ScheduledTimeOut"
    >
        <Field
            name="scheduledTimeOut"
            placeholder="ScheduledTimeOut"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TardinessMinutes"
    >
        <Field
            type="number"
            name="tardinessMinutes"
            placeholder="TardinessMinutes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="UndertimeMinutes"
    >
        <Field
            type="number"
            name="undertimeMinutes"
            placeholder="UndertimeMinutes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="OvertimeMinutes"
    >
        <Field
            type="number"
            name="overtimeMinutes"
            placeholder="OvertimeMinutes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="NightDiffHours"
    >
        <Field
            type="number"
            name="nightDiffHours"
            placeholder="NightDiffHours"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="WorkType" labelFor="workType">
        <Field name="workType" id="workType" component="select">
          
            <option value="REGULAR">REGULAR</option>
          
            <option value="REST_DAY">REST_DAY</option>
          
            <option value="REGULAR_HOLIDAY">REGULAR_HOLIDAY</option>
          
            <option value="SPECIAL_HOLIDAY">SPECIAL_HOLIDAY</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsAbsent' labelFor='isAbsent'>
        <Field
            name='isAbsent'
            id='isAbsent'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsHalfDay' labelFor='isHalfDay'>
        <Field
            name='isHalfDay'
            id='isHalfDay'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="HalfDayType" labelFor="halfDayType">
        <Field name="halfDayType" id="halfDayType" component="select">
          
            <option value="AM">AM</option>
          
            <option value="PM">PM</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Source" labelFor="source">
        <Field name="source" id="source" component="select">
          
            <option value="BIOMETRIC">BIOMETRIC</option>
          
            <option value="MANUAL">MANUAL</option>
          
            <option value="SYSTEM">SYSTEM</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  
    <FormField label="ModificationReason" hasTextareaHeight>
        <Field name="modificationReason" as="textarea" placeholder="ModificationReason" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditTime_recordsPage
