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/exercises/exercisesSlice'
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 ExercisesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { exercises } = useAppSelector((state) => state.exercises)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ExerciseName</p>
                    <p>{exercises?.exercise_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ExerciseType</p>
                    <p>{exercises?.exercise_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PrimaryMuscleGroup</p>
                    <p>{exercises?.primary_muscle_group ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Equipment</p>
                    <p>{exercises?.equipment ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={exercises?.instructions} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={exercises?.common_mistakes} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={exercises?.coaching_cues} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Workout_exercises Exercise</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Order</th>
                                
                                
                                
                                <th>Sets</th>
                                
                                
                                
                                <th>Reps</th>
                                
                                
                                
                                <th>Rest(seconds)</th>
                                
                                
                                
                                <th>BeginnerModifications</th>
                                
                                
                                
                                <th>AdvancedProgressions</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {exercises.workout_exercises_exercise && Array.isArray(exercises.workout_exercises_exercise) &&
                              exercises.workout_exercises_exercise.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/workout_exercises/workout_exercises-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sort_order">
                                        { item.sort_order }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sets">
                                        { item.sets }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reps">
                                        { item.reps }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rest_seconds">
                                        { item.rest_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="beginner_modifications">
                                        { item.beginner_modifications }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="advanced_progressions">
                                        { item.advanced_progressions }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!exercises?.workout_exercises_exercise?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Strength_sets Exercise</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>SetNumber</th>
                                
                                
                                
                                <th>Reps</th>
                                
                                
                                
                                <th>Weight</th>
                                
                                
                                
                                <th>WeightUnit</th>
                                
                                
                                
                                <th>RPE(1-10)</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {exercises.strength_sets_exercise && Array.isArray(exercises.strength_sets_exercise) &&
                              exercises.strength_sets_exercise.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/strength_sets/strength_sets-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="set_number">
                                        { item.set_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reps">
                                        { item.reps }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="weight">
                                        { item.weight }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="weight_unit">
                                        { item.weight_unit }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rpe">
                                        { item.rpe }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!exercises?.strength_sets_exercise?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Physio_exercises Exercise</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Sets</th>
                                
                                
                                
                                <th>Reps</th>
                                
                                
                                
                                <th>ProgressionNotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {exercises.physio_exercises_exercise && Array.isArray(exercises.physio_exercises_exercise) &&
                              exercises.physio_exercises_exercise.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/physio_exercises/physio_exercises-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sets">
                                        { item.sets }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reps">
                                        { item.reps }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="progression_notes">
                                        { item.progression_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!exercises?.physio_exercises_exercise?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default ExercisesView;