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

    

    

    

    

    
    periodFrom: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    periodTo: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    payrollType: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    lockedAt: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    computedAt: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    approvedAt: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    postedAt: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    approvedByUser: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    postedByUser: null,
    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    journalEntry: null,
    

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

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

  const { payroll_runsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PayrollType" labelFor="payrollType">
        <Field name="payrollType" id="payrollType" component="select">
          
            <option value="SEMI_MONTHLY_1">SEMI_MONTHLY_1</option>
          
            <option value="SEMI_MONTHLY_2">SEMI_MONTHLY_2</option>
          
            <option value="THIRTEENTH_MONTH">THIRTEENTH_MONTH</option>
          
            <option value="FINAL_PAY">FINAL_PAY</option>
          
            <option value="ADJUSTMENT">ADJUSTMENT</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="DRAFT">DRAFT</option>
          
            <option value="COMPUTED">COMPUTED</option>
          
            <option value="APPROVED">APPROVED</option>
          
            <option value="POSTED">POSTED</option>
          
            <option value="VOIDED">VOIDED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="ComputedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.computedAt ?
                  new Date(
                      dayjs(initialValues.computedAt).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'computedAt': date})}
          />
      </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="PostedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.postedAt ?
                  new Date(
                      dayjs(initialValues.postedAt).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'postedAt': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  
    <FormField label="Notes" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Notes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditPayroll_runs
