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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NodeKey</p>
                    <p>{flowsheet_nodes?.node_key}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NodeType</p>
                    <p>{flowsheet_nodes?.node_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>UnitOperation</p>
                    <p>{flowsheet_nodes?.unit_operation ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PositionX</p>
                  <p>{flowsheet_nodes?.position_x || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PositionY</p>
                  <p>{flowsheet_nodes?.position_y || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>EquipmentItem</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{flowsheet_nodes?.equipment_item?.model ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>TestBasis</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>
                            {flowsheet_nodes.test_basis && Array.isArray(flowsheet_nodes.test_basis) &&
                              flowsheet_nodes.test_basis.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>
                        {!flowsheet_nodes?.test_basis?.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>{flowsheet_nodes?.organizations?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Flowsheet_streams SourceNode</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>StreamType</th>
                                
                                
                                
                                <th>MassFlow(t/h)</th>
                                
                                
                                
                                <th>WaterFlow(m3/h)</th>
                                
                                
                                
                                <th>Solids(%)</th>
                                
                                
                                
                                <th>AuGrade(g/t)</th>
                                
                                
                                
                                <th>AuRecovery(%)</th>
                                
                                
                                
                                <th>ComponentsJSON</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {flowsheet_nodes.flowsheet_streams_source_node && Array.isArray(flowsheet_nodes.flowsheet_streams_source_node) &&
                              flowsheet_nodes.flowsheet_streams_source_node.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/flowsheet_streams/flowsheet_streams-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="stream_type">
                                        { item.stream_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mass_flow_tph">
                                        { item.mass_flow_tph }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="water_flow_m3h">
                                        { item.water_flow_m3h }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="solids_pct">
                                        { item.solids_pct }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="au_grade_gpt">
                                        { item.au_grade_gpt }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="au_recovery_pct">
                                        { item.au_recovery_pct }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="components_json">
                                        { item.components_json }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!flowsheet_nodes?.flowsheet_streams_source_node?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                <>
                    <p className={'block font-bold mb-2'}>Flowsheet_streams TargetNode</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>StreamType</th>
                                
                                
                                
                                <th>MassFlow(t/h)</th>
                                
                                
                                
                                <th>WaterFlow(m3/h)</th>
                                
                                
                                
                                <th>Solids(%)</th>
                                
                                
                                
                                <th>AuGrade(g/t)</th>
                                
                                
                                
                                <th>AuRecovery(%)</th>
                                
                                
                                
                                <th>ComponentsJSON</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {flowsheet_nodes.flowsheet_streams_target_node && Array.isArray(flowsheet_nodes.flowsheet_streams_target_node) &&
                              flowsheet_nodes.flowsheet_streams_target_node.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/flowsheet_streams/flowsheet_streams-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="stream_type">
                                        { item.stream_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mass_flow_tph">
                                        { item.mass_flow_tph }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="water_flow_m3h">
                                        { item.water_flow_m3h }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="solids_pct">
                                        { item.solids_pct }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="au_grade_gpt">
                                        { item.au_grade_gpt }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="au_recovery_pct">
                                        { item.au_recovery_pct }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="components_json">
                                        { item.components_json }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!flowsheet_nodes?.flowsheet_streams_target_node?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Flowsheet_nodesView;