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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RoomKey</p>
                    <p>{rooms?.room_key}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RoomType</p>
                    <p>{rooms?.room_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AccessLevel</p>
                    <p>{rooms?.access_level ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='LastActiveAt'>
                    {rooms.last_active_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={rooms.last_active_at ?
                        new Date(
                          dayjs(rooms.last_active_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No LastActiveAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Room_invitations Room</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>InviteeEmail</th>
                                
                                
                                
                                <th>InviteCode</th>
                                
                                
                                
                                <th>InviteRole</th>
                                
                                
                                
                                <th>InvitationStatus</th>
                                
                                
                                
                                <th>ExpiresAt</th>
                                
                                
                                
                                <th>AcceptedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {rooms.room_invitations_room && Array.isArray(rooms.room_invitations_room) &&
                              rooms.room_invitations_room.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/room_invitations/room_invitations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="invitee_email">
                                        { item.invitee_email }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="invite_code">
                                        { item.invite_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="invite_role">
                                        { item.invite_role }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="invitation_status">
                                        { item.invitation_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expires_at">
                                        { dataFormatter.dateTimeFormatter(item.expires_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="accepted_at">
                                        { dataFormatter.dateTimeFormatter(item.accepted_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!rooms?.room_invitations_room?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Conferences Room</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ConferenceKey</th>
                                
                                
                                
                                <th>ConferenceType</th>
                                
                                
                                
                                <th>ConferenceStatus</th>
                                
                                
                                
                                <th>ScheduledStartAt</th>
                                
                                
                                
                                <th>ScheduledEndAt</th>
                                
                                
                                
                                <th>ActualStartAt</th>
                                
                                
                                
                                <th>ActualEndAt</th>
                                
                                
                                
                                <th>E2EEEnabled</th>
                                
                                
                                
                                <th>ParticipantLimit</th>
                                
                                
                                
                                <th>RegionPreference</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {rooms.conferences_room && Array.isArray(rooms.conferences_room) &&
                              rooms.conferences_room.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/conferences/conferences-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="conference_key">
                                        { item.conference_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="conference_type">
                                        { item.conference_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="conference_status">
                                        { item.conference_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_start_at">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_end_at">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="actual_start_at">
                                        { dataFormatter.dateTimeFormatter(item.actual_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="actual_end_at">
                                        { dataFormatter.dateTimeFormatter(item.actual_end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="e2ee_enabled">
                                        { dataFormatter.booleanFormatter(item.e2ee_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="participant_limit">
                                        { item.participant_limit }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="region_preference">
                                        { item.region_preference }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!rooms?.conferences_room?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Room_files Room</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Visibility</th>
                                
                                
                                
                                <th>AddedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {rooms.room_files_room && Array.isArray(rooms.room_files_room) &&
                              rooms.room_files_room.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/room_files/room_files-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="visibility">
                                        { item.visibility }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="added_at">
                                        { dataFormatter.dateTimeFormatter(item.added_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!rooms?.room_files_room?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Canvas_documents Room</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>CanvasName</th>
                                
                                
                                
                                <th>CanvasStatus</th>
                                
                                
                                
                                <th>LastEditedAt</th>
                                
                                
                                
                                <th>SnapshotURI</th>
                                
                                
                                
                                <th>OfflineEnabled</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {rooms.canvas_documents_room && Array.isArray(rooms.canvas_documents_room) &&
                              rooms.canvas_documents_room.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/canvas_documents/canvas_documents-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="canvas_status">
                                        { item.canvas_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_edited_at">
                                        { dataFormatter.dateTimeFormatter(item.last_edited_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="snapshot_uri">
                                        { item.snapshot_uri }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="offline_enabled">
                                        { dataFormatter.booleanFormatter(item.offline_enabled) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!rooms?.canvas_documents_room?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Courses DefaultRoom</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>CourseName</th>
                                
                                
                                
                                <th>CourseCode</th>
                                
                                
                                
                                <th>CourseStatus</th>
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {rooms.courses_default_room && Array.isArray(rooms.courses_default_room) &&
                              rooms.courses_default_room.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/courses/courses-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="course_code">
                                        { item.course_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="course_status">
                                        { item.course_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!rooms?.courses_default_room?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default RoomsView;