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/contract_documents/contract_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";

import {hasPermission} from "../../helpers/userPermissions";


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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Contractfolder</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{contract_documents?.contract_folder?.title ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Documentsource</p>
                    <p>{contract_documents?.document_source ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

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

              

              

              

              

              

              

              

              

              

              

              

              

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

              

              

              

              

              

              

              

              

              

              

              

              

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Contenthash</p>
                    <p>{contract_documents?.content_hash}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Currentrevisionnumber</p>
                  <p>{contract_documents?.current_revision_number || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Islocked'>
                    <SwitchField
                      field={{name: 'is_locked', value: contract_documents?.is_locked}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Contract_data_fields Contractdocument</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Name</th>
                                
                                
                                
                                <th>Key</th>
                                
                                
                                
                                <th>Value(text)</th>
                                
                                
                                
                                <th>Value(number)</th>
                                
                                
                                
                                <th>Value(datetime)</th>
                                
                                
                                
                                <th>Value(boolean)</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {contract_documents.contract_data_fields_contract_document && Array.isArray(contract_documents.contract_data_fields_contract_document) &&
                              contract_documents.contract_data_fields_contract_document.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/contract_data_fields/contract_data_fields-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="key">
                                        { item.key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="value_text">
                                        { item.value_text }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="value_number">
                                        { item.value_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="value_datetime">
                                        { dataFormatter.dateTimeFormatter(item.value_datetime) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="value_boolean">
                                        { dataFormatter.booleanFormatter(item.value_boolean) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!contract_documents?.contract_data_fields_contract_document?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Signing_actions Contractdocument</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Revisionnumber</th>
                                
                                
                                
                                <th>Actiontype</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Actionat</th>
                                
                                
                                
                                <th>Signaturehash</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {contract_documents.signing_actions_contract_document && Array.isArray(contract_documents.signing_actions_contract_document) &&
                              contract_documents.signing_actions_contract_document.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/signing_actions/signing_actions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="revision_number">
                                        { item.revision_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="action_type">
                                        { item.action_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="action_at">
                                        { dataFormatter.dateTimeFormatter(item.action_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="signature_hash">
                                        { item.signature_hash }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!contract_documents?.signing_actions_contract_document?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Document_revisions Contractdocument</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Revisionnumber</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                                
                                <th>Proposedat</th>
                                
                                
                                
                                <th>Changesummary</th>
                                
                                
                                
                                
                                
                                <th>Contenthash</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {contract_documents.document_revisions_contract_document && Array.isArray(contract_documents.document_revisions_contract_document) &&
                              contract_documents.document_revisions_contract_document.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/document_revisions/document_revisions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="revision_number">
                                        { item.revision_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="proposed_at">
                                        { dataFormatter.dateTimeFormatter(item.proposed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="change_summary">
                                        { item.change_summary }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="content_hash">
                                        { item.content_hash }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!contract_documents?.document_revisions_contract_document?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ai_contract_classifications Contractdocument</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Modelname</th>
                                
                                
                                
                                <th>Confidence</th>
                                
                                
                                
                                <th>Predictedcategory</th>
                                
                                
                                
                                <th>Predicteddocumenttype</th>
                                
                                
                                
                                <th>Isdisallowed</th>
                                
                                
                                
                                <th>Disallowreason</th>
                                
                                
                                
                                <th>Classifiedat</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {contract_documents.ai_contract_classifications_contract_document && Array.isArray(contract_documents.ai_contract_classifications_contract_document) &&
                              contract_documents.ai_contract_classifications_contract_document.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/ai_contract_classifications/ai_contract_classifications-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="model_name">
                                        { item.model_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="confidence">
                                        { item.confidence }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="predicted_category">
                                        { item.predicted_category }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="predicted_document_type">
                                        { item.predicted_document_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_disallowed">
                                        { dataFormatter.booleanFormatter(item.is_disallowed) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="disallow_reason">
                                        { item.disallow_reason }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="classified_at">
                                        { dataFormatter.dateTimeFormatter(item.classified_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!contract_documents?.ai_contract_classifications_contract_document?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                

                <BaseDivider />

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

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

export default Contract_documentsView;