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/price_quotes/price_quotesSlice'
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 EditPrice_quotesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    market_symbol: null,
    

    
    
    

    

    

    

    

    
    quoted_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'open': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'high': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'low': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'previous_close': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'change_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'change_percent': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'volume': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    timeframe: '',
    

    

    

    

    
    
    
    'source': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Price"
    >
        <Field
            type="number"
            name="price"
            placeholder="Price"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Open"
    >
        <Field
            type="number"
            name="open"
            placeholder="Open"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="High"
    >
        <Field
            type="number"
            name="high"
            placeholder="High"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Low"
    >
        <Field
            type="number"
            name="low"
            placeholder="Low"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PreviousClose"
    >
        <Field
            type="number"
            name="previous_close"
            placeholder="PreviousClose"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ChangeAmount"
    >
        <Field
            type="number"
            name="change_amount"
            placeholder="ChangeAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ChangePercent"
    >
        <Field
            type="number"
            name="change_percent"
            placeholder="ChangePercent"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

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

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

export default EditPrice_quotesPage
