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/challenge_definitions/challenge_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 Challenge_definitionsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { challenge_definitions } = useAppSelector((state) => state.challenge_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 challenge_definitions')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View challenge_definitions')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/challenge_definitions/challenge_definitions-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Challengetype</p>
                    <p>{challenge_definitions?.challenge_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Targetvalue</p>
                  <p>{challenge_definitions?.target_value || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Rewardcoins</p>
                  <p>{challenge_definitions?.reward_coins || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>User_challenges Challengedefinition</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>Progressvalue</th>
                                
                                
                                
                                <th>Joinedat</th>
                                
                                
                                
                                <th>Completedat</th>
                                
                                
                                
                                <th>Coinsawarded</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {challenge_definitions.user_challenges_challenge_definition && Array.isArray(challenge_definitions.user_challenges_challenge_definition) &&
                              challenge_definitions.user_challenges_challenge_definition.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/user_challenges/user_challenges-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="progress_value">
                                        { item.progress_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="joined_at">
                                        { dataFormatter.dateTimeFormatter(item.joined_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completed_at">
                                        { dataFormatter.dateTimeFormatter(item.completed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="coins_awarded">
                                        { item.coins_awarded }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!challenge_definitions?.user_challenges_challenge_definition?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Challenge_definitionsView;