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";


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

    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>
                </>
              

              

              
              

                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Admins User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>FullName</th>
                                
                                
                                
                                <th>GmailAddress</th>
                                
                                
                                
                                <th>AccessLevel</th>
                                
                                
                                
                                <th>Department</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.admins_user && Array.isArray(users.admins_user) &&
                              users.admins_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/admins/admins-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="full_name">
                                        { item.full_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="gmail_address">
                                        { item.gmail_address }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="access_level">
                                        { item.access_level }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="department">
                                        { item.department }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.admins_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>City_doctors User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>FullName</th>
                                
                                
                                
                                <th>HospitalCode</th>
                                
                                
                                
                                <th>DoctorRegistrationNo</th>
                                
                                
                                
                                <th>Specialization</th>
                                
                                
                                
                                <th>YearsExperience</th>
                                
                                
                                
                                <th>Qualification</th>
                                
                                
                                
                                <th>AvailabilityStatus</th>
                                
                                
                                
                                <th>ConsultationFee</th>
                                
                                
                                
                                <th>ClinicAddress</th>
                                
                                
                                
                                <th>ClinicLatitude</th>
                                
                                
                                
                                <th>ClinicLongitude</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.city_doctors_user && Array.isArray(users.city_doctors_user) &&
                              users.city_doctors_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/city_doctors/city_doctors-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="full_name">
                                        { item.full_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="hospital_code">
                                        { item.hospital_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="doctor_registration_no">
                                        { item.doctor_registration_no }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="specialization">
                                        { item.specialization }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="years_experience">
                                        { item.years_experience }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="qualification">
                                        { item.qualification }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="availability_status">
                                        { item.availability_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="consultation_fee">
                                        { item.consultation_fee }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="clinic_address">
                                        { item.clinic_address }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="clinic_latitude">
                                        { item.clinic_latitude }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="clinic_longitude">
                                        { item.clinic_longitude }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.city_doctors_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Rural_doctors User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>FullName</th>
                                
                                
                                
                                <th>DHOCode</th>
                                
                                
                                
                                <th>DoctorRegistrationNo</th>
                                
                                
                                
                                <th>AssignedRegion</th>
                                
                                
                                
                                <th>SubcenterName</th>
                                
                                
                                
                                <th>SubcenterLatitude</th>
                                
                                
                                
                                <th>SubcenterLongitude</th>
                                
                                
                                
                                <th>AvailabilityStatus</th>
                                
                                
                                
                                <th>CanRegisterPatients</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.rural_doctors_user && Array.isArray(users.rural_doctors_user) &&
                              users.rural_doctors_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/rural_doctors/rural_doctors-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="full_name">
                                        { item.full_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="dho_code">
                                        { item.dho_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="doctor_registration_no">
                                        { item.doctor_registration_no }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="assigned_region">
                                        { item.assigned_region }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subcenter_name">
                                        { item.subcenter_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subcenter_latitude">
                                        { item.subcenter_latitude }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subcenter_longitude">
                                        { item.subcenter_longitude }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="availability_status">
                                        { item.availability_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="can_register_patients">
                                        { dataFormatter.booleanFormatter(item.can_register_patients) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.rural_doctors_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Patients User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>PatientUID</th>
                                
                                
                                
                                <th>FullName</th>
                                
                                
                                
                                <th>Gender</th>
                                
                                
                                
                                <th>DateOfBirth</th>
                                
                                
                                
                                <th>BloodGroup</th>
                                
                                
                                
                                <th>Allergies</th>
                                
                                
                                
                                <th>ChronicConditions</th>
                                
                                
                                
                                <th>EmergencyContactName</th>
                                
                                
                                
                                <th>EmergencyContactPhone</th>
                                
                                
                                
                                
                                
                                <th>RegisteredAt</th>
                                
                                
                                
                                <th>AccountStatus</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.patients_user && Array.isArray(users.patients_user) &&
                              users.patients_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/patients/patients-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="patient_uid">
                                        { item.patient_uid }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="full_name">
                                        { item.full_name }
                                    </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="allergies">
                                        { item.allergies }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="chronic_conditions">
                                        { item.chronic_conditions }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="emergency_contact_name">
                                        { item.emergency_contact_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="emergency_contact_phone">
                                        { item.emergency_contact_phone }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="registered_at">
                                        { dataFormatter.dateTimeFormatter(item.registered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="account_status">
                                        { item.account_status }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.patients_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>MessageType</th>
                                
                                
                                
                                <th>MessageText</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="message_type">
                                        { item.message_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="message_text">
                                        { item.message_text }
                                    </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>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Notifications RecipientUser</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>NotificationType</th>
                                
                                
                                
                                <th>TitleText</th>
                                
                                
                                
                                <th>BodyText</th>
                                
                                
                                
                                <th>Priority</th>
                                
                                
                                
                                <th>IsRead</th>
                                
                                
                                
                                <th>SentAt</th>
                                
                                
                                
                                <th>ActionURL</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.notifications_recipient_user && Array.isArray(users.notifications_recipient_user) &&
                              users.notifications_recipient_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/notifications/notifications-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="notification_type">
                                        { item.notification_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title_text">
                                        { item.title_text }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="body_text">
                                        { item.body_text }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="priority">
                                        { item.priority }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_read">
                                        { dataFormatter.booleanFormatter(item.is_read) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sent_at">
                                        { dataFormatter.dateTimeFormatter(item.sent_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="action_url">
                                        { item.action_url }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.notifications_recipient_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Audit_logs ActorUser</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Action</th>
                                
                                
                                
                                <th>EntityName</th>
                                
                                
                                
                                <th>EntityReference</th>
                                
                                
                                
                                <th>ActedAt</th>
                                
                                
                                
                                <th>IPAddress</th>
                                
                                
                                
                                <th>UserAgent</th>
                                
                                
                                
                                <th>Details</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {users.audit_logs_actor_user && Array.isArray(users.audit_logs_actor_user) &&
                              users.audit_logs_actor_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/audit_logs/audit_logs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="action">
                                        { item.action }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="entity_name">
                                        { item.entity_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="entity_reference">
                                        { item.entity_reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="acted_at">
                                        { dataFormatter.dateTimeFormatter(item.acted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ip_address">
                                        { item.ip_address }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="user_agent">
                                        { item.user_agent }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="details">
                                        { item.details }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.audit_logs_actor_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;