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_nodes/knowledge_nodesSlice'
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_nodesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { knowledge_nodes } = useAppSelector((state) => state.knowledge_nodes)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NodeName</p>
                    <p>{knowledge_nodes?.node_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NodeType</p>
                    <p>{knowledge_nodes?.node_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ImportanceScore</p>
                  <p>{knowledge_nodes?.importance_score || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>SupportingChunks</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_nodes.supporting_chunks && Array.isArray(knowledge_nodes.supporting_chunks) &&
                              knowledge_nodes.supporting_chunks.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_nodes?.supporting_chunks?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Knowledge_nodesView;