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_sources/knowledge_sourcesSlice'
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_sourcesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { knowledge_sources } = useAppSelector((state) => state.knowledge_sources)
    

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

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SourceType</p>
                    <p>{knowledge_sources?.source_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SourceName</p>
                    <p>{knowledge_sources?.source_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SourceURL</p>
                    <p>{knowledge_sources?.source_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>UploadedFiles</p>
                    {knowledge_sources?.uploaded_files?.length
                      ? dataFormatter.filesFormatter(knowledge_sources.uploaded_files).map(link => (
                        <button
                          key={link.publicUrl}
                          onClick={(e) => saveFile(e, link.publicUrl, link.name)}
                        >
                            {link.name}
                        </button>
                      )) : <p>No UploadedFiles</p>
                    }
                </div>
              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TrustTier</p>
                    <p>{knowledge_sources?.trust_tier ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ContentDomain</p>
                    <p>{knowledge_sources?.content_domain ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='AutoRefreshEnabled'>
                    <SwitchField
                      field={{name: 'auto_refresh_enabled', value: knowledge_sources?.auto_refresh_enabled}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={knowledge_sources?.refresh_notes} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Knowledge_documents KnowledgeSource</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>DocumentTitle</th>
                                
                                
                                
                                <th>IngestionStatus</th>
                                
                                
                                
                                <th>Language</th>
                                
                                
                                
                                <th>PublishedAt</th>
                                
                                
                                
                                <th>IngestedAt</th>
                                
                                
                                
                                
                                
                                <th>TagsCsv</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {knowledge_sources.knowledge_documents_knowledge_source && Array.isArray(knowledge_sources.knowledge_documents_knowledge_source) &&
                              knowledge_sources.knowledge_documents_knowledge_source.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/knowledge_documents/knowledge_documents-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="document_title">
                                        { item.document_title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ingestion_status">
                                        { item.ingestion_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="language">
                                        { item.language }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="published_at">
                                        { dataFormatter.dateTimeFormatter(item.published_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ingested_at">
                                        { dataFormatter.dateTimeFormatter(item.ingested_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="tags_csv">
                                        { item.tags_csv }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!knowledge_sources?.knowledge_documents_knowledge_source?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Refresh_jobs KnowledgeSource</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>JobType</th>
                                
                                
                                
                                <th>JobStatus</th>
                                
                                
                                
                                <th>ScheduledFor</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>FinishedAt</th>
                                
                                
                                
                                <th>JobSummary</th>
                                
                                
                                
                                <th>JobLog</th>
                                
                                
                                
                                <th>ApprovalRequired</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {knowledge_sources.refresh_jobs_knowledge_source && Array.isArray(knowledge_sources.refresh_jobs_knowledge_source) &&
                              knowledge_sources.refresh_jobs_knowledge_source.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/refresh_jobs/refresh_jobs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="job_type">
                                        { item.job_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="job_status">
                                        { item.job_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_for">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_for) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="finished_at">
                                        { dataFormatter.dateTimeFormatter(item.finished_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="job_summary">
                                        { item.job_summary }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="job_log">
                                        { item.job_log }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="approval_required">
                                        { dataFormatter.booleanFormatter(item.approval_required) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!knowledge_sources?.refresh_jobs_knowledge_source?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

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

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

export default Knowledge_sourcesView;