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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Creator</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{merch_stores?.creator?.handle ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Creator_profiles MerchStore</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Handle</th>
                                
                                
                                
                                <th>CreatorType</th>
                                
                                
                                
                                <th>IsVerified</th>
                                
                                
                                
                                <th>IsAdultCreator</th>
                                
                                
                                
                                <th>Slogan</th>
                                
                                
                                
                                
                                
                                <th>BusinessEmail</th>
                                
                                
                                
                                <th>SupportEmail</th>
                                
                                
                                
                                <th>PreferredLanguage</th>
                                
                                
                                
                                <th>CategoryTags(Comma-separated)</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {merch_stores.creator_profiles_merch_store && Array.isArray(merch_stores.creator_profiles_merch_store) &&
                              merch_stores.creator_profiles_merch_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/creator_profiles/creator_profiles-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="handle">
                                        { item.handle }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="creator_type">
                                        { item.creator_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_verified">
                                        { dataFormatter.booleanFormatter(item.is_verified) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_adult_creator">
                                        { dataFormatter.booleanFormatter(item.is_adult_creator) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="slogan">
                                        { item.slogan }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="business_email">
                                        { item.business_email }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="support_email">
                                        { item.support_email }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="preferred_language">
                                        { item.preferred_language }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="category_tags">
                                        { item.category_tags }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!merch_stores?.creator_profiles_merch_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Products MerchStore</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>ProductType</th>
                                
                                
                                
                                <th>Price</th>
                                
                                
                                
                                <th>Currency</th>
                                
                                
                                
                                <th>StockQuantity</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                                
                                <th>IsAdult</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {merch_stores.products_merch_store && Array.isArray(merch_stores.products_merch_store) &&
                              merch_stores.products_merch_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/products/products-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="product_type">
                                        { item.product_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="price">
                                        { item.price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currency">
                                        { item.currency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stock_quantity">
                                        { item.stock_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_adult">
                                        { dataFormatter.booleanFormatter(item.is_adult) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!merch_stores?.products_merch_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Orders MerchStore</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Subtotal</th>
                                
                                
                                
                                <th>ShippingCost</th>
                                
                                
                                
                                <th>TaxTotal</th>
                                
                                
                                
                                <th>Total</th>
                                
                                
                                
                                <th>Currency</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>PaymentMethod</th>
                                
                                
                                
                                <th>PlacedAt</th>
                                
                                
                                
                                <th>PaidAt</th>
                                
                                
                                
                                <th>FulfilledAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {merch_stores.orders_merch_store && Array.isArray(merch_stores.orders_merch_store) &&
                              merch_stores.orders_merch_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/orders/orders-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="subtotal">
                                        { item.subtotal }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shipping_cost">
                                        { item.shipping_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_total">
                                        { item.tax_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total">
                                        { item.total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currency">
                                        { item.currency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_method">
                                        { item.payment_method }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="placed_at">
                                        { dataFormatter.dateTimeFormatter(item.placed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="paid_at">
                                        { dataFormatter.dateTimeFormatter(item.paid_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="fulfilled_at">
                                        { dataFormatter.dateTimeFormatter(item.fulfilled_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!merch_stores?.orders_merch_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Shipping_profiles MerchStore</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ProfileName</th>
                                
                                
                                
                                <th>OriginCountry</th>
                                
                                
                                
                                <th>OriginRegion</th>
                                
                                
                                
                                <th>OriginPostalCode</th>
                                
                                
                                
                                <th>RulesJSON</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {merch_stores.shipping_profiles_merch_store && Array.isArray(merch_stores.shipping_profiles_merch_store) &&
                              merch_stores.shipping_profiles_merch_store.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/shipping_profiles/shipping_profiles-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="profile_name">
                                        { item.profile_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="origin_country">
                                        { item.origin_country }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="origin_region">
                                        { item.origin_region }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="origin_postal_code">
                                        { item.origin_postal_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rules_json">
                                        { item.rules_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!merch_stores?.shipping_profiles_merch_store?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Merch_storesView;