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/bookings/bookingsSlice'
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 BookingsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { bookings } = useAppSelector((state) => state.bookings)

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

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

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

                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Code</p>
                    <p>{bookings?.code}</p>
                </div>

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

                        <p>{bookings?.user?.firstName ?? 'No data'}</p>

                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{bookings?.status ?? 'No data'}</p>
                </div>

                <FormField label='Check-inDate'>
                    {bookings.checkin_date ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={bookings.checkin_date ?
                        new Date(
                          dayjs(bookings.checkin_date).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No Check-inDate</p>}
                </FormField>

                <FormField label='Check-outDate'>
                    {bookings.checkout_date ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={bookings.checkout_date ?
                        new Date(
                          dayjs(bookings.checkout_date).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No Check-outDate</p>}
                </FormField>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>GuestsCount</p>
                  <p>{bookings?.guests_count || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TotalAmount</p>
                  <p>{bookings?.total_amount || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Currency</p>
                    <p>{bookings?.currency}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PaymentStatus</p>
                    <p>{bookings?.payment_status ?? 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>StripePaymentIntentID</p>
                    <p>{bookings?.stripe_payment_intent_id}</p>
                </div>

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

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

                </div>

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

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

                                <th>Quantity</th>

                                <th>UnitPrice</th>

                                <th>Currency</th>

                                <th>ScheduledFor</th>

                                <th>Status</th>

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

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

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

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

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

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

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

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

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

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

                                <th>StripePaymentIntentID</th>

                                <th>Amount</th>

                                <th>Currency</th>

                                <th>Status</th>

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

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

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

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

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

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

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

                                <th>Rating</th>

                                <th>Title</th>

                                <th>Body</th>

                                <th>IsPublic</th>

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

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

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

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

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

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

                <BaseDivider />

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

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

export default BookingsView;
