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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Slug</p>
                    <p>{clubs?.slug}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>City</p>
                    <p>{clubs?.city}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Country</p>
                    <p>{clubs?.country}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DefaultLanguage</p>
                    <p>{clubs?.default_language ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>DefaultTargetHeadcount</p>
                  <p>{clubs?.default_target_headcount || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PaymentReminderDays</p>
                  <p>{clubs?.payment_reminder_days || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>InactiveNudgeLastGames</p>
                  <p>{clubs?.inactive_nudge_last_games || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>MessengerGroupThread</p>
                    <p>{clubs?.messenger_group_thread}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>MessengerAccessToken</p>
                    <p>{clubs?.messenger_access_token}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>StripeAccountReference</p>
                    <p>{clubs?.stripe_account_reference}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Club_memberships Club</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>
                            {clubs.club_memberships_club && Array.isArray(clubs.club_memberships_club) &&
                              clubs.club_memberships_club.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>
                        {!clubs?.club_memberships_club?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Players Club</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>
                            {clubs.players_club && Array.isArray(clubs.players_club) &&
                              clubs.players_club.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>
                        {!clubs?.players_club?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Seed_votes Club</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>
                            {clubs.seed_votes_club && Array.isArray(clubs.seed_votes_club) &&
                              clubs.seed_votes_club.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>
                        {!clubs?.seed_votes_club?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Venues Club</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>VenueName</th>
                                
                                
                                
                                <th>Address</th>
                                
                                
                                
                                <th>MapURL</th>
                                
                                
                                
                                <th>DefaultFieldCost</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {clubs.venues_club && Array.isArray(clubs.venues_club) &&
                              clubs.venues_club.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/venues/venues-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="address">
                                        { item.address }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="map_url">
                                        { item.map_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="default_field_cost">
                                        { item.default_field_cost }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!clubs?.venues_club?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Leagues Club</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>LeagueName</th>
                                
                                
                                
                                <th>SeasonLabel</th>
                                
                                
                                
                                <th>StartDate</th>
                                
                                
                                
                                <th>EndDate</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>RostersLocked</th>
                                
                                
                                
                                <th>RegistrationFee</th>
                                
                                
                                
                                <th>RulesNotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {clubs.leagues_club && Array.isArray(clubs.leagues_club) &&
                              clubs.leagues_club.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/leagues/leagues-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="season_label">
                                        { item.season_label }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="start_at">
                                        { dataFormatter.dateTimeFormatter(item.start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="end_at">
                                        { dataFormatter.dateTimeFormatter(item.end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rosters_locked">
                                        { dataFormatter.booleanFormatter(item.rosters_locked) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="registration_fee">
                                        { item.registration_fee }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rules_notes">
                                        { item.rules_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!clubs?.leagues_club?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Games Club</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>GameTitle</th>
                                
                                
                                
                                <th>GameType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>StartTime</th>
                                
                                
                                
                                <th>EndTime</th>
                                
                                
                                
                                <th>TargetHeadcount</th>
                                
                                
                                
                                <th>TeamCount</th>
                                
                                
                                
                                <th>FieldCost</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                <th>MessengerBroadcastSent</th>
                                
                                
                                
                                <th>FinalizedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {clubs.games_club && Array.isArray(clubs.games_club) &&
                              clubs.games_club.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/games/games-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="game_type">
                                        { item.game_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="start_at">
                                        { dataFormatter.dateTimeFormatter(item.start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="end_at">
                                        { dataFormatter.dateTimeFormatter(item.end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_headcount">
                                        { item.target_headcount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="team_count">
                                        { item.team_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="field_cost">
                                        { item.field_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="messenger_broadcast_sent">
                                        { dataFormatter.booleanFormatter(item.messenger_broadcast_sent) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="finalized_at">
                                        { dataFormatter.dateTimeFormatter(item.finalized_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!clubs?.games_club?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Notifications Club</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>
                            {clubs.notifications_club && Array.isArray(clubs.notifications_club) &&
                              clubs.notifications_club.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>
                        {!clubs?.notifications_club?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Audit_events Club</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>
                            {clubs.audit_events_club && Array.isArray(clubs.audit_events_club) &&
                              clubs.audit_events_club.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>
                        {!clubs?.audit_events_club?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                

                <BaseDivider />

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

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

export default ClubsView;