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/ir_financial_statement_summaries/ir_financial_statement_summariesSlice'
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 EditIr_financial_statement_summaries = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    ir_company: null,
    

    
    
    

    

    

    

    

    

    

    

    
    statement_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    period_type: '',
    

    

    

    

    
    
    

    

    

    

    

    
    period_end_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'total_revenue': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'net_income': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'total_assets': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'total_liabilities': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'free_cash_flow': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { ir_financial_statement_summariesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='IRCompany' labelFor='ir_company'>
        <Field
            name='ir_company'
            id='ir_company'
            component={SelectField}
            options={initialValues.ir_company}
            itemRef={'ir_companies'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="StatementType" labelFor="statement_type">
        <Field name="statement_type" id="statement_type" component="select">
          
            <option value="income_statement">income_statement</option>
          
            <option value="balance_sheet">balance_sheet</option>
          
            <option value="cash_flow">cash_flow</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PeriodType" labelFor="period_type">
        <Field name="period_type" id="period_type" component="select">
          
            <option value="annual">annual</option>
          
            <option value="quarterly">quarterly</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalRevenue"
    >
        <Field
            type="number"
            name="total_revenue"
            placeholder="TotalRevenue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="NetIncome"
    >
        <Field
            type="number"
            name="net_income"
            placeholder="NetIncome"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalAssets"
    >
        <Field
            type="number"
            name="total_assets"
            placeholder="TotalAssets"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalLiabilities"
    >
        <Field
            type="number"
            name="total_liabilities"
            placeholder="TotalLiabilities"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="FreeCashFlow"
    >
        <Field
            type="number"
            name="free_cash_flow"
            placeholder="FreeCashFlow"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditIr_financial_statement_summaries
