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

              

              

              

              

              

              

              

              

              

              
                {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Organization</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{service_packages?.organization?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                  } 
              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Price</p>
                  <p>{service_packages?.price || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BillingType</p>
                    <p>{service_packages?.billing_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Service_requests ServicePackage</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>ServiceType</th>
                                
                                
                                
                                <th>TimingPreference</th>
                                
                                
                                
                                <th>ScheduledStartAt</th>
                                
                                
                                
                                <th>ScheduledEndAt</th>
                                
                                
                                
                                <th>RequestSummary</th>
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Priority</th>
                                
                                
                                
                                <th>EstimatedTotal</th>
                                
                                
                                
                                <th>FinalTotal</th>
                                
                                
                                
                                <th>SubmittedAt</th>
                                
                                
                                
                                <th>ClosedAt</th>
                                
                                
                                
                                <th>NoContactCoordination</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {service_packages.service_requests_service_package && Array.isArray(service_packages.service_requests_service_package) &&
                              service_packages.service_requests_service_package.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/service_requests/service_requests-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="service_type">
                                        { item.service_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="timing_preference">
                                        { item.timing_preference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_start_at">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_end_at">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="summary">
                                        { item.summary }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="priority">
                                        { item.priority }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="estimated_total">
                                        { item.estimated_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="final_total">
                                        { item.final_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="submitted_at">
                                        { dataFormatter.dateTimeFormatter(item.submitted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="closed_at">
                                        { dataFormatter.dateTimeFormatter(item.closed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requires_no_contact_coordination">
                                        { dataFormatter.booleanFormatter(item.requires_no_contact_coordination) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!service_packages?.service_requests_service_package?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Subscriptions ServicePackage</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>ProviderReference</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>CurrentPeriodStartAt</th>
                                
                                
                                
                                <th>CurrentPeriodEndAt</th>
                                
                                
                                
                                <th>CancelledAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {service_packages.subscriptions_service_package && Array.isArray(service_packages.subscriptions_service_package) &&
                              service_packages.subscriptions_service_package.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/subscriptions/subscriptions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="provider_reference">
                                        { item.provider_reference }
                                    </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="cancelled_at">
                                        { dataFormatter.dateTimeFormatter(item.cancelled_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!service_packages?.subscriptions_service_package?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Service_packagesView;