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/knowledge_documents/knowledge_documentsSlice'
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 EditKnowledge_documentsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    knowledge_source: null,
    

    
    
    
    'document_title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    ingestion_status: '',
    

    

    

    

    
    
    
    'language': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    published_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    ingested_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    
    document_summary: '',
    

    

    

    

    

    

    

    

    

    

    
    
    
    'tags_csv': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='KnowledgeSource' labelFor='knowledge_source'>
        <Field
            name='knowledge_source'
            id='knowledge_source'
            component={SelectField}
            options={initialValues.knowledge_source}
            itemRef={'knowledge_sources'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'source_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="DocumentTitle"
    >
        <Field
            name="document_title"
            placeholder="DocumentTitle"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="IngestionStatus" labelFor="ingestion_status">
        <Field name="ingestion_status" id="ingestion_status" component="select">
          
            <option value="queued">queued</option>
          
            <option value="processing">processing</option>
          
            <option value="completed">completed</option>
          
            <option value="failed">failed</option>
          
            <option value="needs_review">needs_review</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Language"
    >
        <Field
            name="language"
            placeholder="Language"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  
    <FormField label='DocumentSummary' hasTextareaHeight>
        <Field
            name='document_summary'
            id='document_summary'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TagsCsv"
    >
        <Field
            name="tags_csv"
            placeholder="TagsCsv"
        />
    </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('/knowledge_documents/knowledge_documents-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditKnowledge_documentsPage
