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";

import {hasPermission} from "../../helpers/userPermissions";


const UsersView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { users } = useAppSelector((state) => state.users)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

    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>
                </>
              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Club_memberships User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>ClubRole</th>
                                
                                
                                
                                <th>MemberCode</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                                
                                <th>MemberSince</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.club_memberships_user && Array.isArray(users.club_memberships_user) &&
                              users.club_memberships_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/club_memberships/club_memberships-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="club_role">
                                        { item.club_role }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="member_code">
                                        { item.member_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="member_since_at">
                                        { dataFormatter.dateTimeFormatter(item.member_since_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.club_memberships_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Players User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>PlayerName</th>
                                
                                
                                
                                
                                
                                <th>SeedTier</th>
                                
                                
                                
                                <th>PreferredPositions</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.players_user && Array.isArray(users.players_user) &&
                              users.players_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/players/players-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="display_name">
                                        { item.display_name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="seed_tier">
                                        { item.seed_tier }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="preferred_positions">
                                        { item.preferred_positions }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.players_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Seed_votes VoterUser</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Direction</th>
                                
                                
                                
                                <th>VotedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.seed_votes_voter_user && Array.isArray(users.seed_votes_voter_user) &&
                              users.seed_votes_voter_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/seed_votes/seed_votes-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="direction">
                                        { item.direction }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="voted_at">
                                        { dataFormatter.dateTimeFormatter(item.voted_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.seed_votes_voter_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Game_guests AddedByUser</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Nickname</th>
                                
                                
                                
                                <th>RSVPStatus</th>
                                
                                
                                
                                <th>AttendanceStatus</th>
                                
                                
                                
                                <th>PaymentStatus</th>
                                
                                
                                
                                <th>SeedTierForBalance</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.game_guests_added_by_user && Array.isArray(users.game_guests_added_by_user) &&
                              users.game_guests_added_by_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/game_guests/game_guests-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="nickname">
                                        { item.nickname }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rsvp_status">
                                        { item.rsvp_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="attendance_status">
                                        { item.attendance_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_status">
                                        { item.payment_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="seed_tier_for_balance">
                                        { item.seed_tier_for_balance }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.game_guests_added_by_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Notifications RecipientUser</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Category</th>
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>Message</th>
                                
                                
                                
                                <th>DeepLink</th>
                                
                                
                                
                                <th>IsRead</th>
                                
                                
                                
                                <th>SentAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.notifications_recipient_user && Array.isArray(users.notifications_recipient_user) &&
                              users.notifications_recipient_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/notifications/notifications-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="category">
                                        { item.category }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="message">
                                        { item.message }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="deep_link">
                                        { item.deep_link }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_read">
                                        { dataFormatter.booleanFormatter(item.is_read) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sent_at">
                                        { dataFormatter.dateTimeFormatter(item.sent_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.notifications_recipient_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Audit_events ActorUser</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Action</th>
                                
                                
                                
                                <th>EntityName</th>
                                
                                
                                
                                <th>EntityReference</th>
                                
                                
                                
                                <th>Details</th>
                                
                                
                                
                                <th>OccurredAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.audit_events_actor_user && Array.isArray(users.audit_events_actor_user) &&
                              users.audit_events_actor_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/audit_events/audit_events-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="action">
                                        { item.action }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="entity_name">
                                        { item.entity_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="entity_reference">
                                        { item.entity_reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="details">
                                        { item.details }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="occurred_at">
                                        { dataFormatter.dateTimeFormatter(item.occurred_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.audit_events_actor_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;