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/rate_plans/rate_plansSlice'
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";


const Rate_plansView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { rate_plans } = useAppSelector((state) => state.rate_plans)
    

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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PricingModel</p>
                    <p>{rate_plans?.pricing_model ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CancellationPolicyType</p>
                    <p>{rate_plans?.cancellation_policy_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CancellationPolicyText</p>
                  {rate_plans.cancellation_policy_text
                    ? <p dangerouslySetInnerHTML={{__html: rate_plans.cancellation_policy_text}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MinStayNights</p>
                  <p>{rate_plans?.min_stay_nights || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MaxStayNights</p>
                  <p>{rate_plans?.max_stay_nights || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ReleaseDays</p>
                  <p>{rate_plans?.release_days || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='BreakfastIncluded'>
                    <SwitchField
                      field={{name: 'is_breakfast_included', value: rate_plans?.is_breakfast_included}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Refundable'>
                    <SwitchField
                      field={{name: 'is_refundable', value: rate_plans?.is_refundable}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Seasonal_rates RatePlan</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>RateName</th>
                                
                                
                                
                                <th>StartAt</th>
                                
                                
                                
                                <th>EndAt</th>
                                
                                
                                
                                <th>PriceAmount</th>
                                
                                
                                
                                <th>Currency</th>
                                
                                
                                
                                <th>MinStayNights</th>
                                
                                
                                
                                <th>MaxStayNights</th>
                                
                                
                                
                                <th>ClosedToArrival</th>
                                
                                
                                
                                <th>ClosedToDeparture</th>
                                
                                
                                
                                <th>Active</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {rate_plans.seasonal_rates_rate_plan && Array.isArray(rate_plans.seasonal_rates_rate_plan) &&
                              rate_plans.seasonal_rates_rate_plan.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/seasonal_rates/seasonal_rates-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </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="price_amount">
                                        { item.price_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currency">
                                        { item.currency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="min_stay_nights">
                                        { item.min_stay_nights }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_stay_nights">
                                        { item.max_stay_nights }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="closed_to_arrival">
                                        { dataFormatter.booleanFormatter(item.closed_to_arrival) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="closed_to_departure">
                                        { dataFormatter.booleanFormatter(item.closed_to_departure) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!rate_plans?.seasonal_rates_rate_plan?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Bookings RatePlan</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>BookingCode</th>
                                
                                
                                
                                <th>Check-inAt</th>
                                
                                
                                
                                <th>Check-outAt</th>
                                
                                
                                
                                <th>Adults</th>
                                
                                
                                
                                <th>Children</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Source</th>
                                
                                
                                
                                <th>TotalAmount</th>
                                
                                
                                
                                <th>TaxAmount</th>
                                
                                
                                
                                <th>CityTaxAmount</th>
                                
                                
                                
                                <th>Currency</th>
                                
                                
                                
                                <th>DepositAmount</th>
                                
                                
                                
                                <th>DepositStatus</th>
                                
                                
                                
                                <th>SpecialRequests</th>
                                
                                
                                
                                <th>CancelledAt</th>
                                
                                
                                
                                <th>CancellationReason</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {rate_plans.bookings_rate_plan && Array.isArray(rate_plans.bookings_rate_plan) &&
                              rate_plans.bookings_rate_plan.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/bookings/bookings-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="booking_code">
                                        { item.booking_code }
                                    </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="adults">
                                        { item.adults }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="children">
                                        { item.children }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="source">
                                        { item.source }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_amount">
                                        { item.total_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_amount">
                                        { item.tax_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="city_tax_amount">
                                        { item.city_tax_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currency">
                                        { item.currency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="deposit_amount">
                                        { item.deposit_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="deposit_status">
                                        { item.deposit_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="special_requests">
                                        { item.special_requests }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="cancelled_at">
                                        { dataFormatter.dateTimeFormatter(item.cancelled_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="cancellation_reason">
                                        { item.cancellation_reason }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!rate_plans?.bookings_rate_plan?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Rate_plansView;