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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Owner</p>
                    
                    
                        <p>{legal_templates?.owner?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TemplateCategory</p>
                    <p>{legal_templates?.template_category ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Jurisdiction</p>
                    <p>{legal_templates?.jurisdiction}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsPublic'>
                    <SwitchField
                      field={{name: 'is_public', value: legal_templates?.is_public}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SourceFile</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{legal_templates?.source_file?.file_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Template_fields Template</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>FieldKey</th>
                                
                                
                                
                                <th>Label</th>
                                
                                
                                
                                <th>ValueType</th>
                                
                                
                                
                                <th>IsRequired</th>
                                
                                
                                
                                <th>DefaultValue</th>
                                
                                
                                
                                <th>SortOrder</th>
                                
                                
                                
                                <th>HelpText</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {legal_templates.template_fields_template && Array.isArray(legal_templates.template_fields_template) &&
                              legal_templates.template_fields_template.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/template_fields/template_fields-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="field_key">
                                        { item.field_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="label">
                                        { item.label }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="value_type">
                                        { item.value_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_required">
                                        { dataFormatter.booleanFormatter(item.is_required) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="default_value">
                                        { item.default_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sort_order">
                                        { item.sort_order }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="help_text">
                                        { item.help_text }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!legal_templates?.template_fields_template?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Generated_documents Template</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>DocumentTitle</th>
                                
                                
                                
                                <th>DocumentType</th>
                                
                                
                                
                                <th>Format</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>LastEditedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {legal_templates.generated_documents_template && Array.isArray(legal_templates.generated_documents_template) &&
                              legal_templates.generated_documents_template.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/generated_documents/generated_documents-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="document_title">
                                        { item.document_title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="document_type">
                                        { item.document_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="format">
                                        { item.format }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="last_edited_at">
                                        { dataFormatter.dateTimeFormatter(item.last_edited_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!legal_templates?.generated_documents_template?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Legal_templatesView;