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/alert_rules/alert_rulesSlice'
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 EditAlert_rulesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    trading_account: null,
    

    
    
    

    

    

    

    

    

    

    

    
    rule_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_enabled: false,
    

    

    

    

    

    

    
    
    
    'threshold_value': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    threshold_unit: '',
    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='TradingAccount' labelFor='trading_account'>
        <Field
            name='trading_account'
            id='trading_account'
            component={SelectField}
            options={initialValues.trading_account}
            itemRef={'trading_accounts'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RuleType" labelFor="rule_type">
        <Field name="rule_type" id="rule_type" component="select">
          
            <option value="overtrading">overtrading</option>
          
            <option value="revenge_trading">revenge_trading</option>
          
            <option value="drawdown">drawdown</option>
          
            <option value="streak_loss">streak_loss</option>
          
            <option value="streak_win">streak_win</option>
          
            <option value="risk_spike">risk_spike</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsEnabled' labelFor='is_enabled'>
        <Field
            name='is_enabled'
            id='is_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="ThresholdValue"
    >
        <Field
            type="number"
            name="threshold_value"
            placeholder="ThresholdValue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ThresholdUnit" labelFor="threshold_unit">
        <Field name="threshold_unit" id="threshold_unit" component="select">
          
            <option value="count">count</option>
          
            <option value="percent">percent</option>
          
            <option value="currency">currency</option>
          
            <option value="rr">rr</option>
          
            <option value="minutes">minutes</option>
          
            <option value="days">days</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

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

export default EditAlert_rulesPage
