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/instruments/instrumentsSlice'
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 EditInstruments = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'symbol': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    asset_class: '',
    

    

    

    

    
    
    
    'exchange': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tick_size': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'pip_value_hint': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_active: false,
    

    

    

    

    

    

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

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

  const { instrumentsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="Symbol"
    >
        <Field
            name="symbol"
            placeholder="Symbol"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Name"
    >
        <Field
            name="name"
            placeholder="Name"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="AssetClass" labelFor="asset_class">
        <Field name="asset_class" id="asset_class" component="select">
          
            <option value="forex">forex</option>
          
            <option value="crypto">crypto</option>
          
            <option value="stocks">stocks</option>
          
            <option value="options">options</option>
          
            <option value="futures">futures</option>
          
            <option value="indices">indices</option>
          
            <option value="commodities">commodities</option>
          
            <option value="bonds">bonds</option>
          
            <option value="etf">etf</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Exchange"
    >
        <Field
            name="exchange"
            placeholder="Exchange"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TickSize"
    >
        <Field
            name="tick_size"
            placeholder="TickSize"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="PipValueHint"
    >
        <Field
            name="pip_value_hint"
            placeholder="PipValueHint"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsActive' labelFor='is_active'>
        <Field
            name='is_active'
            id='is_active'
            component={SwitchField}
        ></Field>
    </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('/instruments/instruments-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditInstruments
