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/topics/topicsSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";


const TopicsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { topics } = useAppSelector((state) => state.topics)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Subject</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{topics?.subject?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TopicName</p>
                    <p>{topics?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsArchived'>
                    <SwitchField
                      field={{name: 'is_archived', value: topics?.is_archived}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>organizations</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{topics?.organizations?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Pages Topic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>IconEmoji</th>
                                
                                
                                
                                
                                
                                <th>Visibility</th>
                                
                                
                                
                                <th>IsPinned</th>
                                
                                
                                
                                <th>IsArchived</th>
                                
                                
                                
                                <th>LastEditedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {topics.pages_topic && Array.isArray(topics.pages_topic) &&
                              topics.pages_topic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/pages/pages-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="icon_emoji">
                                        { item.icon_emoji }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="visibility">
                                        { item.visibility }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_pinned">
                                        { dataFormatter.booleanFormatter(item.is_pinned) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_archived">
                                        { dataFormatter.booleanFormatter(item.is_archived) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_edited_at">
                                        { dataFormatter.dateTimeFormatter(item.last_edited_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!topics?.pages_topic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Database_entries Topic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                
                                
                                <th>ContentType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>StudiedAt</th>
                                
                                
                                
                                <th>NextReviewAt</th>
                                
                                
                                
                                <th>EstimatedRetention</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {topics.database_entries_topic && Array.isArray(topics.database_entries_topic) &&
                              topics.database_entries_topic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/database_entries/database_entries-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="content_type">
                                        { item.content_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="studied_at">
                                        { dataFormatter.dateTimeFormatter(item.studied_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="next_review_at">
                                        { dataFormatter.dateTimeFormatter(item.next_review_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="estimated_retention">
                                        { item.estimated_retention }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!topics?.database_entries_topic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Sources Topic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>SourceType</th>
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>SourceURL</th>
                                
                                
                                
                                
                                
                                <th>IngestionStatus</th>
                                
                                
                                
                                <th>ErrorMessage</th>
                                
                                
                                
                                <th>IngestedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {topics.sources_topic && Array.isArray(topics.sources_topic) &&
                              topics.sources_topic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/sources/sources-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="source_type">
                                        { item.source_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="source_url">
                                        { item.source_url }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="ingestion_status">
                                        { item.ingestion_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_message">
                                        { item.error_message }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ingested_at">
                                        { dataFormatter.dateTimeFormatter(item.ingested_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!topics?.sources_topic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Flashcard_decks Topic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>DeckName</th>
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                <th>DeckVisibility</th>
                                
                                
                                
                                <th>GenerationMode</th>
                                
                                
                                
                                <th>IsArchived</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {topics.flashcard_decks_topic && Array.isArray(topics.flashcard_decks_topic) &&
                              topics.flashcard_decks_topic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/flashcard_decks/flashcard_decks-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="deck_visibility">
                                        { item.deck_visibility }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="generation_mode">
                                        { item.generation_mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_archived">
                                        { dataFormatter.booleanFormatter(item.is_archived) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!topics?.flashcard_decks_topic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Study_sessions Topic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Mode</th>
                                
                                
                                
                                <th>SessionType</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>EndedAt</th>
                                
                                
                                
                                <th>DurationSeconds</th>
                                
                                
                                
                                <th>DistractionBlockEnabled</th>
                                
                                
                                
                                <th>MusicMode</th>
                                
                                
                                
                                <th>FocusScore</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {topics.study_sessions_topic && Array.isArray(topics.study_sessions_topic) &&
                              topics.study_sessions_topic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/study_sessions/study_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="mode">
                                        { item.mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="session_type">
                                        { item.session_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ended_at">
                                        { dataFormatter.dateTimeFormatter(item.ended_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="duration_seconds">
                                        { item.duration_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="distraction_block_enabled">
                                        { dataFormatter.booleanFormatter(item.distraction_block_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="music_mode">
                                        { item.music_mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="focus_score">
                                        { item.focus_score }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!topics?.study_sessions_topic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quizzes Topic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>QuizType</th>
                                
                                
                                
                                <th>DifficultyLevel</th>
                                
                                
                                
                                <th>TimeLimitMinutes</th>
                                
                                
                                
                                <th>IsPublished</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {topics.quizzes_topic && Array.isArray(topics.quizzes_topic) &&
                              topics.quizzes_topic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quizzes/quizzes-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quiz_type">
                                        { item.quiz_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="difficulty_level">
                                        { item.difficulty_level }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="time_limit_minutes">
                                        { item.time_limit_minutes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_published">
                                        { dataFormatter.booleanFormatter(item.is_published) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!topics?.quizzes_topic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Tutor_conversations Topic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>TutorMode</th>
                                
                                
                                
                                <th>IsArchived</th>
                                
                                
                                
                                <th>LastMessageAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {topics.tutor_conversations_topic && Array.isArray(topics.tutor_conversations_topic) &&
                              topics.tutor_conversations_topic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/tutor_conversations/tutor_conversations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tutor_mode">
                                        { item.tutor_mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_archived">
                                        { dataFormatter.booleanFormatter(item.is_archived) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_message_at">
                                        { dataFormatter.dateTimeFormatter(item.last_message_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!topics?.tutor_conversations_topic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Study_plan_items Topic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>ItemType</th>
                                
                                
                                
                                <th>ScheduledStartAt</th>
                                
                                
                                
                                <th>ScheduledEndAt</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>EstimatedMinutes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {topics.study_plan_items_topic && Array.isArray(topics.study_plan_items_topic) &&
                              topics.study_plan_items_topic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/study_plan_items/study_plan_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="item_type">
                                        { item.item_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_start_at">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_end_at">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="estimated_minutes">
                                        { item.estimated_minutes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!topics?.study_plan_items_topic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

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

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

export default TopicsView;