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/symbol_fundamentals/symbol_fundamentalsSlice'
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 EditSymbol_fundamentalsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    market_symbol: null,
    

    
    
    
    'pe_ratio': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'eps': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'market_cap': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'revenue': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'dividend_yield': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'beta': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'shares_outstanding': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    as_of_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'source': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="EPS"
    >
        <Field
            type="number"
            name="eps"
            placeholder="EPS"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MarketCap"
    >
        <Field
            type="number"
            name="market_cap"
            placeholder="MarketCap"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Revenue"
    >
        <Field
            type="number"
            name="revenue"
            placeholder="Revenue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DividendYield"
    >
        <Field
            type="number"
            name="dividend_yield"
            placeholder="DividendYield"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Beta"
    >
        <Field
            type="number"
            name="beta"
            placeholder="Beta"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SharesOutstanding"
    >
        <Field
            type="number"
            name="shares_outstanding"
            placeholder="SharesOutstanding"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="Source"
    >
        <Field
            name="source"
            placeholder="Source"
        />
    </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('/symbol_fundamentals/symbol_fundamentals-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditSymbol_fundamentalsPage
