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/chapters/chaptersSlice'
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 ChaptersView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { chapters } = useAppSelector((state) => state.chapters)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Textbook</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{chapters?.textbook?.title ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ChapterTitle</p>
                    <p>{chapters?.title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ChapterNumber</p>
                    <p>{chapters?.chapter_number}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SortOrder</p>
                  <p>{chapters?.sort_order || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Questions Chapter</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>QuestionType</th>
                                
                                
                                
                                <th>Difficulty</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>DefaultPoints</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {chapters.questions_chapter && Array.isArray(chapters.questions_chapter) &&
                              chapters.questions_chapter.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/questions/questions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="question_type">
                                        { item.question_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="difficulty">
                                        { item.difficulty }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="default_points">
                                        { item.default_points }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!chapters?.questions_chapter?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Tests Chapter</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>TestTitle</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>BuildMode</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>TargetQuestionCount</th>
                                
                                
                                
                                <th>TargetTotalPoints</th>
                                
                                
                                
                                <th>TargetAverageDifficulty</th>
                                
                                
                                
                                <th>ShuffleQuestions</th>
                                
                                
                                
                                <th>ShuffleAnswers</th>
                                
                                
                                
                                <th>AssignedAt</th>
                                
                                
                                
                                <th>DueAt</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {chapters.tests_chapter && Array.isArray(chapters.tests_chapter) &&
                              chapters.tests_chapter.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/tests/tests-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="build_mode">
                                        { item.build_mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_question_count">
                                        { item.target_question_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_total_points">
                                        { item.target_total_points }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_average_difficulty">
                                        { item.target_average_difficulty }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shuffle_questions">
                                        { dataFormatter.booleanFormatter(item.shuffle_questions) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shuffle_answers">
                                        { dataFormatter.booleanFormatter(item.shuffle_answers) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="assigned_at">
                                        { dataFormatter.dateTimeFormatter(item.assigned_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_at">
                                        { dataFormatter.dateTimeFormatter(item.due_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!chapters?.tests_chapter?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ai_generation_jobs Chapter</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>RequestedQuestionCount</th>
                                
                                
                                
                                <th>DifficultyTarget</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>FinishedAt</th>
                                
                                
                                
                                
                                
                                <th>ErrorMessage</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {chapters.ai_generation_jobs_chapter && Array.isArray(chapters.ai_generation_jobs_chapter) &&
                              chapters.ai_generation_jobs_chapter.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/ai_generation_jobs/ai_generation_jobs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requested_count">
                                        { item.requested_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="difficulty_target">
                                        { item.difficulty_target }
                                    </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="error_message">
                                        { item.error_message }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!chapters?.ai_generation_jobs_chapter?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                

                <BaseDivider />

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

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

export default ChaptersView;