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';

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

const SubjectsView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { subjects } = useAppSelector((state) => state.subjects);

  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 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'}>School</p>

            <p>{subjects?.school?.name ?? 'No data'}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>schools</p>

            <p>{subjects?.schools?.name ?? 'No data'}</p>
          </div>

          <>
            <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>Description</th>

                      <th>DueDate</th>

                      <th>AttachmentURL</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='description'>{item.description}</td>

                          <td data-label='due_date'>
                            {dataFormatter.dateTimeFormatter(item.due_date)}
                          </td>

                          <td data-label='attachment_url'>
                            {item.attachment_url}
                          </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'}>Class_subjects Subject</p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr></tr>
                  </thead>
                  <tbody>
                    {subjects.class_subjects_subject &&
                      Array.isArray(subjects.class_subjects_subject) &&
                      subjects.class_subjects_subject.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/class_subjects/class_subjects-view/?id=${item.id}`,
                            )
                          }
                        ></tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!subjects?.class_subjects_subject?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <>
            <p className={'block font-bold mb-2'}>Exam_subjects Subject</p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr>
                      <th>ExamDate</th>

                      <th>MaxMarks</th>
                    </tr>
                  </thead>
                  <tbody>
                    {subjects.exam_subjects_subject &&
                      Array.isArray(subjects.exam_subjects_subject) &&
                      subjects.exam_subjects_subject.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/exam_subjects/exam_subjects-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='exam_date'>
                            {dataFormatter.dateTimeFormatter(item.exam_date)}
                          </td>

                          <td data-label='max_marks'>{item.max_marks}</td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!subjects?.exam_subjects_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>WeekStartDate</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_date'>
                            {dataFormatter.dateTimeFormatter(
                              item.week_start_date,
                            )}
                          </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>
          </>

          <>
            <p className={'block font-bold mb-2'}>Timetables Subject</p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr>
                      <th>DayofWeek</th>

                      <th>PeriodNumber</th>
                    </tr>
                  </thead>
                  <tbody>
                    {subjects.timetables_subject &&
                      Array.isArray(subjects.timetables_subject) &&
                      subjects.timetables_subject.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/timetables/timetables-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='day_of_week'>{item.day_of_week}</td>

                          <td data-label='period_number'>
                            {item.period_number}
                          </td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!subjects?.timetables_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;
