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/spaces/spacesSlice'
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 SpacesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { spaces } = useAppSelector((state) => state.spaces)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SpaceNameBangla</p>
                    <p>{spaces?.name_bn}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Visibility</p>
                    <p>{spaces?.visibility ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SpaceKind</p>
                    <p>{spaces?.space_kind ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={spaces?.description} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SeedUnreadCount</p>
                  <p>{spaces?.unread_seed_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Space_memberships Space</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>MemberRole</th>
                                
                                
                                
                                <th>MembershipStatus</th>
                                
                                
                                
                                <th>JoinedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {spaces.space_memberships_space && Array.isArray(spaces.space_memberships_space) &&
                              spaces.space_memberships_space.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/space_memberships/space_memberships-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="role">
                                        { item.role }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="joined_at">
                                        { dataFormatter.dateTimeFormatter(item.joined_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!spaces?.space_memberships_space?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Community_posts Space</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>PostType</th>
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>Body</th>
                                
                                
                                
                                <th>ImageURL</th>
                                
                                
                                
                                <th>ResourceURL</th>
                                
                                
                                
                                <th>PollOptionsJSON</th>
                                
                                
                                
                                <th>Pinned</th>
                                
                                
                                
                                <th>PostVisibility</th>
                                
                                
                                
                                <th>LikeCount</th>
                                
                                
                                
                                <th>LoveCount</th>
                                
                                
                                
                                <th>InsightfulCount</th>
                                
                                
                                
                                <th>CommentCount</th>
                                
                                
                                
                                <th>PublishedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {spaces.community_posts_space && Array.isArray(spaces.community_posts_space) &&
                              spaces.community_posts_space.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/community_posts/community_posts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="post_type">
                                        { item.post_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="body">
                                        { item.body }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="image_url">
                                        { item.image_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="resource_url">
                                        { item.resource_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="poll_options_json">
                                        { item.poll_options_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_pinned">
                                        { dataFormatter.booleanFormatter(item.is_pinned) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="visibility">
                                        { item.visibility }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="like_count">
                                        { item.like_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="love_count">
                                        { item.love_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="insightful_count">
                                        { item.insightful_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="comment_count">
                                        { item.comment_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="published_at">
                                        { dataFormatter.dateTimeFormatter(item.published_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!spaces?.community_posts_space?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Live_events Space</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>EventTitle</th>
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>EventType</th>
                                
                                
                                
                                <th>MeetingProvider</th>
                                
                                
                                
                                <th>MeetingLink</th>
                                
                                
                                
                                <th>RecordingLink</th>
                                
                                
                                
                                <th>StartAt</th>
                                
                                
                                
                                <th>EndAt</th>
                                
                                
                                
                                <th>Capacity</th>
                                
                                
                                
                                <th>PriceAmount</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {spaces.live_events_space && Array.isArray(spaces.live_events_space) &&
                              spaces.live_events_space.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/live_events/live_events-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="event_type">
                                        { item.event_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="meeting_provider">
                                        { item.meeting_provider }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="meeting_link">
                                        { item.meeting_link }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="recording_link">
                                        { item.recording_link }
                                    </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="capacity">
                                        { item.capacity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="price_amount">
                                        { item.price_amount }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!spaces?.live_events_space?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                

                <BaseDivider />

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

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

export default SpacesView;