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_runs/ai_insight_runsSlice'
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_runsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    trading_account: null,
    

    
    
    

    

    

    

    

    

    

    

    
    run_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    period_start_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    period_end_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    prompt_snapshot: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    output_summary: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'consistency_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    completed_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit ai_insight_runs')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit ai_insight_runs'} 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="RunType" labelFor="run_type">
        <Field name="run_type" id="run_type" component="select">
          
            <option value="daily_summary">daily_summary</option>
          
            <option value="weekly_summary">weekly_summary</option>
          
            <option value="trade_review">trade_review</option>
          
            <option value="pattern_scan">pattern_scan</option>
          
            <option value="risk_sizing">risk_sizing</option>
          
            <option value="market_condition">market_condition</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="queued">queued</option>
          
            <option value="running">running</option>
          
            <option value="completed">completed</option>
          
            <option value="failed">failed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="PromptSnapshot" hasTextareaHeight>
        <Field name="prompt_snapshot" as="textarea" placeholder="PromptSnapshot" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="OutputSummary" hasTextareaHeight>
        <Field name="output_summary" as="textarea" placeholder="OutputSummary" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ConsistencyScore"
    >
        <Field
            type="number"
            name="consistency_score"
            placeholder="ConsistencyScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="CompletedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.completed_at ?
                  new Date(
                      dayjs(initialValues.completed_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'completed_at': date})}
          />
      </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_runs/ai_insight_runs-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAi_insight_runsPage
