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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>EquipmentType</p>
                    <p>{equipment_items?.equipment_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Manufacturer</p>
                    <p>{equipment_items?.manufacturer}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Model</p>
                    <p>{equipment_items?.model}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Capacity</p>
                  <p>{equipment_items?.capacity || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CapacityUnit</p>
                    <p>{equipment_items?.capacity_unit}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Power(kW)</p>
                  <p>{equipment_items?.power_kw || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>BaseCAPEX(USD)</p>
                  <p>{equipment_items?.base_capex_usd || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>InstallFactor</p>
                  <p>{equipment_items?.install_factor || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>OPEXPerUnit</p>
                  <p>{equipment_items?.opex_per_unit || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>OPEXUnit</p>
                    <p>{equipment_items?.opex_unit}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>VendorQuoteReference</p>
                    <p>{equipment_items?.vendor_quote_reference}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DatasheetFiles</p>
                    {equipment_items?.datasheet_files?.length
                      ? dataFormatter.filesFormatter(equipment_items.datasheet_files).map(link => (
                        <button
                          key={link.publicUrl}
                          onClick={(e) => saveFile(e, link.publicUrl, link.name)}
                        >
                            {link.name}
                        </button>
                      )) : <p>No DatasheetFiles</p>
                    }
                </div>
              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsActive'>
                    <SwitchField
                      field={{name: 'is_active', value: equipment_items?.is_active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Flowsheet_nodes EquipmentItem</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>
                            {equipment_items.flowsheet_nodes_equipment_item && Array.isArray(equipment_items.flowsheet_nodes_equipment_item) &&
                              equipment_items.flowsheet_nodes_equipment_item.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>
                        {!equipment_items?.flowsheet_nodes_equipment_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Capex_items EquipmentItem</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>CostCategory</th>
                                
                                
                                
                                
                                
                                <th>Quantity</th>
                                
                                
                                
                                <th>UnitCost</th>
                                
                                
                                
                                <th>InstallFactor</th>
                                
                                
                                
                                <th>TotalCost</th>
                                
                                
                                
                                <th>EstimateClass</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {equipment_items.capex_items_equipment_item && Array.isArray(equipment_items.capex_items_equipment_item) &&
                              equipment_items.capex_items_equipment_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/capex_items/capex_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="cost_category">
                                        { item.cost_category }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_cost">
                                        { item.unit_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="install_factor">
                                        { item.install_factor }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_cost">
                                        { item.total_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="estimate_class">
                                        { item.estimate_class }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!equipment_items?.capex_items_equipment_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Equipment_itemsView;