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_chunks/knowledge_chunksSlice'
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_chunksView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { knowledge_chunks } = useAppSelector((state) => state.knowledge_chunks)
    

    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_chunks')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View knowledge_chunks')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/knowledge_chunks/knowledge_chunks-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>KnowledgeDocument</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{knowledge_chunks?.knowledge_document?.document_title ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ChunkLabel</p>
                    <p>{knowledge_chunks?.chunk_label}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SourceLocator</p>
                    <p>{knowledge_chunks?.source_locator}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>RecencyWeight</p>
                  <p>{knowledge_chunks?.recency_weight || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>QualityScore</p>
                  <p>{knowledge_chunks?.quality_score || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsSensitive'>
                    <SwitchField
                      field={{name: 'is_sensitive', value: knowledge_chunks?.is_sensitive}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Output_citations KnowledgeChunk</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>RelevanceScore</th>
                                
                                
                                
                                <th>CitationNote</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {knowledge_chunks.output_citations_knowledge_chunk && Array.isArray(knowledge_chunks.output_citations_knowledge_chunk) &&
                              knowledge_chunks.output_citations_knowledge_chunk.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/output_citations/output_citations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="relevance_score">
                                        { item.relevance_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="citation_note">
                                        { item.citation_note }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!knowledge_chunks?.output_citations_knowledge_chunk?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Knowledge_chunksView;