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/question_answers/question_answersSlice'
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 Question_answersView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { question_answers } = useAppSelector((state) => state.question_answers)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Question</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{question_answers?.question?.prompt ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AnswerText</p>
                    <p>{question_answers?.answer_text}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsCorrect'>
                    <SwitchField
                      field={{name: 'is_correct', value: question_answers?.is_correct}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

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

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Player_answers SelectedAnswer</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>IsCorrect</th>
                                
                                
                                
                                <th>PointsAwarded</th>
                                
                                
                                
                                <th>AnswerTime(ms)</th>
                                
                                
                                
                                <th>AnsweredAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {question_answers.player_answers_selected_answer && Array.isArray(question_answers.player_answers_selected_answer) &&
                              question_answers.player_answers_selected_answer.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/player_answers/player_answers-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_correct">
                                        { dataFormatter.booleanFormatter(item.is_correct) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="points_awarded">
                                        { item.points_awarded }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="answer_time_ms">
                                        { item.answer_time_ms }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="answered_at">
                                        { dataFormatter.dateTimeFormatter(item.answered_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!question_answers?.player_answers_selected_answer?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Question_answersView;