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/beds/bedsSlice'
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 BedsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { beds } = useAppSelector((state) => state.beds)

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

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

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

                </div>

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

                        <p>{beds?.room?.name ?? 'No data'}</p>

                </div>

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

                <FormField label='IsBunk'>
                    <SwitchField
                      field={{name: 'is_bunk', value: beds?.is_bunk}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>

                <FormField label='IsLower'>
                    <SwitchField
                      field={{name: 'is_lower', value: beds?.is_lower}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>

                <FormField label='IsActive'>
                    <SwitchField
                      field={{name: 'is_active', value: beds?.is_active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>

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

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

                </div>

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

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

                <BaseDivider />

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

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

export default BedsView;
