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/watchlist_items/watchlist_itemsSlice'
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 EditWatchlist_items = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    watchlist: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    market_symbol: null,
    

    
    
    

    

    

    

    

    
    added_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'alert_above_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'alert_below_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    alerts_enabled: false,
    

    

    

    

    

    

    
    
    

    
    note: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { watchlist_itemsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Watchlist' labelFor='watchlist'>
        <Field
            name='watchlist'
            id='watchlist'
            component={SelectField}
            options={initialValues.watchlist}
            itemRef={'watchlists'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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="AddedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.added_at ?
                  new Date(
                      dayjs(initialValues.added_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'added_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AlertAbovePrice"
    >
        <Field
            type="number"
            name="alert_above_price"
            placeholder="AlertAbovePrice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AlertBelowPrice"
    >
        <Field
            type="number"
            name="alert_below_price"
            placeholder="AlertBelowPrice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AlertsEnabled' labelFor='alerts_enabled'>
        <Field
            name='alerts_enabled'
            id='alerts_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="Note" hasTextareaHeight>
        <Field name="note" as="textarea" placeholder="Note" />
    </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('/watchlist_items/watchlist_items-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditWatchlist_items
