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/market_stores/market_storesSlice'
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 Market_storesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { market_stores } = useAppSelector((state) => state.market_stores)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>StoreStatus</p>
                    <p>{market_stores?.store_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='OpenedAt'>
                    {market_stores.opened_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={market_stores.opened_at ?
                        new Date(
                          dayjs(market_stores.opened_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No OpenedAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='ClosedAt'>
                    {market_stores.closed_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={market_stores.closed_at ?
                        new Date(
                          dayjs(market_stores.closed_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No ClosedAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CashBalance</p>
                  <p>{market_stores?.cash_balance || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PIXBalance</p>
                  <p>{market_stores?.pix_balance || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CardBalance</p>
                  <p>{market_stores?.card_balance || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Employees Store</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>JobTitle</th>
                                
                                
                                
                                <th>EmploymentStatus</th>
                                
                                
                                
                                <th>DailyWage</th>
                                
                                
                                
                                <th>HiredAt</th>
                                
                                
                                
                                <th>CanOperateRegister</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {market_stores.employees_store && Array.isArray(market_stores.employees_store) &&
                              market_stores.employees_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/employees/employees-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="job_title">
                                        { item.job_title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="employment_status">
                                        { item.employment_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="daily_wage">
                                        { item.daily_wage }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="hired_at">
                                        { dataFormatter.dateTimeFormatter(item.hired_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="can_operate_register">
                                        { dataFormatter.booleanFormatter(item.can_operate_register) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!market_stores?.employees_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Store_products Store</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>CurrentPrice</th>
                                
                                
                                
                                <th>PricingMode</th>
                                
                                
                                
                                <th>ReorderPoint</th>
                                
                                
                                
                                <th>TargetStock</th>
                                
                                
                                
                                <th>IsListed</th>
                                
                                
                                
                                <th>TaxRate</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {market_stores.store_products_store && Array.isArray(market_stores.store_products_store) &&
                              market_stores.store_products_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/store_products/store_products-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="current_price">
                                        { item.current_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="pricing_mode">
                                        { item.pricing_mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reorder_point">
                                        { item.reorder_point }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_stock">
                                        { item.target_stock }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_listed">
                                        { dataFormatter.booleanFormatter(item.is_listed) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_rate">
                                        { item.tax_rate }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!market_stores?.store_products_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Purchase_orders Store</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>OrderedAt</th>
                                
                                
                                
                                <th>ExpectedAt</th>
                                
                                
                                
                                <th>SubtotalAmount</th>
                                
                                
                                
                                <th>TaxAmount</th>
                                
                                
                                
                                <th>TotalAmount</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {market_stores.purchase_orders_store && Array.isArray(market_stores.purchase_orders_store) &&
                              market_stores.purchase_orders_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/purchase_orders/purchase_orders-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ordered_at">
                                        { dataFormatter.dateTimeFormatter(item.ordered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expected_at">
                                        { dataFormatter.dateTimeFormatter(item.expected_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal_amount">
                                        { item.subtotal_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_amount">
                                        { item.tax_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_amount">
                                        { item.total_amount }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!market_stores?.purchase_orders_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Deliveries Store</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>DeliveryStatus</th>
                                
                                
                                
                                <th>DepartedAt</th>
                                
                                
                                
                                <th>ArrivedAt</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {market_stores.deliveries_store && Array.isArray(market_stores.deliveries_store) &&
                              market_stores.deliveries_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/deliveries/deliveries-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="delivery_status">
                                        { item.delivery_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="departed_at">
                                        { dataFormatter.dateTimeFormatter(item.departed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="arrived_at">
                                        { dataFormatter.dateTimeFormatter(item.arrived_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!market_stores?.deliveries_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Shelves Store</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Name</th>
                                
                                
                                
                                <th>ShelfType</th>
                                
                                
                                
                                <th>CapacityUnits</th>
                                
                                
                                
                                
                                
                                <th>IsCustomerAccessible</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {market_stores.shelves_store && Array.isArray(market_stores.shelves_store) &&
                              market_stores.shelves_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/shelves/shelves-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shelf_type">
                                        { item.shelf_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="capacity_units">
                                        { item.capacity_units }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_customer_accessible">
                                        { dataFormatter.booleanFormatter(item.is_customer_accessible) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!market_stores?.shelves_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Cash_registers Store</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Name</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>CashDrawerBalance</th>
                                
                                
                                
                                <th>PIXEnabled</th>
                                
                                
                                
                                <th>CreditEnabled</th>
                                
                                
                                
                                <th>DebitEnabled</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {market_stores.cash_registers_store && Array.isArray(market_stores.cash_registers_store) &&
                              market_stores.cash_registers_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/cash_registers/cash_registers-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="cash_drawer_balance">
                                        { item.cash_drawer_balance }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="pix_enabled">
                                        { dataFormatter.booleanFormatter(item.pix_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="credit_enabled">
                                        { dataFormatter.booleanFormatter(item.credit_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="debit_enabled">
                                        { dataFormatter.booleanFormatter(item.debit_enabled) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!market_stores?.cash_registers_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Sales_transactions Store</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>SoldAt</th>
                                
                                
                                
                                <th>PaymentMethod</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>SubtotalAmount</th>
                                
                                
                                
                                <th>TaxAmount</th>
                                
                                
                                
                                <th>TotalAmount</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {market_stores.sales_transactions_store && Array.isArray(market_stores.sales_transactions_store) &&
                              market_stores.sales_transactions_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/sales_transactions/sales_transactions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sold_at">
                                        { dataFormatter.dateTimeFormatter(item.sold_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_method">
                                        { item.payment_method }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal_amount">
                                        { item.subtotal_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_amount">
                                        { item.tax_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_amount">
                                        { item.total_amount }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!market_stores?.sales_transactions_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Daily_reports Store</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>BusinessDate</th>
                                
                                
                                
                                <th>GrossSales</th>
                                
                                
                                
                                <th>NetSales</th>
                                
                                
                                
                                <th>TransactionsCount</th>
                                
                                
                                
                                <th>CashTotal</th>
                                
                                
                                
                                <th>PIXTotal</th>
                                
                                
                                
                                <th>CardTotal</th>
                                
                                
                                
                                <th>WagesPaid</th>
                                
                                
                                
                                <th>InventoryPurchases</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {market_stores.daily_reports_store && Array.isArray(market_stores.daily_reports_store) &&
                              market_stores.daily_reports_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/daily_reports/daily_reports-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="business_date">
                                        { dataFormatter.dateTimeFormatter(item.business_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="gross_sales">
                                        { item.gross_sales }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="net_sales">
                                        { item.net_sales }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="transactions_count">
                                        { item.transactions_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="cash_total">
                                        { item.cash_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="pix_total">
                                        { item.pix_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="card_total">
                                        { item.card_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="wages_paid">
                                        { item.wages_paid }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="inventory_purchases">
                                        { item.inventory_purchases }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!market_stores?.daily_reports_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Market_storesView;