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/ai_insight_items/ai_insight_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 EditAi_insight_items = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    ai_insight_run: null,
    

    
    
    

    

    

    

    

    

    

    

    
    category: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    severity: '',
    

    

    

    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    detail: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { ai_insight_itemsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='AIInsightRun' labelFor='ai_insight_run'>
        <Field
            name='ai_insight_run'
            id='ai_insight_run'
            component={SelectField}
            options={initialValues.ai_insight_run}
            itemRef={'ai_insight_runs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'output_summary'}
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Category" labelFor="category">
        <Field name="category" id="category" component="select">
          
            <option value="pattern">pattern</option>
          
            <option value="weakness">weakness</option>
          
            <option value="edge">edge</option>
          
            <option value="tip">tip</option>
          
            <option value="alert">alert</option>
          
            <option value="sizing">sizing</option>
          
            <option value="market_condition">market_condition</option>
          
            <option value="summary">summary</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Severity" labelFor="severity">
        <Field name="severity" id="severity" component="select">
          
            <option value="info">info</option>
          
            <option value="low">low</option>
          
            <option value="medium">medium</option>
          
            <option value="high">high</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Title"
    >
        <Field
            name="title"
            placeholder="Title"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Detail" hasTextareaHeight>
        <Field name="detail" as="textarea" placeholder="Detail" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='RelatedTrades' labelFor='related_trades'>
          <Field
              name='related_trades'
              id='related_trades'
              component={SelectFieldMany}
              options={initialValues.related_trades}
              itemRef={'trades'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'notes'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></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('/ai_insight_items/ai_insight_items-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAi_insight_items
