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

    

    

    

    

    

    

    

    

    

    

    
    employee: null,
    

    
    
    

    

    

    

    

    

    

    

    
    loanType: '',
    

    

    

    

    
    
    
    'loanSource': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'loanAmount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    approvalDate: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    firstDeductionDate: new Date(),
    

    

    

    

    

    

    

    
    
    
    'monthlyAmortization': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'remainingBalance': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    termsMonths: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    
    'loanAccountNumber': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit employee_loans')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit employee_loans'} 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="LoanType" labelFor="loanType">
        <Field name="loanType" id="loanType" component="select">
          
            <option value="SSS">SSS</option>
          
            <option value="PAG_IBIG">PAG_IBIG</option>
          
            <option value="COMPANY">COMPANY</option>
          
            <option value="SALARY">SALARY</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="LoanSource"
    >
        <Field
            name="loanSource"
            placeholder="LoanSource"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="LoanAmount"
    >
        <Field
            type="number"
            name="loanAmount"
            placeholder="LoanAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MonthlyAmortization"
    >
        <Field
            type="number"
            name="monthlyAmortization"
            placeholder="MonthlyAmortization"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RemainingBalance"
    >
        <Field
            type="number"
            name="remainingBalance"
            placeholder="RemainingBalance"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TermsMonths"
    >
        <Field
            type="number"
            name="termsMonths"
            placeholder="TermsMonths"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="ACTIVE">ACTIVE</option>
          
            <option value="FULLY_PAID">FULLY_PAID</option>
          
            <option value="CANCELLED">CANCELLED</option>
          
            <option value="DEFERRED">DEFERRED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="LoanAccountNumber"
    >
        <Field
            name="loanAccountNumber"
            placeholder="LoanAccountNumber"
        />
    </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_loans/employee_loans-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditEmployee_loansPage
