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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={pricing_plans?.description} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PriceAmount</p>
                  <p>{pricing_plans?.price_amount || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Currency</p>
                    <p>{pricing_plans?.currency}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BillingInterval</p>
                    <p>{pricing_plans?.billing_interval ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>StripePriceRef</p>
                    <p>{pricing_plans?.stripe_price_ref}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Subscriptions PricingPlan</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>StripeCustomerRef</th>
                                
                                
                                
                                <th>StripeSubscriptionRef</th>
                                
                                
                                
                                <th>CurrentPeriodStartAt</th>
                                
                                
                                
                                <th>CurrentPeriodEndAt</th>
                                
                                
                                
                                <th>CanceledAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {pricing_plans.subscriptions_pricing_plan && Array.isArray(pricing_plans.subscriptions_pricing_plan) &&
                              pricing_plans.subscriptions_pricing_plan.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/subscriptions/subscriptions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stripe_customer_ref">
                                        { item.stripe_customer_ref }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stripe_subscription_ref">
                                        { item.stripe_subscription_ref }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="current_period_start_at">
                                        { dataFormatter.dateTimeFormatter(item.current_period_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="current_period_end_at">
                                        { dataFormatter.dateTimeFormatter(item.current_period_end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="canceled_at">
                                        { dataFormatter.dateTimeFormatter(item.canceled_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!pricing_plans?.subscriptions_pricing_plan?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Payments PricingPlan</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Amount</th>
                                
                                
                                
                                <th>Currency</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>StripePaymentIntentRef</th>
                                
                                
                                
                                <th>ReceiptURL</th>
                                
                                
                                
                                <th>PaidAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {pricing_plans.payments_pricing_plan && Array.isArray(pricing_plans.payments_pricing_plan) &&
                              pricing_plans.payments_pricing_plan.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/payments/payments-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="amount">
                                        { item.amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currency">
                                        { item.currency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stripe_payment_intent_ref">
                                        { item.stripe_payment_intent_ref }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="receipt_url">
                                        { item.receipt_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="paid_at">
                                        { dataFormatter.dateTimeFormatter(item.paid_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!pricing_plans?.payments_pricing_plan?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                

                <BaseDivider />

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

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

export default Pricing_plansView;