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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>FrameworkName</p>
                    <p>{control_frameworks?.framework_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Version</p>
                    <p>{control_frameworks?.version}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='ActiveFlag'>
                    <SwitchField
                      field={{name: 'active_flag', value: control_frameworks?.active_flag}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Control_requirements Framework</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ControlCode</th>
                                
                                
                                
                                <th>ControlFamily</th>
                                
                                
                                
                                <th>ControlTitle</th>
                                
                                
                                
                                <th>ControlDescription</th>
                                
                                
                                
                                <th>EvidenceExpectationSummary</th>
                                
                                
                                
                                <th>ActiveFlag</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {control_frameworks.control_requirements_framework && Array.isArray(control_frameworks.control_requirements_framework) &&
                              control_frameworks.control_requirements_framework.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/control_requirements/control_requirements-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="control_code">
                                        { item.control_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="control_family">
                                        { item.control_family }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="control_title">
                                        { item.control_title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="control_description">
                                        { item.control_description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="evidence_expectation_summary">
                                        { item.evidence_expectation_summary }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="active_flag">
                                        { dataFormatter.booleanFormatter(item.active_flag) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!control_frameworks?.control_requirements_framework?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Proof_packets Framework</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>PacketName</th>
                                
                                
                                
                                <th>PacketType</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>PacketStatus</th>
                                
                                
                                
                                <th>ReadinessScore</th>
                                
                                
                                
                                <th>GeneratedOn</th>
                                
                                
                                
                                <th>DueDate</th>
                                
                                
                                
                                <th>Summary</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {control_frameworks.proof_packets_framework && Array.isArray(control_frameworks.proof_packets_framework) &&
                              control_frameworks.proof_packets_framework.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/proof_packets/proof_packets-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="packet_name">
                                        { item.packet_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="packet_type">
                                        { item.packet_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="packet_status">
                                        { item.packet_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="readiness_score">
                                        { item.readiness_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="generated_on">
                                        { dataFormatter.dateTimeFormatter(item.generated_on) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_date">
                                        { dataFormatter.dateTimeFormatter(item.due_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="summary">
                                        { item.summary }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!control_frameworks?.proof_packets_framework?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Control_frameworksView;