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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Category</p>
                    <p>{achievement_definitions?.category ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Rarity</p>
                    <p>{achievement_definitions?.rarity ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

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

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Metric</p>
                    <p>{achievement_definitions?.metric ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Active'>
                    <SwitchField
                      field={{name: 'active', value: achievement_definitions?.active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>User_achievements Achievementdefinition</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Earnedat</th>
                                
                                
                                
                                <th>Progressvalue</th>
                                
                                
                                
                                <th>Notified</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {achievement_definitions.user_achievements_achievement_definition && Array.isArray(achievement_definitions.user_achievements_achievement_definition) &&
                              achievement_definitions.user_achievements_achievement_definition.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/user_achievements/user_achievements-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="earned_at">
                                        { dataFormatter.dateTimeFormatter(item.earned_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="progress_value">
                                        { item.progress_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notified">
                                        { dataFormatter.booleanFormatter(item.notified) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!achievement_definitions?.user_achievements_achievement_definition?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Achievement_definitionsView;