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/exempt_incomes/exempt_incomesSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



const EditExempt_incomesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    declaration: null,
    

    
    
    

    

    

    

    

    

    

    

    
    income_type: '',
    

    

    

    

    
    
    
    'source_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'source_document': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'competence': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

  const { exempt_incomes } = useAppSelector((state) => state.exempt_incomes)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Declaração' labelFor='declaration'>
        <Field
            name='declaration'
            id='declaration'
            component={SelectField}
            options={initialValues.declaration}
            itemRef={'declarations'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'status'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Tipoderendimentoisento" labelFor="income_type">
        <Field name="income_type" id="income_type" component="select">
          
            <option value="dividendos">dividendos</option>
          
            <option value="lci_lca_cri_cra">lci_lca_cri_cra</option>
          
            <option value="poupanca">poupanca</option>
          
            <option value="fgts">fgts</option>
          
            <option value="indenizacao_rescisao">indenizacao_rescisao</option>
          
            <option value="seguro_desemprego">seguro_desemprego</option>
          
            <option value="bolsa_estudo">bolsa_estudo</option>
          
            <option value="aposentadoria_parcela_isenta_65">aposentadoria_parcela_isenta_65</option>
          
            <option value="ganho_acoes_ate_20k_mes">ganho_acoes_ate_20k_mes</option>
          
            <option value="lucro_unico_imovel_ate_440k">lucro_unico_imovel_ate_440k</option>
          
            <option value="outros">outros</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Fonte"
    >
        <Field
            name="source_name"
            placeholder="Fonte"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CPF/CNPJdafonte"
    >
        <Field
            name="source_document"
            placeholder="CPF/CNPJdafonte"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Valor"
    >
        <Field
            type="number"
            name="amount"
            placeholder="Valor"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Competência"
    >
        <Field
            name="competence"
            placeholder="Competência"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditExempt_incomesPage
