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/room_types/room_typesSlice'
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 Room_typesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { room_types } = useAppSelector((state) => state.room_types)

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

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

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

                </div>

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

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

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Capacity</p>
                  <p>{room_types?.capacity || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>GenderPolicy</p>
                    <p>{room_types?.gender_policy ?? 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AmenitiesJSON</p>
                    <p>{room_types?.amenities_json}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>BasePrice</p>
                  <p>{room_types?.base_price || 'No data'}</p>
                </div>

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

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

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

                </div>

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

                                <th>Date</th>

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

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

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

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

                                <th>Date</th>

                                <th>AllotedBeds</th>

                                <th>ClosedforArrival</th>

                                <th>ClosedforDeparture</th>

                                <th>OverridePrice</th>

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

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

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

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

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

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

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

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

                                <th>Name</th>

                                <th>Floor</th>

                                <th>Notes</th>

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

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

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

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

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

                <BaseDivider />

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

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

export default Room_typesView;
