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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>VersionNumber</p>
                  <p>{flowsheets?.version_number || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ApprovedBy</p>
                    
                    
                        <p>{flowsheets?.approved_by?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Flowsheet_nodes Flowsheet</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>NodeKey</th>
                                
                                
                                
                                <th>NodeType</th>
                                
                                
                                
                                <th>UnitOperation</th>
                                
                                
                                
                                <th>Name</th>
                                
                                
                                
                                <th>PositionX</th>
                                
                                
                                
                                <th>PositionY</th>
                                
                                
                                
                                <th>ParametersJSON</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {flowsheets.flowsheet_nodes_flowsheet && Array.isArray(flowsheets.flowsheet_nodes_flowsheet) &&
                              flowsheets.flowsheet_nodes_flowsheet.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/flowsheet_nodes/flowsheet_nodes-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="node_key">
                                        { item.node_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="node_type">
                                        { item.node_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_operation">
                                        { item.unit_operation }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="position_x">
                                        { item.position_x }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="position_y">
                                        { item.position_y }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="parameters_json">
                                        { item.parameters_json }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!flowsheets?.flowsheet_nodes_flowsheet?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Flowsheet_streams Flowsheet</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>
                            {flowsheets.flowsheet_streams_flowsheet && Array.isArray(flowsheets.flowsheet_streams_flowsheet) &&
                              flowsheets.flowsheet_streams_flowsheet.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>
                        {!flowsheets?.flowsheet_streams_flowsheet?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Economic_models Flowsheet</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>ModelName</th>
                                
                                
                                
                                <th>Currency</th>
                                
                                
                                
                                <th>GoldPrice</th>
                                
                                
                                
                                <th>DiscountRate(%)</th>
                                
                                
                                
                                <th>Throughput(tpd)</th>
                                
                                
                                
                                <th>Recovery(%)</th>
                                
                                
                                
                                <th>MineLife(years)</th>
                                
                                
                                
                                <th>TotalCAPEX</th>
                                
                                
                                
                                <th>OPEXPerTonne</th>
                                
                                
                                
                                <th>NPV</th>
                                
                                
                                
                                <th>IRR(%)</th>
                                
                                
                                
                                <th>Payback(years)</th>
                                
                                
                                
                                <th>AssumptionsJSON</th>
                                
                                
                                
                                <th>SensitivityJSON</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {flowsheets.economic_models_flowsheet && Array.isArray(flowsheets.economic_models_flowsheet) &&
                              flowsheets.economic_models_flowsheet.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/economic_models/economic_models-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currency">
                                        { item.currency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="gold_price">
                                        { item.gold_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="discount_rate_pct">
                                        { item.discount_rate_pct }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="throughput_tpd">
                                        { item.throughput_tpd }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="recovery_pct">
                                        { item.recovery_pct }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mine_life_years">
                                        { item.mine_life_years }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="capex_total">
                                        { item.capex_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="opex_per_tonne">
                                        { item.opex_per_tonne }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="npv">
                                        { item.npv }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="irr_pct">
                                        { item.irr_pct }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payback_years">
                                        { item.payback_years }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="assumptions_json">
                                        { item.assumptions_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sensitivity_json">
                                        { item.sensitivity_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!flowsheets?.economic_models_flowsheet?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default FlowsheetsView;