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_questions/quiz_questionsSlice'
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_questionsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { quiz_questions } = useAppSelector((state) => state.quiz_questions)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Quiz</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{quiz_questions?.quiz?.title ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>QuestionPrompt</p>
                  {quiz_questions.prompt
                    ? <p dangerouslySetInnerHTML={{__html: quiz_questions.prompt}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PromptImages</p>
                    {quiz_questions?.prompt_images?.length
                      ? (
                        <ImageField
                          name={'prompt_images'}
                          image={quiz_questions?.prompt_images}
                          className='w-20 h-20'
                        />
                      ) : <p>No PromptImages</p>
                    }
                </div>
              

              
              

              

              

              

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

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Points</p>
                  <p>{quiz_questions?.points || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>QuestionType</p>
                    <p>{quiz_questions?.question_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>OrderIndex</p>
                  <p>{quiz_questions?.order_index || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Question_answers Question</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>AnswerText</th>
                                
                                
                                
                                
                                
                                <th>IsCorrect</th>
                                
                                
                                
                                <th>OrderIndex</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {quiz_questions.question_answers_question && Array.isArray(quiz_questions.question_answers_question) &&
                              quiz_questions.question_answers_question.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/question_answers/question_answers-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="answer_text">
                                        { item.answer_text }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_correct">
                                        { dataFormatter.booleanFormatter(item.is_correct) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="order_index">
                                        { item.order_index }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!quiz_questions?.question_answers_question?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Match_questions QuizQuestion</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>QuestionIndex</th>
                                
                                
                                
                                <th>RevealedAt</th>
                                
                                
                                
                                <th>ClosedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {quiz_questions.match_questions_quiz_question && Array.isArray(quiz_questions.match_questions_quiz_question) &&
                              quiz_questions.match_questions_quiz_question.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/match_questions/match_questions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="question_index">
                                        { item.question_index }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="revealed_at">
                                        { dataFormatter.dateTimeFormatter(item.revealed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="closed_at">
                                        { dataFormatter.dateTimeFormatter(item.closed_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!quiz_questions?.match_questions_quiz_question?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Quiz_questionsView;