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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PlanTier</p>
                    <p>{subscription_plans?.plan_tier ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PriceMonthly</p>
                  <p>{subscription_plans?.price_monthly || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PriceYearly</p>
                  <p>{subscription_plans?.price_yearly || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MaxUsers</p>
                  <p>{subscription_plans?.max_users || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MaxBranches</p>
                  <p>{subscription_plans?.max_branches || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MaxDocuments</p>
                  <p>{subscription_plans?.max_documents || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='AIAssistantEnabled'>
                    <SwitchField
                      field={{name: 'ai_assistant_enabled', value: subscription_plans?.ai_assistant_enabled}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='WhatsAppAlertsEnabled'>
                    <SwitchField
                      field={{name: 'whatsapp_alerts_enabled', value: subscription_plans?.whatsapp_alerts_enabled}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='CustomRulesEnabled'>
                    <SwitchField
                      field={{name: 'custom_rules_enabled', value: subscription_plans?.custom_rules_enabled}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Company_subscriptions Plan</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>BillingProvider</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>CurrentPeriodStartAt</th>
                                
                                
                                
                                <th>CurrentPeriodEndAt</th>
                                
                                
                                
                                <th>TrialEndAt</th>
                                
                                
                                
                                <th>ProviderCustomerKey</th>
                                
                                
                                
                                <th>ProviderSubscriptionKey</th>
                                
                                
                                
                                <th>AutoRenew</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {subscription_plans.company_subscriptions_plan && Array.isArray(subscription_plans.company_subscriptions_plan) &&
                              subscription_plans.company_subscriptions_plan.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/company_subscriptions/company_subscriptions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="billing_provider">
                                        { item.billing_provider }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </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="trial_end_at">
                                        { dataFormatter.dateTimeFormatter(item.trial_end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="provider_customer_key">
                                        { item.provider_customer_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="provider_subscription_key">
                                        { item.provider_subscription_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="auto_renew">
                                        { dataFormatter.booleanFormatter(item.auto_renew) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!subscription_plans?.company_subscriptions_plan?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Subscription_plansView;