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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

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

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MinimumSubtotal</p>
                  <p>{promo_codes?.min_subtotal || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>UsageLimit</p>
                  <p>{promo_codes?.usage_limit || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>UsageCount</p>
                  <p>{promo_codes?.usage_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Bookings PromoCode</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>PickupAt</th>
                                
                                
                                
                                <th>DropoffAt</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>PaymentStatus</th>
                                
                                
                                
                                <th>SubtotalAmount</th>
                                
                                
                                
                                <th>ExtrasAmount</th>
                                
                                
                                
                                <th>InsuranceAmount</th>
                                
                                
                                
                                <th>DiscountAmount</th>
                                
                                
                                
                                <th>TaxAmount</th>
                                
                                
                                
                                <th>TotalAmount</th>
                                
                                
                                
                                <th>Currency</th>
                                
                                
                                
                                <th>CustomerNotes</th>
                                
                                
                                
                                <th>InternalNotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {promo_codes.bookings_promo_code && Array.isArray(promo_codes.bookings_promo_code) &&
                              promo_codes.bookings_promo_code.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/bookings/bookings-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="pickup_at">
                                        { dataFormatter.dateTimeFormatter(item.pickup_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="dropoff_at">
                                        { dataFormatter.dateTimeFormatter(item.dropoff_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_status">
                                        { item.payment_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal_amount">
                                        { item.subtotal_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="extras_amount">
                                        { item.extras_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="insurance_amount">
                                        { item.insurance_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="discount_amount">
                                        { item.discount_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_amount">
                                        { item.tax_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_amount">
                                        { item.total_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currency">
                                        { item.currency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="customer_notes">
                                        { item.customer_notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="internal_notes">
                                        { item.internal_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!promo_codes?.bookings_promo_code?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Promo_codesView;