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

    

    

    

    

    

    

    

    

    

    

    
    journalEntry: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    account: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    costCenter: null,
    

    
    
    
    'description': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'debit': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'credit': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    lineOrder: '',
    

    

    

    

    

    

    

    

    

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

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

  const { journal_linesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='JournalEntry' labelFor='journalEntry'>
        <Field
            name='journalEntry'
            id='journalEntry'
            component={SelectField}
            options={initialValues.journalEntry}
            itemRef={'journal_entries'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'entryNumber'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Account' labelFor='account'>
        <Field
            name='account'
            id='account'
            component={SelectField}
            options={initialValues.account}
            itemRef={'accounts'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CostCenter' labelFor='costCenter'>
        <Field
            name='costCenter'
            id='costCenter'
            component={SelectField}
            options={initialValues.costCenter}
            itemRef={'cost_centers'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Description"
    >
        <Field
            name="description"
            placeholder="Description"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Debit"
    >
        <Field
            type="number"
            name="debit"
            placeholder="Debit"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Credit"
    >
        <Field
            type="number"
            name="credit"
            placeholder="Credit"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="LineOrder"
    >
        <Field
            type="number"
            name="lineOrder"
            placeholder="LineOrder"
        />
    </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('/journal_lines/journal_lines-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditJournal_lines
