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/earnings_reports/earnings_reportsSlice'
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 EditEarnings_reports = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    market_symbol: null,
    

    
    
    

    

    

    

    

    
    period_end_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    reported_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'reported_eps': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'estimated_eps': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'surprise': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'surprise_percent': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'reported_revenue': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'estimated_revenue': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { earnings_reportsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='MarketSymbol' labelFor='market_symbol'>
        <Field
            name='market_symbol'
            id='market_symbol'
            component={SelectField}
            options={initialValues.market_symbol}
            itemRef={'market_symbols'}
      
        
      
        
      
        
      
        
      
        
            showField={'symbol'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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="ReportedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.reported_at ?
                  new Date(
                      dayjs(initialValues.reported_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'reported_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ReportedEPS"
    >
        <Field
            type="number"
            name="reported_eps"
            placeholder="ReportedEPS"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="EstimatedEPS"
    >
        <Field
            type="number"
            name="estimated_eps"
            placeholder="EstimatedEPS"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Surprise"
    >
        <Field
            type="number"
            name="surprise"
            placeholder="Surprise"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SurprisePercent"
    >
        <Field
            type="number"
            name="surprise_percent"
            placeholder="SurprisePercent"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ReportedRevenue"
    >
        <Field
            type="number"
            name="reported_revenue"
            placeholder="ReportedRevenue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

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

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

export default EditEarnings_reports
