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/inspection_characteristics/inspection_characteristicsSlice'
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 Inspection_characteristicsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { inspection_characteristics } = useAppSelector((state) => state.inspection_characteristics)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>InspectionPlan</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{inspection_characteristics?.inspection_plan?.plan_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SequenceNo</p>
                  <p>{inspection_characteristics?.sequence_no || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CharacteristicName</p>
                    <p>{inspection_characteristics?.characteristic_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DataType</p>
                    <p>{inspection_characteristics?.data_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TargetValue</p>
                  <p>{inspection_characteristics?.target_value || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>LowerLimit</p>
                  <p>{inspection_characteristics?.lower_limit || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>UpperLimit</p>
                  <p>{inspection_characteristics?.upper_limit || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Unit</p>
                    <p>{inspection_characteristics?.unit}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Critical'>
                    <SwitchField
                      field={{name: 'critical', value: inspection_characteristics?.critical}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Inspection_results Characteristic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>MeasuredValue</th>
                                
                                
                                
                                <th>AttributeValue</th>
                                
                                
                                
                                <th>Judgement</th>
                                
                                
                                
                                <th>Comments</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {inspection_characteristics.inspection_results_characteristic && Array.isArray(inspection_characteristics.inspection_results_characteristic) &&
                              inspection_characteristics.inspection_results_characteristic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/inspection_results/inspection_results-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="measured_value">
                                        { item.measured_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="attribute_value">
                                        { item.attribute_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="judgement">
                                        { item.judgement }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="comments">
                                        { item.comments }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!inspection_characteristics?.inspection_results_characteristic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Inspection_characteristicsView;