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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SystemName</p>
                    <p>{connected_systems?.system_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SystemType</p>
                    <p>{connected_systems?.system_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Vendor</p>
                    <p>{connected_systems?.vendor}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Environment</p>
                    <p>{connected_systems?.environment ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ConnectionStatus</p>
                    <p>{connected_systems?.connection_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='LastSuccessfulSyncAt'>
                    {connected_systems.last_successful_sync_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={connected_systems.last_successful_sync_at ?
                        new Date(
                          dayjs(connected_systems.last_successful_sync_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No LastSuccessfulSyncAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='SourceofTruthFlag'>
                    <SwitchField
                      field={{name: 'source_of_truth_flag', value: connected_systems?.source_of_truth_flag}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Evidence_models SourceSystem</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>EvidenceClaim</th>
                                
                                
                                
                                
                                
                                <th>ArtifactType</th>
                                
                                
                                
                                <th>RequiredFields</th>
                                
                                
                                
                                <th>ApprovalRequiredFlag</th>
                                
                                
                                
                                <th>RetentionPeriod</th>
                                
                                
                                
                                <th>EvidenceStrength</th>
                                
                                
                                
                                <th>CollectionMethod</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {connected_systems.evidence_models_source_system && Array.isArray(connected_systems.evidence_models_source_system) &&
                              connected_systems.evidence_models_source_system.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/evidence_models/evidence_models-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="evidence_claim">
                                        { item.evidence_claim }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="artifact_type">
                                        { item.artifact_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="required_fields">
                                        { item.required_fields }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="approval_required_flag">
                                        { dataFormatter.booleanFormatter(item.approval_required_flag) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="retention_period">
                                        { item.retention_period }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="evidence_strength">
                                        { item.evidence_strength }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="collection_method">
                                        { item.collection_method }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!connected_systems?.evidence_models_source_system?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Artifacts SourceSystem</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>
                            {connected_systems.artifacts_source_system && Array.isArray(connected_systems.artifacts_source_system) &&
                              connected_systems.artifacts_source_system.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>
                        {!connected_systems?.artifacts_source_system?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Connected_systemsView;