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/enrollments/enrollmentsSlice'
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 EnrollmentsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { enrollments } = useAppSelector((state) => state.enrollments)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Camper</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{enrollments?.camper?.first_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>EnrollmentStatus</p>
                    <p>{enrollments?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='ApplicationFeeWaived'>
                    <SwitchField
                      field={{name: 'application_fee_waived', value: enrollments?.application_fee_waived}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Payments Enrollment</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>PaymentKind</th>
                                
                                
                                
                                <th>Amount</th>
                                
                                
                                
                                <th>PaymentStatus</th>
                                
                                
                                
                                <th>PaymentMethod</th>
                                
                                
                                
                                <th>ReferenceCode</th>
                                
                                
                                
                                <th>DueDate</th>
                                
                                
                                
                                <th>PaidDate</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {enrollments.payments_enrollment && Array.isArray(enrollments.payments_enrollment) &&
                              enrollments.payments_enrollment.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/payments/payments-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="payment_kind">
                                        { item.payment_kind }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="amount">
                                        { item.amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="method">
                                        { item.method }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reference_code">
                                        { item.reference_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_datetime">
                                        { dataFormatter.dateTimeFormatter(item.due_datetime) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="paid_datetime">
                                        { dataFormatter.dateTimeFormatter(item.paid_datetime) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!enrollments?.payments_enrollment?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Document_signatures Enrollment</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>SignatureStatus</th>
                                
                                
                                
                                <th>SignedAt</th>
                                
                                
                                
                                <th>SignatureName</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {enrollments.document_signatures_enrollment && Array.isArray(enrollments.document_signatures_enrollment) &&
                              enrollments.document_signatures_enrollment.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/document_signatures/document_signatures-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="signed_datetime">
                                        { dataFormatter.dateTimeFormatter(item.signed_datetime) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="signature_name">
                                        { item.signature_name }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!enrollments?.document_signatures_enrollment?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Trip_registrations Enrollment</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>RegistrationStatus</th>
                                
                                
                                
                                <th>PermissionGranted</th>
                                
                                
                                
                                <th>RegisteredAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {enrollments.trip_registrations_enrollment && Array.isArray(enrollments.trip_registrations_enrollment) &&
                              enrollments.trip_registrations_enrollment.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/trip_registrations/trip_registrations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="permission_granted">
                                        { dataFormatter.booleanFormatter(item.permission_granted) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="registered_datetime">
                                        { dataFormatter.dateTimeFormatter(item.registered_datetime) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!enrollments?.trip_registrations_enrollment?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>End_of_summer_trip_selections Enrollment</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>SelectionStatus</th>
                                
                                
                                
                                <th>SelectedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {enrollments.end_of_summer_trip_selections_enrollment && Array.isArray(enrollments.end_of_summer_trip_selections_enrollment) &&
                              enrollments.end_of_summer_trip_selections_enrollment.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/end_of_summer_trip_selections/end_of_summer_trip_selections-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="selected_datetime">
                                        { dataFormatter.dateTimeFormatter(item.selected_datetime) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!enrollments?.end_of_summer_trip_selections_enrollment?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default EnrollmentsView;