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

              

              

              

              

              

              

              

              

              

              
                {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Organization</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{evidence_models?.organization?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                  } 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Workflow</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{evidence_models?.workflow?.workflow_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ControlRequirement</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{evidence_models?.control_requirement?.control_code ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SourceSystem</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{evidence_models?.source_system?.system_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ArtifactType</p>
                    <p>{evidence_models?.artifact_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='ApprovalRequiredFlag'>
                    <SwitchField
                      field={{name: 'approval_required_flag', value: evidence_models?.approval_required_flag}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RetentionPeriod</p>
                    <p>{evidence_models?.retention_period ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>EvidenceStrength</p>
                    <p>{evidence_models?.evidence_strength ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CollectionMethod</p>
                    <p>{evidence_models?.collection_method ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Artifacts EvidenceModel</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>ArtifactName</th>
                                
                                
                                
                                <th>ArtifactType</th>
                                
                                
                                
                                <th>ArtifactReference</th>
                                
                                
                                
                                
                                
                                <th>CollectedAt</th>
                                
                                
                                
                                <th>EventTimestamp</th>
                                
                                
                                
                                <th>SubjectUser</th>
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>ValidationNotes</th>
                                
                                
                                
                                <th>EvidenceStrengthScore</th>
                                
                                
                                
                                <th>ChainIntegrityScore</th>
                                
                                
                                
                                <th>RetentionExpiry</th>
                                
                                
                                
                                <th>Tags</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {evidence_models.artifacts_evidence_model && Array.isArray(evidence_models.artifacts_evidence_model) &&
                              evidence_models.artifacts_evidence_model.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/artifacts/artifacts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="artifact_name">
                                        { item.artifact_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="artifact_type">
                                        { item.artifact_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="artifact_reference">
                                        { item.artifact_reference }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="collected_at">
                                        { dataFormatter.dateTimeFormatter(item.collected_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="event_timestamp">
                                        { dataFormatter.dateTimeFormatter(item.event_timestamp) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subject_user">
                                        { item.subject_user }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="validation_notes">
                                        { item.validation_notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="evidence_strength_score">
                                        { item.evidence_strength_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="chain_integrity_score">
                                        { item.chain_integrity_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="retention_expiry">
                                        { dataFormatter.dateTimeFormatter(item.retention_expiry) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tags">
                                        { item.tags }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!evidence_models?.artifacts_evidence_model?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Evidence_modelsView;