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/subjects/subjectsSlice'
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 SubjectsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { subjects } = useAppSelector((state) => state.subjects)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SubjectNameAfaanOromoo</p>
                    <p>{subjects?.name_om}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SubjectNameAmharic</p>
                    <p>{subjects?.name_am}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SubjectNameEnglish</p>
                    <p>{subjects?.name_en}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Active'>
                    <SwitchField
                      field={{name: 'active', value: subjects?.active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Subject_offerings Subject</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>TopicsOutline</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {subjects.subject_offerings_subject && Array.isArray(subjects.subject_offerings_subject) &&
                              subjects.subject_offerings_subject.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/subject_offerings/subject_offerings-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="topics_outline">
                                        { item.topics_outline }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!subjects?.subject_offerings_subject?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Study_materials Subject</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>TitleAfaanOromoo</th>
                                
                                
                                
                                <th>TitleAmharic</th>
                                
                                
                                
                                <th>TitleEnglish</th>
                                
                                
                                
                                <th>MaterialType</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>ExternalURL</th>
                                
                                
                                
                                <th>Visibility</th>
                                
                                
                                
                                
                                
                                <th>PublishedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {subjects.study_materials_subject && Array.isArray(subjects.study_materials_subject) &&
                              subjects.study_materials_subject.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/study_materials/study_materials-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="title_om">
                                        { item.title_om }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title_am">
                                        { item.title_am }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title_en">
                                        { item.title_en }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="material_type">
                                        { item.material_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="external_url">
                                        { item.external_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="visibility">
                                        { item.visibility }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="published_at">
                                        { dataFormatter.dateTimeFormatter(item.published_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!subjects?.study_materials_subject?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Exam_results Subject</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Score</th>
                                
                                
                                
                                <th>OutOf</th>
                                
                                
                                
                                <th>ResultStatus</th>
                                
                                
                                
                                
                                
                                <th>EnteredAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {subjects.exam_results_subject && Array.isArray(subjects.exam_results_subject) &&
                              subjects.exam_results_subject.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/exam_results/exam_results-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="score">
                                        { item.score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="out_of">
                                        { item.out_of }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="result_status">
                                        { item.result_status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="entered_at">
                                        { dataFormatter.dateTimeFormatter(item.entered_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!subjects?.exam_results_subject?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Attendance_sessions Subject</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>SessionAt</th>
                                
                                
                                
                                <th>SessionType</th>
                                
                                
                                
                                <th>Note</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {subjects.attendance_sessions_subject && Array.isArray(subjects.attendance_sessions_subject) &&
                              subjects.attendance_sessions_subject.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/attendance_sessions/attendance_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="session_at">
                                        { dataFormatter.dateTimeFormatter(item.session_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="session_type">
                                        { item.session_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="note">
                                        { item.note }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!subjects?.attendance_sessions_subject?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Class_schedules Subject</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Weekday</th>
                                
                                
                                
                                <th>PeriodLabel</th>
                                
                                
                                
                                <th>StartsAt</th>
                                
                                
                                
                                <th>EndsAt</th>
                                
                                
                                
                                <th>Room</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {subjects.class_schedules_subject && Array.isArray(subjects.class_schedules_subject) &&
                              subjects.class_schedules_subject.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/class_schedules/class_schedules-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="weekday">
                                        { item.weekday }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="period_label">
                                        { item.period_label }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="starts_at">
                                        { dataFormatter.dateTimeFormatter(item.starts_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ends_at">
                                        { dataFormatter.dateTimeFormatter(item.ends_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="room">
                                        { item.room }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!subjects?.class_schedules_subject?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Assignments Subject</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>AssignedAt</th>
                                
                                
                                
                                <th>DueAt</th>
                                
                                
                                
                                <th>Visibility</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {subjects.assignments_subject && Array.isArray(subjects.assignments_subject) &&
                              subjects.assignments_subject.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/assignments/assignments-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="assigned_at">
                                        { dataFormatter.dateTimeFormatter(item.assigned_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_at">
                                        { dataFormatter.dateTimeFormatter(item.due_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="visibility">
                                        { item.visibility }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!subjects?.assignments_subject?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Lesson_plans Subject</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>WeekStartAt</th>
                                
                                
                                
                                <th>WeekEndAt</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {subjects.lesson_plans_subject && Array.isArray(subjects.lesson_plans_subject) &&
                              subjects.lesson_plans_subject.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/lesson_plans/lesson_plans-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="week_start_at">
                                        { dataFormatter.dateTimeFormatter(item.week_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="week_end_at">
                                        { dataFormatter.dateTimeFormatter(item.week_end_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!subjects?.lesson_plans_subject?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default SubjectsView;