import React, { ReactElement, useEffect } from 'react';
import Head from 'next/head'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";
import {useAppDispatch, useAppSelector} from "../../stores/hooks";
import {useRouter} from "next/router";
import { fetch } from '../../stores/knowledge_documents/knowledge_documentsSlice'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";
import LayoutAuthenticated from "../../layouts/Authenticated";
import {getPageTitle} from "../../config";
import SectionTitleLineWithButton from "../../components/SectionTitleLineWithButton";
import SectionMain from "../../components/SectionMain";
import CardBox from "../../components/CardBox";
import BaseButton from "../../components/BaseButton";
import BaseDivider from "../../components/BaseDivider";
import {mdiChartTimelineVariant} from "@mdi/js";
import {SwitchField} from "../../components/SwitchField";
import FormField from "../../components/FormField";


const Knowledge_documentsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { knowledge_documents } = useAppSelector((state) => state.knowledge_documents)
    

    const { id } = router.query;
    
    function removeLastCharacter(str) {
      console.log(str,`str`)
      return str.slice(0, -1);
    }

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


    return (
      <>
          <Head>
              <title>{getPageTitle('View knowledge_documents')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View knowledge_documents')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/knowledge_documents/knowledge_documents-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>KnowledgeSource</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{knowledge_documents?.knowledge_source?.source_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DocumentTitle</p>
                    <p>{knowledge_documents?.document_title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>IngestionStatus</p>
                    <p>{knowledge_documents?.ingestion_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Language</p>
                    <p>{knowledge_documents?.language}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>DocumentSummary</p>
                  {knowledge_documents.document_summary
                    ? <p dangerouslySetInnerHTML={{__html: knowledge_documents.document_summary}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TagsCsv</p>
                    <p>{knowledge_documents?.tags_csv}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Knowledge_chunks KnowledgeDocument</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ChunkLabel</th>
                                
                                
                                
                                
                                
                                <th>SourceLocator</th>
                                
                                
                                
                                <th>RecencyWeight</th>
                                
                                
                                
                                <th>QualityScore</th>
                                
                                
                                
                                <th>IsSensitive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {knowledge_documents.knowledge_chunks_knowledge_document && Array.isArray(knowledge_documents.knowledge_chunks_knowledge_document) &&
                              knowledge_documents.knowledge_chunks_knowledge_document.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/knowledge_chunks/knowledge_chunks-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="chunk_label">
                                        { item.chunk_label }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="source_locator">
                                        { item.source_locator }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="recency_weight">
                                        { item.recency_weight }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quality_score">
                                        { item.quality_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_sensitive">
                                        { dataFormatter.booleanFormatter(item.is_sensitive) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!knowledge_documents?.knowledge_chunks_knowledge_document?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

                <BaseButton
                    color='info'
                    label='Back'
                    onClick={() => router.push('/knowledge_documents/knowledge_documents-list')}
                />
              </CardBox>
          </SectionMain>
      </>
    );
};

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

export default Knowledge_documentsView;