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/membership_plans/membership_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";


const Membership_plansView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { membership_plans } = useAppSelector((state) => state.membership_plans)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

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

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>StripeProductRef</p>
                    <p>{membership_plans?.stripe_product_ref}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Memberships MembershipPlan</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>StartedAt</th>
                                
                                
                                
                                <th>CurrentPeriodEndAt</th>
                                
                                
                                
                                <th>CanceledAt</th>
                                
                                
                                
                                <th>StripeCustomerRef</th>
                                
                                
                                
                                <th>StripeSubscriptionRef</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {membership_plans.memberships_membership_plan && Array.isArray(membership_plans.memberships_membership_plan) &&
                              membership_plans.memberships_membership_plan.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/memberships/memberships-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_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>
                                    
                                    
                                    
                                    <td data-label="stripe_customer_ref">
                                        { item.stripe_customer_ref }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stripe_subscription_ref">
                                        { item.stripe_subscription_ref }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!membership_plans?.memberships_membership_plan?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Membership_plansView;