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/deductible_payments/deductible_paymentsSlice'
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 EditDeductible_payments = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    declaration: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    dependent: null,
    

    
    
    
    'payment_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'beneficiary_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'beneficiary_document': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'amount_paid': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { deductible_paymentsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit deductible_payments')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit deductible_payments'} 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='Dependente' labelFor='dependent'>
        <Field
            name='dependent'
            id='dependent'
            component={SelectField}
            options={initialValues.dependent}
            itemRef={'dependents'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Código"
    >
        <Field
            name="payment_code"
            placeholder="Código"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Beneficiário"
    >
        <Field
            name="beneficiary_name"
            placeholder="Beneficiário"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CPF/CNPJdobeneficiário"
    >
        <Field
            name="beneficiary_document"
            placeholder="CPF/CNPJdobeneficiário"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Valorpago"
    >
        <Field
            type="number"
            name="amount_paid"
            placeholder="Valorpago"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Observações" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Observações" />
    </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('/deductible_payments/deductible_payments-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditDeductible_payments
