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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ClusterType</p>
                    <p>{infrastructure_clusters?.cluster_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Region</p>
                    <p>{infrastructure_clusters?.region}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Provider</p>
                    <p>{infrastructure_clusters?.provider}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>APIGatewayURL</p>
                    <p>{infrastructure_clusters?.api_gateway_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CPUCapacity</p>
                  <p>{infrastructure_clusters?.cpu_capacity || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>GPUCapacity</p>
                  <p>{infrastructure_clusters?.gpu_capacity || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>StorageCapacityTB</p>
                  <p>{infrastructure_clusters?.storage_capacity_tb || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Edge_sensors Cluster</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>SensorName</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Location</th>
                                
                                
                                
                                <th>Site</th>
                                
                                
                                
                                <th>HardwareModel</th>
                                
                                
                                
                                <th>FirmwareVersion</th>
                                
                                
                                
                                <th>LastSeenAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {infrastructure_clusters.edge_sensors_cluster && Array.isArray(infrastructure_clusters.edge_sensors_cluster) &&
                              infrastructure_clusters.edge_sensors_cluster.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/edge_sensors/edge_sensors-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="location">
                                        { item.location }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="site">
                                        { item.site }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="hardware_model">
                                        { item.hardware_model }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="firmware_version">
                                        { item.firmware_version }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_seen_at">
                                        { dataFormatter.dateTimeFormatter(item.last_seen_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!infrastructure_clusters?.edge_sensors_cluster?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                

                <BaseDivider />

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

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

export default Infrastructure_clustersView;