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/units/unitsSlice'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";
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";

import {hasPermission} from "../../helpers/userPermissions";


const UnitsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { units } = useAppSelector((state) => state.units)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Property</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{units?.property?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Unittype</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{units?.unit_type?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Unitnumber</p>
                    <p>{units?.unit_number}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Maxoccupancyoverride</p>
                  <p>{units?.max_occupancy_override || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>Availabilityblocks</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>Blocktype</th>
                                
                                
                                
                                    <th>Startat</th>
                                
                                
                                
                                    <th>Endat</th>
                                
                                
                                
                                    <th>Reason</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {units.availability_blocks && Array.isArray(units.availability_blocks) &&
                              units.availability_blocks.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/unit_availability_blocks/unit_availability_blocks-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    
                                    
                                    <td data-label="block_type">
                                        { item.block_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="start_at">
                                        { dataFormatter.dateTimeFormatter(item.start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="end_at">
                                        { dataFormatter.dateTimeFormatter(item.end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reason">
                                        { item.reason }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!units?.availability_blocks?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>organizations</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{units?.organizations?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Unit_availability_blocks Unit</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Blocktype</th>
                                
                                
                                
                                <th>Startat</th>
                                
                                
                                
                                <th>Endat</th>
                                
                                
                                
                                <th>Reason</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {units.unit_availability_blocks_unit && Array.isArray(units.unit_availability_blocks_unit) &&
                              units.unit_availability_blocks_unit.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/unit_availability_blocks/unit_availability_blocks-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="block_type">
                                        { item.block_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="start_at">
                                        { dataFormatter.dateTimeFormatter(item.start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="end_at">
                                        { dataFormatter.dateTimeFormatter(item.end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reason">
                                        { item.reason }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!units?.unit_availability_blocks_unit?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Reservations Unit</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Reservationcode</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Check-inat</th>
                                
                                
                                
                                <th>Check-outat</th>
                                
                                
                                
                                <th>Actualcheck-inat</th>
                                
                                
                                
                                <th>Actualcheck-outat</th>
                                
                                
                                
                                <th>Earlycheck-in</th>
                                
                                
                                
                                <th>Latecheck-out</th>
                                
                                
                                
                                <th>Guestcount</th>
                                
                                
                                
                                <th>Nightlyrate</th>
                                
                                
                                
                                <th>Monthlyrate</th>
                                
                                
                                
                                <th>Currency</th>
                                
                                
                                
                                <th>Internalnotes</th>
                                
                                
                                
                                <th>Externalnotes</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {units.reservations_unit && Array.isArray(units.reservations_unit) &&
                              units.reservations_unit.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/reservations/reservations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="reservation_code">
                                        { item.reservation_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="check_in_at">
                                        { dataFormatter.dateTimeFormatter(item.check_in_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="check_out_at">
                                        { dataFormatter.dateTimeFormatter(item.check_out_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="actual_check_in_at">
                                        { dataFormatter.dateTimeFormatter(item.actual_check_in_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="actual_check_out_at">
                                        { dataFormatter.dateTimeFormatter(item.actual_check_out_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="early_check_in">
                                        { dataFormatter.booleanFormatter(item.early_check_in) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="late_check_out">
                                        { dataFormatter.booleanFormatter(item.late_check_out) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="guest_count">
                                        { item.guest_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="nightly_rate">
                                        { item.nightly_rate }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="monthly_rate">
                                        { item.monthly_rate }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currency">
                                        { item.currency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="internal_notes">
                                        { item.internal_notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="external_notes">
                                        { item.external_notes }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!units?.reservations_unit?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

UnitsView.getLayout = function getLayout(page: ReactElement) {
    return (
      <LayoutAuthenticated
        
        permission={'READ_UNITS'}
        
      >
          {page}
      </LayoutAuthenticated>
    )
}

export default UnitsView;