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/textbooks/textbooksSlice'
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 TextbooksView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { textbooks } = useAppSelector((state) => state.textbooks)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Publisher</p>
                    <p>{textbooks?.publisher}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Edition</p>
                    <p>{textbooks?.edition}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ISBN</p>
                    <p>{textbooks?.isbn}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

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

                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Chapters Textbook</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ChapterTitle</th>
                                
                                
                                
                                <th>ChapterNumber</th>
                                
                                
                                
                                <th>SortOrder</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {textbooks.chapters_textbook && Array.isArray(textbooks.chapters_textbook) &&
                              textbooks.chapters_textbook.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/chapters/chapters-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="chapter_number">
                                        { item.chapter_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sort_order">
                                        { item.sort_order }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!textbooks?.chapters_textbook?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Question_banks Textbook</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>BankName</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>IsPublic</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {textbooks.question_banks_textbook && Array.isArray(textbooks.question_banks_textbook) &&
                              textbooks.question_banks_textbook.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/question_banks/question_banks-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_public">
                                        { dataFormatter.booleanFormatter(item.is_public) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!textbooks?.question_banks_textbook?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Tests Textbook</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>
                            {textbooks.tests_textbook && Array.isArray(textbooks.tests_textbook) &&
                              textbooks.tests_textbook.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>
                        {!textbooks?.tests_textbook?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default TextbooksView;