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 dataFormatter from '../../helpers/dataFormatter';
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'}>App Role</p>

                        <p>{users?.app_role?.name ?? 'No data'}</p>

                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Properties</p>

                        <p>{users?.properties?.name ?? 'No data'}</p>

                </div>

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

                                <th>EntityID</th>

                                <th>DiffJSON</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">
                                        { item.entity }
                                    </td>

                                    <td data-label="entity_id">
                                        { item.entity_id }
                                    </td>

                                    <td data-label="diff_json">
                                        { item.diff_json }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.audit_logs_actor_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Bookings User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>

                                <th>Code</th>

                                <th>Status</th>

                                <th>Check-inDate</th>

                                <th>Check-outDate</th>

                                <th>GuestsCount</th>

                                <th>TotalAmount</th>

                                <th>Currency</th>

                                <th>PaymentStatus</th>

                                <th>StripePaymentIntentID</th>

                            </tr>
                            </thead>
                            <tbody>
                            {users.bookings_user && Array.isArray(users.bookings_user) &&
                              users.bookings_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/bookings/bookings-view/?id=${item.id}`)}>

                                    <td data-label="code">
                                        { item.code }
                                    </td>

                                    <td data-label="status">
                                        { item.status }
                                    </td>

                                    <td data-label="checkin_date">
                                        { dataFormatter.dateTimeFormatter(item.checkin_date) }
                                    </td>

                                    <td data-label="checkout_date">
                                        { dataFormatter.dateTimeFormatter(item.checkout_date) }
                                    </td>

                                    <td data-label="guests_count">
                                        { item.guests_count }
                                    </td>

                                    <td data-label="total_amount">
                                        { item.total_amount }
                                    </td>

                                    <td data-label="currency">
                                        { item.currency }
                                    </td>

                                    <td data-label="payment_status">
                                        { item.payment_status }
                                    </td>

                                    <td data-label="stripe_payment_intent_id">
                                        { item.stripe_payment_intent_id }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.bookings_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Event_orders User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>

                                <th>TotalAmount</th>

                                <th>Currency</th>

                                <th>PaymentStatus</th>

                                <th>StripePaymentIntentID</th>

                            </tr>
                            </thead>
                            <tbody>
                            {users.event_orders_user && Array.isArray(users.event_orders_user) &&
                              users.event_orders_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/event_orders/event_orders-view/?id=${item.id}`)}>

                                    <td data-label="total_amount">
                                        { item.total_amount }
                                    </td>

                                    <td data-label="currency">
                                        { item.currency }
                                    </td>

                                    <td data-label="payment_status">
                                        { item.payment_status }
                                    </td>

                                    <td data-label="stripe_payment_intent_id">
                                        { item.stripe_payment_intent_id }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.event_orders_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Housekeeping_tasks AssigneeUser</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>

                                <th>DueDate</th>

                                <th>TaskType</th>

                                <th>Status</th>

                                <th>Notes</th>

                            </tr>
                            </thead>
                            <tbody>
                            {users.housekeeping_tasks_assignee_user && Array.isArray(users.housekeeping_tasks_assignee_user) &&
                              users.housekeeping_tasks_assignee_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/housekeeping_tasks/housekeeping_tasks-view/?id=${item.id}`)}>

                                    <td data-label="due_date">
                                        { dataFormatter.dateTimeFormatter(item.due_date) }
                                    </td>

                                    <td data-label="task_type">
                                        { item.task_type }
                                    </td>

                                    <td data-label="status">
                                        { item.status }
                                    </td>

                                    <td data-label="notes">
                                        { item.notes }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.housekeeping_tasks_assignee_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Maintenance_tickets CreatedByUser</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>

                                <th>Priority</th>

                                <th>Status</th>

                                <th>Title</th>

                                <th>Description</th>

                                <th>PhotosJSON</th>

                            </tr>
                            </thead>
                            <tbody>
                            {users.maintenance_tickets_created_by_user && Array.isArray(users.maintenance_tickets_created_by_user) &&
                              users.maintenance_tickets_created_by_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/maintenance_tickets/maintenance_tickets-view/?id=${item.id}`)}>

                                    <td data-label="priority">
                                        { item.priority }
                                    </td>

                                    <td data-label="status">
                                        { item.status }
                                    </td>

                                    <td data-label="title">
                                        { item.title }
                                    </td>

                                    <td data-label="description">
                                        { item.description }
                                    </td>

                                    <td data-label="photos_json">
                                        { item.photos_json }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.maintenance_tickets_created_by_user?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Message_threads UserGuest</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>

                                <th>Subject</th>

                            </tr>
                            </thead>
                            <tbody>
                            {users.message_threads_user_guest && Array.isArray(users.message_threads_user_guest) &&
                              users.message_threads_user_guest.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/message_threads/message_threads-view/?id=${item.id}`)}>

                                    <td data-label="subject">
                                        { item.subject }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.message_threads_user_guest?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>User_roles User</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>

                            </tr>
                            </thead>
                            <tbody>
                            {users.user_roles_user && Array.isArray(users.user_roles_user) &&
                              users.user_roles_user.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/user_roles/user_roles-view/?id=${item.id}`)}>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!users?.user_roles_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>
          {page}
      </LayoutAuthenticated>
    )
}

export default UsersView;
