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/stores/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";

import {hasPermission} from "../../helpers/userPermissions";


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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>MerchantProfile</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{stores?.merchant_profile?.business_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>StoreName</p>
                    <p>{stores?.store_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>StoreCode</p>
                    <p>{stores?.store_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>StorePhone</p>
                    <p>{stores?.store_phone}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>StoreEmail</p>
                    <p>{stores?.store_email}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Address</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{stores?.address?.label ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{stores?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='OffersPickup'>
                    <SwitchField
                      field={{name: 'offers_pickup', value: stores?.offers_pickup}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='OffersDelivery'>
                    <SwitchField
                      field={{name: 'offers_delivery', value: stores?.offers_delivery}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='ClosedAt'>
                    {stores.closed_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={stores.closed_at ?
                        new Date(
                          dayjs(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'}>organizations</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{stores?.organizations?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Products Store</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>ProductName</th>
                                
                                
                                
                                <th>SKU</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Price</th>
                                
                                
                                
                                <th>CompareAtPrice</th>
                                
                                
                                
                                <th>StockQuantity</th>
                                
                                
                                
                                <th>IsAvailable</th>
                                
                                
                                
                                <th>IsFeatured</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {stores.products_store && Array.isArray(stores.products_store) &&
                              stores.products_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/products/products-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="product_name">
                                        { item.product_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sku">
                                        { item.sku }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="price">
                                        { item.price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="compare_at_price">
                                        { item.compare_at_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stock_quantity">
                                        { item.stock_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_available">
                                        { dataFormatter.booleanFormatter(item.is_available) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_featured">
                                        { dataFormatter.booleanFormatter(item.is_featured) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!stores?.products_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Carts 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>LastActivityAt</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {stores.carts_store && Array.isArray(stores.carts_store) &&
                              stores.carts_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/carts/carts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_activity_at">
                                        { dataFormatter.dateTimeFormatter(item.last_activity_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!stores?.carts_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Orders Store</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>FulfillmentType</th>
                                
                                
                                
                                <th>SubtotalAmount</th>
                                
                                
                                
                                <th>DeliveryFee</th>
                                
                                
                                
                                <th>ServiceFee</th>
                                
                                
                                
                                <th>DiscountAmount</th>
                                
                                
                                
                                <th>TaxAmount</th>
                                
                                
                                
                                <th>TotalAmount</th>
                                
                                
                                
                                <th>CustomerNote</th>
                                
                                
                                
                                <th>PlacedAt</th>
                                
                                
                                
                                <th>DeliveredAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {stores.orders_store && Array.isArray(stores.orders_store) &&
                              stores.orders_store.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="fulfillment_type">
                                        { item.fulfillment_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal_amount">
                                        { item.subtotal_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="delivery_fee">
                                        { item.delivery_fee }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="service_fee">
                                        { item.service_fee }
                                    </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="customer_note">
                                        { item.customer_note }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="placed_at">
                                        { dataFormatter.dateTimeFormatter(item.placed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="delivered_at">
                                        { dataFormatter.dateTimeFormatter(item.delivered_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!stores?.orders_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Commissions Store</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Scope</th>
                                
                                
                                
                                
                                
                                <th>MerchantType</th>
                                
                                
                                
                                <th>FeeType</th>
                                
                                
                                
                                <th>FeeValue</th>
                                
                                
                                
                                <th>EffectiveFrom</th>
                                
                                
                                
                                <th>EffectiveTo</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {stores.commissions_store && Array.isArray(stores.commissions_store) &&
                              stores.commissions_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/commissions/commissions-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="scope">
                                        { item.scope }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="merchant_type">
                                        { item.merchant_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="fee_type">
                                        { item.fee_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="fee_value">
                                        { item.fee_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="effective_from">
                                        { dataFormatter.dateTimeFormatter(item.effective_from) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="effective_to">
                                        { dataFormatter.dateTimeFormatter(item.effective_to) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!stores?.commissions_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Reviews Store</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>TargetType</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Rating</th>
                                
                                
                                
                                <th>Comment</th>
                                
                                
                                
                                <th>ReviewedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {stores.reviews_store && Array.isArray(stores.reviews_store) &&
                              stores.reviews_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/reviews/reviews-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="target_type">
                                        { item.target_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="rating">
                                        { item.rating }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="comment">
                                        { item.comment }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reviewed_at">
                                        { dataFormatter.dateTimeFormatter(item.reviewed_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!stores?.reviews_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default StoresView;