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/users/usersSlice'
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 UsersView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { users } = useAppSelector((state) => state.users)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Phone Number</p>
                    <p>{users?.phoneNumber}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>E-Mail</p>
                    <p>{users?.email}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>App Role</p>
                    
                    
                    
                    
                        <p>{users?.app_role?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>Custom Permissions</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                    <th>Name</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.custom_permissions && Array.isArray(users.custom_permissions) &&
                              users.custom_permissions.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/permissions/permissions-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                  
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.custom_permissions?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Api_request_logs User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Provider</th>
                                
                                
                                
                                <th>RequestMethod</th>
                                
                                
                                
                                <th>RequestPath</th>
                                
                                
                                
                                <th>RequestQuery</th>
                                
                                
                                
                                <th>ResponseStatus</th>
                                
                                
                                
                                <th>DurationMs</th>
                                
                                
                                
                                <th>ErrorMessage</th>
                                
                                
                                
                                <th>RequestedAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.api_request_logs_user && Array.isArray(users.api_request_logs_user) &&
                              users.api_request_logs_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/api_request_logs/api_request_logs-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="provider">
                                        { item.provider }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="request_method">
                                        { item.request_method }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="request_path">
                                        { item.request_path }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="request_query">
                                        { item.request_query }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="response_status">
                                        { item.response_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="duration_ms">
                                        { item.duration_ms }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_message">
                                        { item.error_message }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requested_at">
                                        { dataFormatter.dateTimeFormatter(item.requested_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.api_request_logs_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Sync_jobs TriggeredBy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>JobType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>FinishedAt</th>
                                
                                
                                
                                <th>ProcessedCount</th>
                                
                                
                                
                                <th>InsertedCount</th>
                                
                                
                                
                                <th>UpdatedCount</th>
                                
                                
                                
                                <th>SkippedCount</th>
                                
                                
                                
                                <th>ErrorCount</th>
                                
                                
                                
                                <th>ErrorSummary</th>
                                
                                
                                
                                <th>JobPayloadJson</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.sync_jobs_triggered_by && Array.isArray(users.sync_jobs_triggered_by) &&
                              users.sync_jobs_triggered_by.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/sync_jobs/sync_jobs-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="job_type">
                                        { item.job_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="finished_at">
                                        { dataFormatter.dateTimeFormatter(item.finished_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="processed_count">
                                        { item.processed_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="inserted_count">
                                        { item.inserted_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="updated_count">
                                        { item.updated_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="skipped_count">
                                        { item.skipped_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_count">
                                        { item.error_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_summary">
                                        { item.error_summary }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="job_payload_json">
                                        { item.job_payload_json }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.sync_jobs_triggered_by?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Game_sessions User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>SessionToken</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>StartingLives</th>
                                
                                
                                
                                <th>LivesRemaining</th>
                                
                                
                                
                                <th>Streak</th>
                                
                                
                                
                                <th>BestStreak</th>
                                
                                
                                
                                <th>TimerProfile</th>
                                
                                
                                
                                <th>ModeSelection</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>EndedAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.game_sessions_user && Array.isArray(users.game_sessions_user) &&
                              users.game_sessions_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/game_sessions/game_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="session_token">
                                        { item.session_token }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="starting_lives">
                                        { item.starting_lives }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lives_remaining">
                                        { item.lives_remaining }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="streak">
                                        { item.streak }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="best_streak">
                                        { item.best_streak }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="timer_profile">
                                        { item.timer_profile }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mode_selection">
                                        { item.mode_selection }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ended_at">
                                        { dataFormatter.dateTimeFormatter(item.ended_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.game_sessions_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Leaderboard_entries User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>BoardType</th>
                                
                                
                                
                                <th>SeedKey</th>
                                
                                
                                
                                <th>Score</th>
                                
                                
                                
                                <th>RoundsSurvived</th>
                                
                                
                                
                                <th>BestStreak</th>
                                
                                
                                
                                <th>AchievedAt</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.leaderboard_entries_user && Array.isArray(users.leaderboard_entries_user) &&
                              users.leaderboard_entries_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/leaderboard_entries/leaderboard_entries-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="board_type">
                                        { item.board_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="seed_key">
                                        { item.seed_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="score">
                                        { item.score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rounds_survived">
                                        { item.rounds_survived }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="best_streak">
                                        { item.best_streak }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="achieved_at">
                                        { dataFormatter.dateTimeFormatter(item.achieved_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.leaderboard_entries_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                

                <BaseDivider />

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

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

export default UsersView;