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/upsells/upsellsSlice'
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 UpsellsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { upsells } = useAppSelector((state) => state.upsells)

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

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

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

                </div>

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

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

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Price</p>
                  <p>{upsells?.price || 'No data'}</p>
                </div>

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

                <FormField label='Taxable'>
                    <SwitchField
                      field={{name: 'taxable', value: upsells?.taxable}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>

                <FormField label='RequiresDateTime'>
                    <SwitchField
                      field={{name: 'requires_date_time', value: upsells?.requires_date_time}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Stock</p>
                  <p>{upsells?.stock || 'No data'}</p>
                </div>

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

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

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

                </div>

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

                <BaseDivider />

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

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

export default UpsellsView;
