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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>LaboratoryName</p>
                    <p>{laboratories?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Country</p>
                    <p>{laboratories?.country}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>City</p>
                    <p>{laboratories?.city}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ContactEmail</p>
                    <p>{laboratories?.contact_email}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ContactPhone</p>
                    <p>{laboratories?.contact_phone}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Metallurgical_tests Laboratory</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>
                            {laboratories.metallurgical_tests_laboratory && Array.isArray(laboratories.metallurgical_tests_laboratory) &&
                              laboratories.metallurgical_tests_laboratory.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>
                        {!laboratories?.metallurgical_tests_laboratory?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default LaboratoriesView;