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/screener_results/screener_resultsSlice'
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 EditScreener_results = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    screener_run: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    market_symbol: null,
    

    
    
    
    'last_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'market_cap': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'pe_ratio': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'volume': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'performance_52w': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'sector': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { screener_resultsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ScreenerRun' labelFor='screener_run'>
        <Field
            name='screener_run'
            id='screener_run'
            component={SelectField}
            options={initialValues.screener_run}
            itemRef={'screener_runs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'status'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <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="LastPrice"
    >
        <Field
            type="number"
            name="last_price"
            placeholder="LastPrice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="P/ERatio"
    >
        <Field
            type="number"
            name="pe_ratio"
            placeholder="P/ERatio"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Volume"
    >
        <Field
            type="number"
            name="volume"
            placeholder="Volume"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="52-WeekPerformance"
    >
        <Field
            type="number"
            name="performance_52w"
            placeholder="52-WeekPerformance"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



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

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

export default EditScreener_results
