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

              

              

              

              

              

              

              

              

              

              
                {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Organization</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{teams?.organization?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                  } 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TeamStatus</p>
                    <p>{teams?.team_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>AlwaysOnStateRetentionDays</p>
                  <p>{teams?.always_on_state_retention_days || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Memberships Team</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Role</th>
                                
                                
                                
                                <th>MembershipStatus</th>
                                
                                
                                
                                <th>JoinedAt</th>
                                
                                
                                
                                <th>InvitedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {teams.memberships_team && Array.isArray(teams.memberships_team) &&
                              teams.memberships_team.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/memberships/memberships-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="role">
                                        { item.role }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="membership_status">
                                        { item.membership_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="joined_at">
                                        { dataFormatter.dateTimeFormatter(item.joined_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="invited_at">
                                        { dataFormatter.dateTimeFormatter(item.invited_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!teams?.memberships_team?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Conference_templates Team</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>TemplateName</th>
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                <th>E2EEEnabled</th>
                                
                                
                                
                                <th>LobbyEnabled</th>
                                
                                
                                
                                <th>RecordingEnabled</th>
                                
                                
                                
                                <th>TranscriptionEnabled</th>
                                
                                
                                
                                <th>CanvasEnabled</th>
                                
                                
                                
                                <th>DefaultLayoutMode</th>
                                
                                
                                
                                <th>MaxParticipants</th>
                                
                                
                                
                                <th>TemplateStatus</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {teams.conference_templates_team && Array.isArray(teams.conference_templates_team) &&
                              teams.conference_templates_team.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/conference_templates/conference_templates-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="e2ee_enabled">
                                        { dataFormatter.booleanFormatter(item.e2ee_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lobby_enabled">
                                        { dataFormatter.booleanFormatter(item.lobby_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="recording_enabled">
                                        { dataFormatter.booleanFormatter(item.recording_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="transcription_enabled">
                                        { dataFormatter.booleanFormatter(item.transcription_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="canvas_enabled">
                                        { dataFormatter.booleanFormatter(item.canvas_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="default_layout_mode">
                                        { item.default_layout_mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_participants">
                                        { item.max_participants }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="template_status">
                                        { item.template_status }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!teams?.conference_templates_team?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Rooms Team</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>RoomName</th>
                                
                                
                                
                                <th>RoomKey</th>
                                
                                
                                
                                <th>RoomType</th>
                                
                                
                                
                                <th>AccessLevel</th>
                                
                                
                                
                                <th>E2EEEnabled</th>
                                
                                
                                
                                <th>RecordingEnabled</th>
                                
                                
                                
                                <th>TranscriptionEnabled</th>
                                
                                
                                
                                <th>CanvasEnabled</th>
                                
                                
                                
                                <th>ChatEnabled</th>
                                
                                
                                
                                <th>IsPersistent</th>
                                
                                
                                
                                <th>LastActiveAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {teams.rooms_team && Array.isArray(teams.rooms_team) &&
                              teams.rooms_team.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/rooms/rooms-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="room_key">
                                        { item.room_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="room_type">
                                        { item.room_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="access_level">
                                        { item.access_level }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="e2ee_enabled">
                                        { dataFormatter.booleanFormatter(item.e2ee_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="recording_enabled">
                                        { dataFormatter.booleanFormatter(item.recording_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="transcription_enabled">
                                        { dataFormatter.booleanFormatter(item.transcription_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="canvas_enabled">
                                        { dataFormatter.booleanFormatter(item.canvas_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="chat_enabled">
                                        { dataFormatter.booleanFormatter(item.chat_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_persistent">
                                        { dataFormatter.booleanFormatter(item.is_persistent) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_active_at">
                                        { dataFormatter.dateTimeFormatter(item.last_active_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!teams?.rooms_team?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>App_installations Team</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>InstallationStatus</th>
                                
                                
                                
                                <th>InstalledAt</th>
                                
                                
                                
                                <th>DisabledAt</th>
                                
                                
                                
                                <th>DisableReason</th>
                                
                                
                                
                                <th>AutoSuggestEnabled</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {teams.app_installations_team && Array.isArray(teams.app_installations_team) &&
                              teams.app_installations_team.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/app_installations/app_installations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="installation_status">
                                        { item.installation_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="installed_at">
                                        { dataFormatter.dateTimeFormatter(item.installed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="disabled_at">
                                        { dataFormatter.dateTimeFormatter(item.disabled_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="disable_reason">
                                        { item.disable_reason }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="auto_suggest_enabled">
                                        { dataFormatter.booleanFormatter(item.auto_suggest_enabled) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!teams?.app_installations_team?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default TeamsView;