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/locations/locationsSlice'
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 LocationsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { locations } = useAppSelector((state) => state.locations)
    
    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 locations')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View locations')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/locations/locations-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Tenant</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{locations?.tenant?.tenant_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>LocationName</p>
                    <p>{locations?.location_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AddressLine1</p>
                    <p>{locations?.address_line_1}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AddressLine2</p>
                    <p>{locations?.address_line_2}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>City</p>
                    <p>{locations?.city}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Region</p>
                    <p>{locations?.region}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PostalCode</p>
                    <p>{locations?.postal_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Country</p>
                    <p>{locations?.country}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Phone</p>
                    <p>{locations?.phone}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Registers Location</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>RegisterName</th>
                                
                                
                                
                                <th>DeviceLabel</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {locations.registers_location && Array.isArray(locations.registers_location) &&
                              locations.registers_location.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/registers/registers-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="register_name">
                                        { item.register_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="device_label">
                                        { item.device_label }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!locations?.registers_location?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Carts Location</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>CartStatus</th>
                                
                                
                                
                                <th>SubtotalAmount</th>
                                
                                
                                
                                <th>DiscountAmount</th>
                                
                                
                                
                                <th>TaxAmount</th>
                                
                                
                                
                                <th>TotalAmount</th>
                                
                                
                                
                                <th>HoldReason</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {locations.carts_location && Array.isArray(locations.carts_location) &&
                              locations.carts_location.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/carts/carts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="cart_status">
                                        { item.cart_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal_amount">
                                        { item.subtotal_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="discount_amount">
                                        { item.discount_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_amount">
                                        { item.tax_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_amount">
                                        { item.total_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="hold_reason">
                                        { item.hold_reason }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!locations?.carts_location?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Orders Location</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>OrderNumber</th>
                                
                                
                                
                                <th>OrderStatus</th>
                                
                                
                                
                                <th>PlacedAt</th>
                                
                                
                                
                                <th>SubtotalAmount</th>
                                
                                
                                
                                <th>DiscountAmount</th>
                                
                                
                                
                                <th>TaxAmount</th>
                                
                                
                                
                                <th>TipAmount</th>
                                
                                
                                
                                <th>TotalAmount</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {locations.orders_location && Array.isArray(locations.orders_location) &&
                              locations.orders_location.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/orders/orders-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="order_number">
                                        { item.order_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="order_status">
                                        { item.order_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="placed_at">
                                        { dataFormatter.dateTimeFormatter(item.placed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal_amount">
                                        { item.subtotal_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="discount_amount">
                                        { item.discount_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_amount">
                                        { item.tax_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tip_amount">
                                        { item.tip_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_amount">
                                        { item.total_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!locations?.orders_location?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Inventory_items Location</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>OnHandQuantity</th>
                                
                                
                                
                                <th>ReorderPointQuantity</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {locations.inventory_items_location && Array.isArray(locations.inventory_items_location) &&
                              locations.inventory_items_location.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/inventory_items/inventory_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="on_hand_quantity">
                                        { item.on_hand_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reorder_point_quantity">
                                        { item.reorder_point_quantity }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!locations?.inventory_items_location?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Reports Location</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>ReportType</th>
                                
                                
                                
                                <th>ReportName</th>
                                
                                
                                
                                <th>PeriodStart</th>
                                
                                
                                
                                <th>PeriodEnd</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {locations.reports_location && Array.isArray(locations.reports_location) &&
                              locations.reports_location.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/reports/reports-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="report_type">
                                        { item.report_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="report_name">
                                        { item.report_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="period_start">
                                        { dataFormatter.dateTimeFormatter(item.period_start) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="period_end">
                                        { dataFormatter.dateTimeFormatter(item.period_end) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!locations?.reports_location?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

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

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

export default LocationsView;