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/screener_filters/screener_filtersSlice'
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 EditScreener_filters = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    asset_class: '',
    

    

    

    

    
    
    
    'sector': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'market_cap_min': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'market_cap_max': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'pe_min': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'pe_max': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'price_min': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'price_max': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'volume_min': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'performance_52w_min': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'performance_52w_max': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_pinned: false,
    

    

    

    

    

    

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

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

  const { screener_filtersId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='User' labelFor='user'>
        <Field
            name='user'
            id='user'
            component={SelectField}
            options={initialValues.user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </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="stock">stock</option>
          
            <option value="etf">etf</option>
          
            <option value="crypto">crypto</option>
          
            <option value="forex">forex</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Sector"
    >
        <Field
            name="sector"
            placeholder="Sector"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MarketCapMin"
    >
        <Field
            type="number"
            name="market_cap_min"
            placeholder="MarketCapMin"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MarketCapMax"
    >
        <Field
            type="number"
            name="market_cap_max"
            placeholder="MarketCapMax"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="P/EMin"
    >
        <Field
            type="number"
            name="pe_min"
            placeholder="P/EMin"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="P/EMax"
    >
        <Field
            type="number"
            name="pe_max"
            placeholder="P/EMax"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PriceMin"
    >
        <Field
            type="number"
            name="price_min"
            placeholder="PriceMin"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PriceMax"
    >
        <Field
            type="number"
            name="price_max"
            placeholder="PriceMax"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="VolumeMin"
    >
        <Field
            type="number"
            name="volume_min"
            placeholder="VolumeMin"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="52-WeekPerformanceMin"
    >
        <Field
            type="number"
            name="performance_52w_min"
            placeholder="52-WeekPerformanceMin"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="52-WeekPerformanceMax"
    >
        <Field
            type="number"
            name="performance_52w_max"
            placeholder="52-WeekPerformanceMax"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsPinned' labelFor='is_pinned'>
        <Field
            name='is_pinned'
            id='is_pinned'
            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('/screener_filters/screener_filters-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditScreener_filters
