import React, { ReactElement, useEffect } from 'react';
import Head from 'next/head'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";
import {useAppDispatch, useAppSelector} from "../../stores/hooks";
import {useRouter} from "next/router";
import { fetch } from '../../stores/users/usersSlice'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";
import LayoutAuthenticated from "../../layouts/Authenticated";
import {getPageTitle} from "../../config";
import SectionTitleLineWithButton from "../../components/SectionTitleLineWithButton";
import SectionMain from "../../components/SectionMain";
import CardBox from "../../components/CardBox";
import BaseButton from "../../components/BaseButton";
import BaseDivider from "../../components/BaseDivider";
import {mdiChartTimelineVariant} from "@mdi/js";
import {SwitchField} from "../../components/SwitchField";
import FormField from "../../components/FormField";

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


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

    const { id } = router.query;
    
    function removeLastCharacter(str) {
      console.log(str,`str`)
      return str.slice(0, -1);
    }

    useEffect(() => {
        dispatch(fetch({ id }));
    }, [dispatch, id]);


    return (
      <>
          <Head>
              <title>{getPageTitle('View users')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View users')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/users/users-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>First Name</p>
                    <p>{users?.firstName}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Last Name</p>
                    <p>{users?.lastName}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Phone Number</p>
                    <p>{users?.phoneNumber}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>E-Mail</p>
                    <p>{users?.email}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>Custom Permissions</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                    <th>Name</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.custom_permissions && Array.isArray(users.custom_permissions) &&
                              users.custom_permissions.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/permissions/permissions-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                  
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.custom_permissions?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Workspaces Owner</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>WorkspaceName</th>
                                
                                
                                
                                <th>LegalName</th>
                                
                                
                                
                                <th>Website</th>
                                
                                
                                
                                <th>Industry</th>
                                
                                
                                
                                <th>Country</th>
                                
                                
                                
                                <th>BaseCurrency</th>
                                
                                
                                
                                <th>OnboardedAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.workspaces_owner && Array.isArray(users.workspaces_owner) &&
                              users.workspaces_owner.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/workspaces/workspaces-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="legal_name">
                                        { item.legal_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="website">
                                        { item.website }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="industry">
                                        { item.industry }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="country">
                                        { item.country }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="base_currency">
                                        { item.base_currency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="onboarded_at">
                                        { dataFormatter.dateTimeFormatter(item.onboarded_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.workspaces_owner?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Workspace_memberships User</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>InviteStatus</th>
                                
                                
                                
                                <th>InvitedAt</th>
                                
                                
                                
                                <th>AcceptedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.workspace_memberships_user && Array.isArray(users.workspace_memberships_user) &&
                              users.workspace_memberships_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/workspace_memberships/workspace_memberships-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="member_role">
                                        { item.member_role }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="invite_status">
                                        { item.invite_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="invited_at">
                                        { dataFormatter.dateTimeFormatter(item.invited_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="accepted_at">
                                        { dataFormatter.dateTimeFormatter(item.accepted_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.workspace_memberships_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Recommendations AssignedTo</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>RecommendationType</th>
                                
                                
                                
                                <th>Priority</th>
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>Rationale</th>
                                
                                
                                
                                <th>ActionSteps</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>CreatedOn</th>
                                
                                
                                
                                <th>DueOn</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.recommendations_assigned_to && Array.isArray(users.recommendations_assigned_to) &&
                              users.recommendations_assigned_to.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/recommendations/recommendations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="recommendation_type">
                                        { item.recommendation_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="priority">
                                        { item.priority }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title_text">
                                        { item.title_text }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rationale">
                                        { item.rationale }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="action_steps">
                                        { item.action_steps }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="created_on">
                                        { dataFormatter.dateTimeFormatter(item.created_on) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_on">
                                        { dataFormatter.dateTimeFormatter(item.due_on) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.recommendations_assigned_to?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Chat_threads StartedBy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>ThreadTitle</th>
                                
                                
                                
                                <th>ThreadType</th>
                                
                                
                                
                                <th>LastMessageAt</th>
                                
                                
                                
                                <th>IsPinned</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.chat_threads_started_by && Array.isArray(users.chat_threads_started_by) &&
                              users.chat_threads_started_by.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/chat_threads/chat_threads-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="thread_title">
                                        { item.thread_title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="thread_type">
                                        { item.thread_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_message_at">
                                        { dataFormatter.dateTimeFormatter(item.last_message_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_pinned">
                                        { dataFormatter.booleanFormatter(item.is_pinned) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.chat_threads_started_by?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Chat_messages AuthorUser</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>AuthorType</th>
                                
                                
                                
                                
                                
                                <th>SentAt</th>
                                
                                
                                
                                <th>MessageKind</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.chat_messages_author_user && Array.isArray(users.chat_messages_author_user) &&
                              users.chat_messages_author_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/chat_messages/chat_messages-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="author_type">
                                        { item.author_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sent_at">
                                        { dataFormatter.dateTimeFormatter(item.sent_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="message_kind">
                                        { item.message_kind }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.chat_messages_author_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Files_library UploadedBy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>FileType</th>
                                
                                
                                
                                <th>FileName</th>
                                
                                
                                
                                
                                
                                <th>UploadedAt</th>
                                
                                
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.files_library_uploaded_by && Array.isArray(users.files_library_uploaded_by) &&
                              users.files_library_uploaded_by.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/files_library/files_library-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="file_type">
                                        { item.file_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="file_name">
                                        { item.file_name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="uploaded_at">
                                        { dataFormatter.dateTimeFormatter(item.uploaded_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.files_library_uploaded_by?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

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

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

export default UsersView;