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'}>Doctor_profiles User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>DoctorType</th>
                                
                                
                                
                                
                                
                                <th>MedicalLicenseNumber</th>
                                
                                
                                
                                <th>Specialization</th>
                                
                                
                                
                                <th>Qualification</th>
                                
                                
                                
                                <th>YearsOfExperience</th>
                                
                                
                                
                                <th>AvailabilityStatus</th>
                                
                                
                                
                                <th>ConsultationFeeNote</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.doctor_profiles_user && Array.isArray(users.doctor_profiles_user) &&
                              users.doctor_profiles_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/doctor_profiles/doctor_profiles-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="doctor_type">
                                        { item.doctor_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="medical_license_number">
                                        { item.medical_license_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="specialization">
                                        { item.specialization }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="qualification">
                                        { item.qualification }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="years_of_experience">
                                        { item.years_of_experience }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="availability_status">
                                        { item.availability_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="consultation_fee_note">
                                        { item.consultation_fee_note }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.doctor_profiles_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Patient_profiles User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>PatientIdentifier</th>
                                
                                
                                
                                <th>Gender</th>
                                
                                
                                
                                <th>DateOfBirth</th>
                                
                                
                                
                                <th>BloodGroup</th>
                                
                                
                                
                                <th>Address</th>
                                
                                
                                
                                <th>Village</th>
                                
                                
                                
                                <th>District</th>
                                
                                
                                
                                <th>State</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>ConsentForTelemedicine</th>
                                
                                
                                
                                <th>ConsentForNotifications</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.patient_profiles_user && Array.isArray(users.patient_profiles_user) &&
                              users.patient_profiles_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/patient_profiles/patient_profiles-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="patient_identifier">
                                        { item.patient_identifier }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="gender">
                                        { item.gender }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="date_of_birth">
                                        { dataFormatter.dateTimeFormatter(item.date_of_birth) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="blood_group">
                                        { item.blood_group }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="address_line">
                                        { item.address_line }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="village">
                                        { item.village }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="district">
                                        { item.district }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="state">
                                        { item.state }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="consent_for_telemedicine">
                                        { dataFormatter.booleanFormatter(item.consent_for_telemedicine) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="consent_for_notifications">
                                        { dataFormatter.booleanFormatter(item.consent_for_notifications) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.patient_profiles_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Chat_sessions CreatedByUser</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>SessionType</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>SessionStatus</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>EndedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.chat_sessions_created_by_user && Array.isArray(users.chat_sessions_created_by_user) &&
                              users.chat_sessions_created_by_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/chat_sessions/chat_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="session_type">
                                        { item.session_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="session_status">
                                        { item.session_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ended_at">
                                        { dataFormatter.dateTimeFormatter(item.ended_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.chat_sessions_created_by_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Messages SenderUser</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>SenderType</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>SentAt</th>
                                
                                
                                
                                <th>IsRead</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.messages_sender_user && Array.isArray(users.messages_sender_user) &&
                              users.messages_sender_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/messages/messages-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sender_type">
                                        { item.sender_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sent_at">
                                        { dataFormatter.dateTimeFormatter(item.sent_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_read">
                                        { dataFormatter.booleanFormatter(item.is_read) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.messages_sender_user?.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;