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/coupons/couponsSlice'
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 CouponsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { coupons } = useAppSelector((state) => state.coupons)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Code</p>
                    <p>{coupons?.code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DiscountType</p>
                    <p>{coupons?.discount_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>DiscountValue</p>
                  <p>{coupons?.discount_value || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MinimumOrderTotal(IQD)</p>
                  <p>{coupons?.min_order_total_iqd || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>UsageLimit(Total)</p>
                  <p>{coupons?.usage_limit_total || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>UsageLimit(PerUser)</p>
                  <p>{coupons?.usage_limit_per_user || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>Redemptions</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>RedeemedAt</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {coupons.redemptions && Array.isArray(coupons.redemptions) &&
                              coupons.redemptions.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/coupon_redemptions/coupon_redemptions-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="redeemed_at">
                                        { dataFormatter.dateTimeFormatter(item.redeemed_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!coupons?.redemptions?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Carts AppliedCoupon</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>SessionKey</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Subtotal(IQD)</th>
                                
                                
                                
                                <th>DiscountTotal(IQD)</th>
                                
                                
                                
                                <th>ShippingTotal(IQD)</th>
                                
                                
                                
                                <th>GrandTotal(IQD)</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {coupons.carts_applied_coupon && Array.isArray(coupons.carts_applied_coupon) &&
                              coupons.carts_applied_coupon.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/carts/carts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="session_key">
                                        { item.session_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal_iqd">
                                        { item.subtotal_iqd }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="discount_total_iqd">
                                        { item.discount_total_iqd }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shipping_total_iqd">
                                        { item.shipping_total_iqd }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="grand_total_iqd">
                                        { item.grand_total_iqd }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!coupons?.carts_applied_coupon?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Orders AppliedCoupon</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>Status</th>
                                
                                
                                
                                <th>PaymentStatus</th>
                                
                                
                                
                                <th>FulfillmentStatus</th>
                                
                                
                                
                                <th>Subtotal(IQD)</th>
                                
                                
                                
                                <th>DiscountTotal(IQD)</th>
                                
                                
                                
                                <th>ShippingTotal(IQD)</th>
                                
                                
                                
                                <th>GrandTotal(IQD)</th>
                                
                                
                                
                                <th>CustomerNote</th>
                                
                                
                                
                                <th>PlacedAt</th>
                                
                                
                                
                                <th>PaidAt</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {coupons.orders_applied_coupon && Array.isArray(coupons.orders_applied_coupon) &&
                              coupons.orders_applied_coupon.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="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_status">
                                        { item.payment_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="fulfillment_status">
                                        { item.fulfillment_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal_iqd">
                                        { item.subtotal_iqd }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="discount_total_iqd">
                                        { item.discount_total_iqd }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shipping_total_iqd">
                                        { item.shipping_total_iqd }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="grand_total_iqd">
                                        { item.grand_total_iqd }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="customer_note">
                                        { item.customer_note }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="placed_at">
                                        { dataFormatter.dateTimeFormatter(item.placed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="paid_at">
                                        { dataFormatter.dateTimeFormatter(item.paid_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!coupons?.orders_applied_coupon?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Coupon_redemptions Coupon</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>RedeemedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {coupons.coupon_redemptions_coupon && Array.isArray(coupons.coupon_redemptions_coupon) &&
                              coupons.coupon_redemptions_coupon.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/coupon_redemptions/coupon_redemptions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="redeemed_at">
                                        { dataFormatter.dateTimeFormatter(item.redeemed_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!coupons?.coupon_redemptions_coupon?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default CouponsView;