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/teachers/teachersSlice';
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 TeachersView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { teachers } = useAppSelector((state) => state.teachers);

  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 teachers')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title={removeLastCharacter('View teachers')}
          main
        >
          <BaseButton
            color='info'
            label='Edit'
            href={`/teachers/teachers-edit/?id=${id}`}
          />
        </SectionTitleLineWithButton>
        <CardBox>
          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>User</p>

            <p>{teachers?.user?.firstName ?? 'No data'}</p>
          </div>

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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>FullName</p>
            <p>{teachers?.full_name}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>SubjectSpecialization</p>
            <p>{teachers?.subject_specialization}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>PhoneNumber</p>
            <p>{teachers?.phone_number}</p>
          </div>

          <FormField label='JoiningDate'>
            {teachers.joining_date ? (
              <DatePicker
                dateFormat='yyyy-MM-dd hh:mm'
                showTimeSelect
                selected={
                  teachers.joining_date
                    ? new Date(
                        dayjs(teachers.joining_date).format('YYYY-MM-DD hh:mm'),
                      )
                    : null
                }
                disabled
              />
            ) : (
              <p>No JoiningDate</p>
            )}
          </FormField>

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

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

          <>
            <p className={'block font-bold mb-2'}>Assignments Teacher</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>
                    {teachers.assignments_teacher &&
                      Array.isArray(teachers.assignments_teacher) &&
                      teachers.assignments_teacher.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>
              {!teachers?.assignments_teacher?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <>
            <p className={'block font-bold mb-2'}>Class_subjects Teacher</p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr></tr>
                  </thead>
                  <tbody>
                    {teachers.class_subjects_teacher &&
                      Array.isArray(teachers.class_subjects_teacher) &&
                      teachers.class_subjects_teacher.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/class_subjects/class_subjects-view/?id=${item.id}`,
                            )
                          }
                        ></tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!teachers?.class_subjects_teacher?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

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

                      <th>Section</th>
                    </tr>
                  </thead>
                  <tbody>
                    {teachers.classes_class_teacher &&
                      Array.isArray(teachers.classes_class_teacher) &&
                      teachers.classes_class_teacher.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/classes/classes-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='name'>{item.name}</td>

                          <td data-label='section'>{item.section}</td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!teachers?.classes_class_teacher?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <>
            <p className={'block font-bold mb-2'}>Lesson_plans Teacher</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>
                    {teachers.lesson_plans_teacher &&
                      Array.isArray(teachers.lesson_plans_teacher) &&
                      teachers.lesson_plans_teacher.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>
              {!teachers?.lesson_plans_teacher?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

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

                      <th>Status</th>
                    </tr>
                  </thead>
                  <tbody>
                    {teachers.student_attendance_marked_by &&
                      Array.isArray(teachers.student_attendance_marked_by) &&
                      teachers.student_attendance_marked_by.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/student_attendance/student_attendance-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='date'>
                            {dataFormatter.dateTimeFormatter(item.date)}
                          </td>

                          <td data-label='status'>{item.status}</td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!teachers?.student_attendance_marked_by?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

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

                      <th>Status</th>
                    </tr>
                  </thead>
                  <tbody>
                    {teachers.teacher_attendance_teacher &&
                      Array.isArray(teachers.teacher_attendance_teacher) &&
                      teachers.teacher_attendance_teacher.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/teacher_attendance/teacher_attendance-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='date'>
                            {dataFormatter.dateTimeFormatter(item.date)}
                          </td>

                          <td data-label='status'>{item.status}</td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!teachers?.teacher_attendance_teacher?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <>
            <p className={'block font-bold mb-2'}>Timetables Teacher</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>
                    {teachers.timetables_teacher &&
                      Array.isArray(teachers.timetables_teacher) &&
                      teachers.timetables_teacher.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>
              {!teachers?.timetables_teacher?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <BaseDivider />

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

TeachersView.getLayout = function getLayout(page: ReactElement) {
  return (
    <LayoutAuthenticated permission={'READ_TEACHERS'}>
      {page}
    </LayoutAuthenticated>
  );
};

export default TeachersView;
