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/quiz_definitions/quiz_definitionsSlice'
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 Quiz_definitionsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { quiz_definitions } = useAppSelector((state) => state.quiz_definitions)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Book</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{quiz_definitions?.book?.title ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Title</p>
                    <p>{quiz_definitions?.title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Difficulty</p>
                    <p>{quiz_definitions?.difficulty ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{quiz_definitions?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Questioncount</p>
                  <p>{quiz_definitions?.question_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Timelimit(seconds)</p>
                  <p>{quiz_definitions?.time_limit_seconds || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quiz_questions Quizdefinition</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Prompt</th>
                                
                                
                                
                                <th>Questiontype</th>
                                
                                
                                
                                <th>Explanation</th>
                                
                                
                                
                                <th>Orderindex</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {quiz_definitions.quiz_questions_quiz_definition && Array.isArray(quiz_definitions.quiz_questions_quiz_definition) &&
                              quiz_definitions.quiz_questions_quiz_definition.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quiz_questions/quiz_questions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="prompt">
                                        { item.prompt }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="question_type">
                                        { item.question_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="explanation">
                                        { item.explanation }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="order_index">
                                        { item.order_index }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!quiz_definitions?.quiz_questions_quiz_definition?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quiz_attempts Quizdefinition</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Scorepercent</th>
                                
                                
                                
                                <th>Correctcount</th>
                                
                                
                                
                                <th>Totalcount</th>
                                
                                
                                
                                <th>Startedat</th>
                                
                                
                                
                                <th>Submittedat</th>
                                
                                
                                
                                <th>Coinsawarded</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {quiz_definitions.quiz_attempts_quiz_definition && Array.isArray(quiz_definitions.quiz_attempts_quiz_definition) &&
                              quiz_definitions.quiz_attempts_quiz_definition.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quiz_attempts/quiz_attempts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="score_percent">
                                        { item.score_percent }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="correct_count">
                                        { item.correct_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_count">
                                        { item.total_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="submitted_at">
                                        { dataFormatter.dateTimeFormatter(item.submitted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="coins_awarded">
                                        { item.coins_awarded }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!quiz_definitions?.quiz_attempts_quiz_definition?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Quiz_definitionsView;