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/exams/examsSlice'
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 ExamsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { exams } = useAppSelector((state) => state.exams)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ExamType</p>
                    <p>{exams?.exam_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SchoolYear</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{exams?.school_year?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Term</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{exams?.term?.term_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Stream</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{exams?.stream?.name_en ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='PublicResults'>
                    <SwitchField
                      field={{name: 'public_results', value: exams?.public_results}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Exam_results Exam</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>
                            {exams.exam_results_exam && Array.isArray(exams.exam_results_exam) &&
                              exams.exam_results_exam.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>
                        {!exams?.exam_results_exam?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Exam_performance_summaries Exam</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>AverageScore</th>
                                
                                
                                
                                <th>HighestScore</th>
                                
                                
                                
                                <th>LowestScore</th>
                                
                                
                                
                                <th>PassRate</th>
                                
                                
                                
                                <th>StudentCount</th>
                                
                                
                                
                                <th>Published</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {exams.exam_performance_summaries_exam && Array.isArray(exams.exam_performance_summaries_exam) &&
                              exams.exam_performance_summaries_exam.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/exam_performance_summaries/exam_performance_summaries-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="average_score">
                                        { item.average_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="highest_score">
                                        { item.highest_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lowest_score">
                                        { item.lowest_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="pass_rate">
                                        { item.pass_rate }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="student_count">
                                        { item.student_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="published">
                                        { dataFormatter.booleanFormatter(item.published) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!exams?.exam_performance_summaries_exam?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Top_student_features Exam</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>TotalScore</th>
                                
                                
                                
                                <th>Rank</th>
                                
                                
                                
                                <th>ConsentToPublish</th>
                                
                                
                                
                                <th>Published</th>
                                
                                
                                
                                <th>Note</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {exams.top_student_features_exam && Array.isArray(exams.top_student_features_exam) &&
                              exams.top_student_features_exam.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/top_student_features/top_student_features-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="total_score">
                                        { item.total_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rank">
                                        { item.rank }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="consent_to_publish">
                                        { dataFormatter.booleanFormatter(item.consent_to_publish) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="published">
                                        { dataFormatter.booleanFormatter(item.published) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="note">
                                        { item.note }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!exams?.top_student_features_exam?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                

                <BaseDivider />

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

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

export default ExamsView;