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/generation_outputs/generation_outputsSlice'
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 EditGeneration_outputsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    generation_run: null,
    

    
    
    

    

    

    

    

    

    

    

    
    output_type: '',
    

    

    

    

    
    
    
    'output_title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    output_content: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_exported: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    exported_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='GenerationRun' labelFor='generation_run'>
        <Field
            name='generation_run'
            id='generation_run'
            component={SelectField}
            options={initialValues.generation_run}
            itemRef={'generation_runs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'model_label'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="OutputType" labelFor="output_type">
        <Field name="output_type" id="output_type" component="select">
          
            <option value="ad_copy">ad_copy</option>
          
            <option value="creative_brief">creative_brief</option>
          
            <option value="analysis_report">analysis_report</option>
          
            <option value="compliance_report">compliance_report</option>
          
            <option value="ab_variants">ab_variants</option>
          
            <option value="strategy_plan">strategy_plan</option>
          
            <option value="research_notes">research_notes</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="OutputTitle"
    >
        <Field
            name="output_title"
            placeholder="OutputTitle"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='OutputContent' hasTextareaHeight>
        <Field
            name='output_content'
            id='output_content'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsExported' labelFor='is_exported'>
        <Field
            name='is_exported'
            id='is_exported'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

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

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

export default EditGeneration_outputsPage
