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/technical_indicators/technical_indicatorsSlice'
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 EditTechnical_indicatorsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    market_symbol: null,
    

    
    
    

    

    

    

    

    
    calculated_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    indicator_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    timeframe: '',
    

    

    

    

    
    
    

    

    

    
    period: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'value': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'signal': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'histogram': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'upper_band': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'middle_band': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'lower_band': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'source': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit technical_indicators')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit technical_indicators'} 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="CalculatedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.calculated_at ?
                  new Date(
                      dayjs(initialValues.calculated_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'calculated_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="IndicatorType" labelFor="indicator_type">
        <Field name="indicator_type" id="indicator_type" component="select">
          
            <option value="rsi">rsi</option>
          
            <option value="macd">macd</option>
          
            <option value="bollinger_bands">bollinger_bands</option>
          
            <option value="sma">sma</option>
          
            <option value="ema">ema</option>
          
            <option value="volume">volume</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Timeframe" labelFor="timeframe">
        <Field name="timeframe" id="timeframe" component="select">
          
            <option value="1min">1min</option>
          
            <option value="5min">5min</option>
          
            <option value="15min">15min</option>
          
            <option value="30min">30min</option>
          
            <option value="60min">60min</option>
          
            <option value="daily">daily</option>
          
            <option value="weekly">weekly</option>
          
            <option value="monthly">monthly</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Period"
    >
        <Field
            type="number"
            name="period"
            placeholder="Period"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Value"
    >
        <Field
            type="number"
            name="value"
            placeholder="Value"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Signal"
    >
        <Field
            type="number"
            name="signal"
            placeholder="Signal"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Histogram"
    >
        <Field
            type="number"
            name="histogram"
            placeholder="Histogram"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="UpperBand"
    >
        <Field
            type="number"
            name="upper_band"
            placeholder="UpperBand"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MiddleBand"
    >
        <Field
            type="number"
            name="middle_band"
            placeholder="MiddleBand"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

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

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

export default EditTechnical_indicatorsPage
