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/waiver_documents/waiver_documentsSlice'
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 Waiver_documentsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { waiver_documents } = useAppSelector((state) => state.waiver_documents)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Title</p>
                    <p>{waiver_documents?.title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DocumentType</p>
                    <p>{waiver_documents?.document_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DocumentFile</p>
                    {waiver_documents?.document_file?.length
                      ? dataFormatter.filesFormatter(waiver_documents.document_file).map(link => (
                        <button
                          key={link.publicUrl}
                          onClick={(e) => saveFile(e, link.publicUrl, link.name)}
                        >
                            {link.name}
                        </button>
                      )) : <p>No DocumentFile</p>
                    }
                </div>
              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Required'>
                    <SwitchField
                      field={{name: 'is_required', value: waiver_documents?.is_required}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Document_signatures WaiverDocument</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>
                            {waiver_documents.document_signatures_waiver_document && Array.isArray(waiver_documents.document_signatures_waiver_document) &&
                              waiver_documents.document_signatures_waiver_document.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>
                        {!waiver_documents?.document_signatures_waiver_document?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Waiver_documentsView;