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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>GradeName</p>
                    <p>{grade_levels?.grade_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>GradeOrder</p>
                  <p>{grade_levels?.grade_order || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsActive'>
                    <SwitchField
                      field={{name: 'is_active', value: grade_levels?.is_active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Subjects GradeLevel</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>SubjectName</th>
                                
                                
                                
                                <th>SubjectCode</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {grade_levels.subjects_grade_level && Array.isArray(grade_levels.subjects_grade_level) &&
                              grade_levels.subjects_grade_level.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/subjects/subjects-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="subject_name">
                                        { item.subject_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subject_code">
                                        { item.subject_code }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!grade_levels?.subjects_grade_level?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Classrooms GradeLevel</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>ClassroomName</th>
                                
                                
                                
                                <th>SectionName</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>InviteCode</th>
                                
                                
                                
                                <th>InviteCodeExpiresAt</th>
                                
                                
                                
                                <th>InviteCodeEnabled</th>
                                
                                
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {grade_levels.classrooms_grade_level && Array.isArray(grade_levels.classrooms_grade_level) &&
                              grade_levels.classrooms_grade_level.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/classrooms/classrooms-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="classroom_name">
                                        { item.classroom_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="section_name">
                                        { item.section_name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="invite_code">
                                        { item.invite_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="invite_code_expires_at">
                                        { dataFormatter.dateTimeFormatter(item.invite_code_expires_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="invite_code_enabled">
                                        { dataFormatter.booleanFormatter(item.invite_code_enabled) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!grade_levels?.classrooms_grade_level?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Grade_levelsView;