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/lessons/lessonsSlice'
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 LessonsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { lessons } = useAppSelector((state) => state.lessons)
    
    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 lessons')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View lessons')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/lessons/lessons-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>LessonNumber</p>
                  <p>{lessons?.lesson_number || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>EstimatedMinutes</p>
                  <p>{lessons?.estimated_minutes || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{lessons?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='HasQuiz'>
                    <SwitchField
                      field={{name: 'has_quiz', value: lessons?.has_quiz}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='HasBuildTask'>
                    <SwitchField
                      field={{name: 'has_build_task', value: lessons?.has_build_task}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>SkillNodesAwarded</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>SkillNodeName</th>
                                
                                
                                
                                    <th>Description</th>
                                
                                
                                
                                    <th>Category</th>
                                
                                
                                
                                    <th>NodeIndex</th>
                                
                                
                                
                                    <th>RequiredForCertification</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {lessons.skill_nodes_awarded && Array.isArray(lessons.skill_nodes_awarded) &&
                              lessons.skill_nodes_awarded.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/skill_nodes/skill_nodes-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="category">
                                        { item.category }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="node_index">
                                        { item.node_index }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_required_for_certification">
                                        { dataFormatter.booleanFormatter(item.is_required_for_certification) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!lessons?.skill_nodes_awarded?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Lesson_variants Lesson</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>FunctionalRole</th>
                                
                                
                                
                                
                                
                                <th>AdaptationNotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {lessons.lesson_variants_lesson && Array.isArray(lessons.lesson_variants_lesson) &&
                              lessons.lesson_variants_lesson.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/lesson_variants/lesson_variants-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="functional_role">
                                        { item.functional_role }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="adaptation_notes">
                                        { item.adaptation_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!lessons?.lesson_variants_lesson?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quiz_questions Lesson</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Prompt</th>
                                
                                
                                
                                <th>QuestionType</th>
                                
                                
                                
                                <th>ChoicesJSON</th>
                                
                                
                                
                                <th>CorrectAnswer</th>
                                
                                
                                
                                <th>Explanation</th>
                                
                                
                                
                                <th>OrderIndex</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {lessons.quiz_questions_lesson && Array.isArray(lessons.quiz_questions_lesson) &&
                              lessons.quiz_questions_lesson.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quiz_questions/quiz_questions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="prompt">
                                        { item.prompt }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="question_type">
                                        { item.question_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="choices_json">
                                        { item.choices_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="correct_answer">
                                        { item.correct_answer }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="explanation">
                                        { item.explanation }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="order_index">
                                        { item.order_index }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!lessons?.quiz_questions_lesson?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Build_tasks Lesson</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>TaskName</th>
                                
                                
                                
                                
                                
                                <th>SubmissionType</th>
                                
                                
                                
                                <th>ExpectedDeliverable</th>
                                
                                
                                
                                <th>MaxFiles</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {lessons.build_tasks_lesson && Array.isArray(lessons.build_tasks_lesson) &&
                              lessons.build_tasks_lesson.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/build_tasks/build_tasks-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="submission_type">
                                        { item.submission_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expected_deliverable">
                                        { item.expected_deliverable }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_files">
                                        { item.max_files }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!lessons?.build_tasks_lesson?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Lesson_progress Lesson</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>StartedAt</th>
                                
                                
                                
                                <th>CompletedAt</th>
                                
                                
                                
                                <th>TimeSpentSeconds</th>
                                
                                
                                
                                <th>LessonProgressPercent</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {lessons.lesson_progress_lesson && Array.isArray(lessons.lesson_progress_lesson) &&
                              lessons.lesson_progress_lesson.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/lesson_progress/lesson_progress-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completed_at">
                                        { dataFormatter.dateTimeFormatter(item.completed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="time_spent_seconds">
                                        { item.time_spent_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lesson_progress_percent">
                                        { item.lesson_progress_percent }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!lessons?.lesson_progress_lesson?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quiz_attempts Lesson</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>ScorePercent</th>
                                
                                
                                
                                <th>CorrectCount</th>
                                
                                
                                
                                <th>QuestionCount</th>
                                
                                
                                
                                <th>SubmittedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {lessons.quiz_attempts_lesson && Array.isArray(lessons.quiz_attempts_lesson) &&
                              lessons.quiz_attempts_lesson.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quiz_attempts/quiz_attempts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="score_percent">
                                        { item.score_percent }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="correct_count">
                                        { item.correct_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="question_count">
                                        { item.question_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="submitted_at">
                                        { dataFormatter.dateTimeFormatter(item.submitted_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!lessons?.quiz_attempts_lesson?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ai_chat_sessions Lesson</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>Status</th>
                                
                                
                                
                                <th>ContextSummary</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>EndedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {lessons.ai_chat_sessions_lesson && Array.isArray(lessons.ai_chat_sessions_lesson) &&
                              lessons.ai_chat_sessions_lesson.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/ai_chat_sessions/ai_chat_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="mode">
                                        { item.mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="context_summary">
                                        { item.context_summary }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ended_at">
                                        { dataFormatter.dateTimeFormatter(item.ended_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!lessons?.ai_chat_sessions_lesson?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default LessonsView;