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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Phase</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{design_criteria_items?.phase?.gate_notes ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Parameter</p>
                    <p>{design_criteria_items?.parameter}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Value</p>
                  <p>{design_criteria_items?.value || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Confidence</p>
                    <p>{design_criteria_items?.confidence ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>BasisNotes</p>
                  {design_criteria_items.basis_notes
                    ? <p dangerouslySetInnerHTML={{__html: design_criteria_items.basis_notes}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>SourceTests</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>LabReference</th>
                                
                                
                                
                                    <th>Status</th>
                                
                                
                                
                                    <th>DateSubmitted</th>
                                
                                
                                
                                    <th>DateCompleted</th>
                                
                                
                                
                                    <th>DataSource</th>
                                
                                
                                
                                    <th>ParametersJSON</th>
                                
                                
                                
                                    <th>ResultsJSON</th>
                                
                                
                                
                                    <th>QAQCStatus</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>ReviewedAt</th>
                                
                                
                                
                                
                                
                                    <th>ApprovedAt</th>
                                
                                
                                
                                    <th>IsDeleted</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {design_criteria_items.source_tests && Array.isArray(design_criteria_items.source_tests) &&
                              design_criteria_items.source_tests.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/metallurgical_tests/metallurgical_tests-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="lab_reference">
                                        { item.lab_reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="date_submitted">
                                        { dataFormatter.dateTimeFormatter(item.date_submitted) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="date_completed">
                                        { dataFormatter.dateTimeFormatter(item.date_completed) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="data_source">
                                        { item.data_source }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="parameters_json">
                                        { item.parameters_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="results_json">
                                        { item.results_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="qaqc_status">
                                        { item.qaqc_status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="reviewed_at">
                                        { dataFormatter.dateTimeFormatter(item.reviewed_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="approved_at">
                                        { dataFormatter.dateTimeFormatter(item.approved_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_deleted">
                                        { dataFormatter.booleanFormatter(item.is_deleted) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!design_criteria_items?.source_tests?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Design_criteria_itemsView;