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

    

    

    

    

    

    

    

    

    

    

    
    loan: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    payrollItem: null,
    

    
    
    
    'amountDeducted': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    deductionDate: new Date(),
    

    

    

    

    

    

    

    
    
    
    'balanceBefore': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'balanceAfter': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    isDeferred: false,
    

    

    

    

    

    

    
    
    

    
    deferredReason: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { loan_deductionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Loan' labelFor='loan'>
        <Field
            name='loan'
            id='loan'
            component={SelectField}
            options={initialValues.loan}
            itemRef={'employee_loans'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'loanSource'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PayrollItem' labelFor='payrollItem'>
        <Field
            name='payrollItem'
            id='payrollItem'
            component={SelectField}
            options={initialValues.payrollItem}
            itemRef={'payroll_items'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'payslipHash'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="AmountDeducted"
    >
        <Field
            type="number"
            name="amountDeducted"
            placeholder="AmountDeducted"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="BalanceBefore"
    >
        <Field
            type="number"
            name="balanceBefore"
            placeholder="BalanceBefore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="BalanceAfter"
    >
        <Field
            type="number"
            name="balanceAfter"
            placeholder="BalanceAfter"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsDeferred' labelFor='isDeferred'>
        <Field
            name='isDeferred'
            id='isDeferred'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="DeferredReason" hasTextareaHeight>
        <Field name="deferredReason" as="textarea" placeholder="DeferredReason" />
    </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('/loan_deductions/loan_deductions-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditLoan_deductions
