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/restaurant_tables/restaurant_tablesSlice'
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 Restaurant_tablesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { restaurant_tables } = useAppSelector((state) => state.restaurant_tables)
    

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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Nom/Numérodetable</p>
                    <p>{restaurant_tables?.table_label}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Nombredeplaces</p>
                  <p>{restaurant_tables?.seat_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DonnéesQR(payload)</p>
                    <p>{restaurant_tables?.qr_payload}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ImageQRcode</p>
                    {restaurant_tables?.qr_image?.length
                      ? (
                        <ImageField
                          name={'qr_image'}
                          image={restaurant_tables?.qr_image}
                          className='w-20 h-20'
                        />
                      ) : <p>No ImageQRcode</p>
                    }
                </div>
              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Disponible'>
                    <SwitchField
                      field={{name: 'is_available', value: restaurant_tables?.is_available}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Orders Table</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Canal</th>
                                
                                
                                
                                <th>Mode</th>
                                
                                
                                
                                <th>Adressedelivraison</th>
                                
                                
                                
                                <th>Latitudelivraison</th>
                                
                                
                                
                                <th>Longitudelivraison</th>
                                
                                
                                
                                <th>Statutcommande</th>
                                
                                
                                
                                <th>Datedecommande</th>
                                
                                
                                
                                <th>Souhaitéepour</th>
                                
                                
                                
                                <th>Noteclient</th>
                                
                                
                                
                                <th>Sous-total</th>
                                
                                
                                
                                <th>Total</th>
                                
                                
                                
                                <th>Payée</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {restaurant_tables.orders_table && Array.isArray(restaurant_tables.orders_table) &&
                              restaurant_tables.orders_table.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/orders/orders-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="order_channel">
                                        { item.order_channel }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="fulfillment_type">
                                        { item.fulfillment_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="delivery_address">
                                        { item.delivery_address }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="delivery_latitude">
                                        { item.delivery_latitude }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="delivery_longitude">
                                        { item.delivery_longitude }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ordered_at">
                                        { dataFormatter.dateTimeFormatter(item.ordered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requested_for">
                                        { dataFormatter.dateTimeFormatter(item.requested_for) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="customer_note">
                                        { item.customer_note }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal_amount">
                                        { item.subtotal_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_amount">
                                        { item.total_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_paid">
                                        { dataFormatter.booleanFormatter(item.is_paid) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!restaurant_tables?.orders_table?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Reservations Table</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Débutréservation</th>
                                
                                
                                
                                <th>Finréservation</th>
                                
                                
                                
                                <th>Nombredepersonnes</th>
                                
                                
                                
                                <th>Statut</th>
                                
                                
                                
                                <th>Note</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {restaurant_tables.reservations_table && Array.isArray(restaurant_tables.reservations_table) &&
                              restaurant_tables.reservations_table.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/reservations/reservations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="start_time">
                                        { dataFormatter.dateTimeFormatter(item.start_time) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="end_time">
                                        { dataFormatter.dateTimeFormatter(item.end_time) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="party_size">
                                        { item.party_size }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="note">
                                        { item.note }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!restaurant_tables?.reservations_table?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Restaurant_tablesView;