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/risk_assessments/risk_assessmentsSlice'
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 Risk_assessmentsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { risk_assessments } = useAppSelector((state) => state.risk_assessments)
    

    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 risk_assessments')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View risk_assessments')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/risk_assessments/risk_assessments-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Product</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{risk_assessments?.product?.product_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RiskFileName</p>
                    <p>{risk_assessments?.risk_file_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RiskFileCode</p>
                    <p>{risk_assessments?.risk_file_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Methodology</p>
                    <p>{risk_assessments?.methodology ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{risk_assessments?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

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

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Risk_items RiskAssessment</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>HazardID</th>
                                
                                
                                
                                <th>HazardDescription</th>
                                
                                
                                
                                <th>SequenceOfEvents</th>
                                
                                
                                
                                <th>Severity</th>
                                
                                
                                
                                <th>Occurrence</th>
                                
                                
                                
                                <th>Detectability</th>
                                
                                
                                
                                <th>RiskLevelInitial</th>
                                
                                
                                
                                <th>RiskControls</th>
                                
                                
                                
                                <th>RiskLevelResidual</th>
                                
                                
                                
                                <th>VerificationOfControls</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {risk_assessments.risk_items_risk_assessment && Array.isArray(risk_assessments.risk_items_risk_assessment) &&
                              risk_assessments.risk_items_risk_assessment.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/risk_items/risk_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="hazard_id">
                                        { item.hazard_id }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="hazard_description">
                                        { item.hazard_description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sequence_of_events">
                                        { item.sequence_of_events }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="severity">
                                        { item.severity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="occurrence">
                                        { item.occurrence }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="detectability">
                                        { item.detectability }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="risk_level_initial">
                                        { item.risk_level_initial }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="risk_controls">
                                        { item.risk_controls }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="risk_level_residual">
                                        { item.risk_level_residual }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="verification_of_controls">
                                        { item.verification_of_controls }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!risk_assessments?.risk_items_risk_assessment?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Risk_assessmentsView;