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_sources/knowledge_sourcesSlice'
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_sourcesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    
    source_type: '',
    

    

    

    

    
    
    
    'source_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'source_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    uploaded_files: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    
    trust_tier: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    content_domain: '',
    

    

    

    

    
    
    

    

    

    

    

    
    last_ingested_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    next_refresh_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    auto_refresh_enabled: false,
    

    

    

    

    

    

    
    
    

    
    refresh_notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  
    <FormField label="SourceType" labelFor="source_type">
        <Field name="source_type" id="source_type" component="select">
          
            <option value="website">website</option>
          
            <option value="document">document</option>
          
            <option value="video">video</option>
          
            <option value="audio">audio</option>
          
            <option value="note">note</option>
          
            <option value="dataset">dataset</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="SourceName"
    >
        <Field
            name="source_name"
            placeholder="SourceName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SourceURL"
    >
        <Field
            name="source_url"
            placeholder="SourceURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='UploadedFiles'
            color='info'
            icon={mdiUpload}
            path={'knowledge_sources/uploaded_files'}
            name='uploaded_files'
            id='uploaded_files'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  

  

  

  

  
    <FormField label="TrustTier" labelFor="trust_tier">
        <Field name="trust_tier" id="trust_tier" component="select">
          
            <option value="gold">gold</option>
          
            <option value="silver">silver</option>
          
            <option value="bronze">bronze</option>
          
            <option value="blocked">blocked</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ContentDomain" labelFor="content_domain">
        <Field name="content_domain" id="content_domain" component="select">
          
            <option value="ndis">ndis</option>
          
            <option value="compliance">compliance</option>
          
            <option value="behavioral_science">behavioral_science</option>
          
            <option value="platform_policy">platform_policy</option>
          
            <option value="competitor_research">competitor_research</option>
          
            <option value="brand_assets">brand_assets</option>
          
            <option value="campaign_reports">campaign_reports</option>
          
            <option value="general_marketing">general_marketing</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AutoRefreshEnabled' labelFor='auto_refresh_enabled'>
        <Field
            name='auto_refresh_enabled'
            id='auto_refresh_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="RefreshNotes" hasTextareaHeight>
        <Field name="refresh_notes" as="textarea" placeholder="RefreshNotes" />
    </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_sources/knowledge_sources-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditKnowledge_sourcesPage
