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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Grade</p>
                  <p>{classes?.grade || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>HomeroomLabel</p>
                    <p>{classes?.homeroom_label}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>HomeroomTeacher</p>
                    
                    
                        <p>{classes?.homeroom_teacher?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Students CurrentClass</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>NIS</th>
                                
                                
                                
                                <th>NISN</th>
                                
                                
                                
                                <th>BirthPlace</th>
                                
                                
                                
                                <th>BirthDate</th>
                                
                                
                                
                                <th>Gender</th>
                                
                                
                                
                                <th>Address</th>
                                
                                
                                
                                <th>EnrollmentDate</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {classes.students_current_class && Array.isArray(classes.students_current_class) &&
                              classes.students_current_class.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/students/students-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="nis">
                                        { item.nis }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="nisn">
                                        { item.nisn }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="birth_place">
                                        { item.birth_place }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="birth_date">
                                        { dataFormatter.dateTimeFormatter(item.birth_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="gender">
                                        { item.gender }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="address">
                                        { item.address }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="enrollment_date">
                                        { dataFormatter.dateTimeFormatter(item.enrollment_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!classes?.students_current_class?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Class_subjects Class</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>WeeklySessions</th>
                                
                                
                                
                                <th>Active</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {classes.class_subjects_class && Array.isArray(classes.class_subjects_class) &&
                              classes.class_subjects_class.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/class_subjects/class_subjects-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="weekly_sessions">
                                        { item.weekly_sessions }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="active">
                                        { dataFormatter.booleanFormatter(item.active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!classes?.class_subjects_class?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Timetable_entries Class</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>
                                
                                
                                
                                <th>StartTime</th>
                                
                                
                                
                                <th>EndTime</th>
                                
                                
                                
                                <th>Room</th>
                                
                                
                                
                                <th>Active</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {classes.timetable_entries_class && Array.isArray(classes.timetable_entries_class) &&
                              classes.timetable_entries_class.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/timetable_entries/timetable_entries-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="day_of_week">
                                        { item.day_of_week }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="period_number">
                                        { item.period_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="start_time">
                                        { item.start_time }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="end_time">
                                        { item.end_time }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="room">
                                        { item.room }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="active">
                                        { dataFormatter.booleanFormatter(item.active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!classes?.timetable_entries_class?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Attendance_sessions Class</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>AttendanceDate</th>
                                
                                
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {classes.attendance_sessions_class && Array.isArray(classes.attendance_sessions_class) &&
                              classes.attendance_sessions_class.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/attendance_sessions/attendance_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="attendance_date">
                                        { dataFormatter.dateTimeFormatter(item.attendance_date) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!classes?.attendance_sessions_class?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Announcements TargetClass</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>Audience</th>
                                
                                
                                
                                
                                
                                <th>PublishAt</th>
                                
                                
                                
                                <th>ExpireAt</th>
                                
                                
                                
                                <th>Pinned</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {classes.announcements_target_class && Array.isArray(classes.announcements_target_class) &&
                              classes.announcements_target_class.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/announcements/announcements-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="audience">
                                        { item.audience }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="publish_at">
                                        { dataFormatter.dateTimeFormatter(item.publish_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expire_at">
                                        { dataFormatter.dateTimeFormatter(item.expire_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="pinned">
                                        { dataFormatter.booleanFormatter(item.pinned) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!classes?.announcements_target_class?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Fee_definitions ClassOverride</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>FeeType</th>
                                
                                
                                
                                <th>FeeName</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Amount</th>
                                
                                
                                
                                <th>AllowInstallments</th>
                                
                                
                                
                                <th>MinimumDownPayment</th>
                                
                                
                                
                                <th>MaxInstallments</th>
                                
                                
                                
                                <th>InstallmentDeadline</th>
                                
                                
                                
                                <th>Active</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {classes.fee_definitions_class && Array.isArray(classes.fee_definitions_class) &&
                              classes.fee_definitions_class.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/fee_definitions/fee_definitions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="fee_type">
                                        { item.fee_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="amount">
                                        { item.amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="allow_installments">
                                        { dataFormatter.booleanFormatter(item.allow_installments) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="minimum_down_payment">
                                        { item.minimum_down_payment }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_installments">
                                        { item.max_installments }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="installment_deadline">
                                        { dataFormatter.dateTimeFormatter(item.installment_deadline) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="active">
                                        { dataFormatter.booleanFormatter(item.active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!classes?.fee_definitions_class?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                

                <BaseDivider />

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

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

export default ClassesView;