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/portfolio_snapshots/portfolio_snapshotsSlice'
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 EditPortfolio_snapshotsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    paper_account: null,
    

    
    
    

    

    

    

    

    
    snapshotted_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'total_value': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'cash_value': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'positions_value': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'day_pl': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'all_time_pl': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'day_pl_percent': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PaperAccount' labelFor='paper_account'>
        <Field
            name='paper_account'
            id='paper_account'
            component={SelectField}
            options={initialValues.paper_account}
            itemRef={'paper_accounts'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalValue"
    >
        <Field
            type="number"
            name="total_value"
            placeholder="TotalValue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CashValue"
    >
        <Field
            type="number"
            name="cash_value"
            placeholder="CashValue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PositionsValue"
    >
        <Field
            type="number"
            name="positions_value"
            placeholder="PositionsValue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DayP&L"
    >
        <Field
            type="number"
            name="day_pl"
            placeholder="DayP&L"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="All-timeP&L"
    >
        <Field
            type="number"
            name="all_time_pl"
            placeholder="All-timeP&L"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DayP&LPercent"
    >
        <Field
            type="number"
            name="day_pl_percent"
            placeholder="DayP&LPercent"
        />
    </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('/portfolio_snapshots/portfolio_snapshots-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditPortfolio_snapshotsPage
