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/document_templates/document_templatesSlice'
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 Document_templatesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { document_templates } = useAppSelector((state) => state.document_templates)
    

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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TemplateName</p>
                    <p>{document_templates?.template_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TemplateCode</p>
                    <p>{document_templates?.template_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TemplateFamily</p>
                    <p>{document_templates?.template_family ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

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

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Format</p>
                    <p>{document_templates?.format ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>MappedClauses</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>ClauseCode</th>
                                
                                
                                
                                    <th>Title</th>
                                
                                
                                
                                    <th>TextExcerpt</th>
                                
                                
                                
                                    <th>GuidanceNotes</th>
                                
                                
                                
                                    <th>Active</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {document_templates.mapped_clauses && Array.isArray(document_templates.mapped_clauses) &&
                              document_templates.mapped_clauses.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/clauses/clauses-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    
                                    
                                    <td data-label="clause_code">
                                        { item.clause_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="text_excerpt">
                                        { item.text_excerpt }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="guidance_notes">
                                        { item.guidance_notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="active">
                                        { dataFormatter.booleanFormatter(item.active) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!document_templates?.mapped_clauses?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Documents Template</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>DocumentNumber</th>
                                
                                
                                
                                <th>CurrentRevision</th>
                                
                                
                                
                                <th>LifecycleStatus</th>
                                
                                
                                
                                <th>ControlLevel</th>
                                
                                
                                
                                <th>Confidentiality</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>EffectiveDate</th>
                                
                                
                                
                                <th>NextReviewDate</th>
                                
                                
                                
                                <th>Summary</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>TrainingRequired</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {document_templates.documents_template && Array.isArray(document_templates.documents_template) &&
                              document_templates.documents_template.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/documents/documents-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="document_number">
                                        { item.document_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="current_revision">
                                        { item.current_revision }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lifecycle_status">
                                        { item.lifecycle_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="control_level">
                                        { item.control_level }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="confidentiality">
                                        { item.confidentiality }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="effective_date">
                                        { dataFormatter.dateTimeFormatter(item.effective_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="next_review_date">
                                        { dataFormatter.dateTimeFormatter(item.next_review_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="summary">
                                        { item.summary }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="training_required">
                                        { dataFormatter.booleanFormatter(item.training_required) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!document_templates?.documents_template?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Document_templatesView;