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/rooms/roomsSlice'
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 RoomsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { rooms } = useAppSelector((state) => state.rooms)

    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 rooms')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View rooms')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/rooms/rooms-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>

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

                        <p>{rooms?.property?.name ?? 'No data'}</p>

                </div>

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

                        <p>{rooms?.room_type?.name ?? 'No data'}</p>

                </div>

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

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Floor</p>
                  <p>{rooms?.floor || 'No data'}</p>
                </div>

                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={rooms?.notes} />
                </FormField>

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

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

                </div>

                <>
                    <p className={'block font-bold mb-2'}>Beds Room</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>IsBunk</th>

                                <th>IsLower</th>

                                <th>IsActive</th>

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

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

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

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

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

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

                <>
                    <p className={'block font-bold mb-2'}>Housekeeping_tasks Room</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>
                            {rooms.housekeeping_tasks_room && Array.isArray(rooms.housekeeping_tasks_room) &&
                              rooms.housekeeping_tasks_room.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>
                        {!rooms?.housekeeping_tasks_room?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Maintenance_tickets Room</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>
                            {rooms.maintenance_tickets_room && Array.isArray(rooms.maintenance_tickets_room) &&
                              rooms.maintenance_tickets_room.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>
                        {!rooms?.maintenance_tickets_room?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <BaseDivider />

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

RoomsView.getLayout = function getLayout(page: ReactElement) {
    return (
      <LayoutAuthenticated>
          {page}
      </LayoutAuthenticated>
    )
}

export default RoomsView;
