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/monthly_capital_gains/monthly_capital_gainsSlice'
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 EditMonthly_capital_gains = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    declaration: null,
    

    
    
    

    

    

    
    year: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    month: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    asset_type: '',
    

    

    

    

    
    
    
    'gross_gain': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'gross_loss': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'carryforward_loss': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'net_gain': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tax_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tax_due': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    darf_paid: false,
    

    

    

    

    

    

    
    
    
    'darf_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { monthly_capital_gainsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit monthly_capital_gains')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit monthly_capital_gains'} 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="Ano"
    >
        <Field
            type="number"
            name="year"
            placeholder="Ano"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Mês"
    >
        <Field
            type="number"
            name="month"
            placeholder="Mês"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Tipodeativo" labelFor="asset_type">
        <Field name="asset_type" id="asset_type" component="select">
          
            <option value="acoes">acoes</option>
          
            <option value="day_trade">day_trade</option>
          
            <option value="fiis">fiis</option>
          
            <option value="etfs">etfs</option>
          
            <option value="bdrs">bdrs</option>
          
            <option value="exterior">exterior</option>
          
            <option value="cripto">cripto</option>
          
            <option value="ganho_capital_bens">ganho_capital_bens</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Ganhobruto"
    >
        <Field
            type="number"
            name="gross_gain"
            placeholder="Ganhobruto"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Prejuízo"
    >
        <Field
            type="number"
            name="gross_loss"
            placeholder="Prejuízo"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Prejuízoacumulado"
    >
        <Field
            type="number"
            name="carryforward_loss"
            placeholder="Prejuízoacumulado"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Ganholíquido"
    >
        <Field
            type="number"
            name="net_gain"
            placeholder="Ganholíquido"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Alíquota"
    >
        <Field
            type="number"
            name="tax_rate"
            placeholder="Alíquota"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Impostodevido"
    >
        <Field
            type="number"
            name="tax_due"
            placeholder="Impostodevido"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='DARFpago' labelFor='darf_paid'>
        <Field
            name='darf_paid'
            id='darf_paid'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  
    <FormField
        label="CódigoDARF"
    >
        <Field
            name="darf_code"
            placeholder="CódigoDARF"
        />
    </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('/monthly_capital_gains/monthly_capital_gains-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMonthly_capital_gains
