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/plants/plantsSlice'
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";


const PlantsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { plants } = useAppSelector((state) => state.plants)
    

    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 plants')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View plants')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/plants/plants-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PlantName</p>
                    <p>{plants?.plant_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PlantCode</p>
                    <p>{plants?.plant_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Timezone</p>
                    <p>{plants?.timezone}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Warehouses Plant</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>WarehouseName</th>
                                
                                
                                
                                <th>WarehouseCode</th>
                                
                                
                                
                                
                                
                                <th>WarehouseType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {plants.warehouses_plant && Array.isArray(plants.warehouses_plant) &&
                              plants.warehouses_plant.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/warehouses/warehouses-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="warehouse_name">
                                        { item.warehouse_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="warehouse_code">
                                        { item.warehouse_code }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="warehouse_type">
                                        { item.warehouse_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!plants?.warehouses_plant?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Work_centers Plant</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>WorkCenterName</th>
                                
                                
                                
                                <th>WorkCenterCode</th>
                                
                                
                                
                                
                                
                                <th>CenterType</th>
                                
                                
                                
                                <th>CapacityUnitsPerHour</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {plants.work_centers_plant && Array.isArray(plants.work_centers_plant) &&
                              plants.work_centers_plant.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/work_centers/work_centers-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="work_center_name">
                                        { item.work_center_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="work_center_code">
                                        { item.work_center_code }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="center_type">
                                        { item.center_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="capacity_units_per_hour">
                                        { item.capacity_units_per_hour }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!plants?.work_centers_plant?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Shifts Plant</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>ShiftName</th>
                                
                                
                                
                                
                                
                                <th>ShiftType</th>
                                
                                
                                
                                <th>StartsAt</th>
                                
                                
                                
                                <th>EndsAt</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {plants.shifts_plant && Array.isArray(plants.shifts_plant) &&
                              plants.shifts_plant.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/shifts/shifts-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="shift_name">
                                        { item.shift_name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="shift_type">
                                        { item.shift_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="starts_at">
                                        { dataFormatter.dateTimeFormatter(item.starts_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ends_at">
                                        { dataFormatter.dateTimeFormatter(item.ends_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!plants?.shifts_plant?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Production_orders Plant</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>OrderNumber</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>OrderType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>PlannedQuantity</th>
                                
                                
                                
                                <th>CompletedQuantity</th>
                                
                                
                                
                                
                                
                                <th>PlannedStartAt</th>
                                
                                
                                
                                <th>PlannedEndAt</th>
                                
                                
                                
                                <th>ReleasedAt</th>
                                
                                
                                
                                <th>DueAt</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {plants.production_orders_plant && Array.isArray(plants.production_orders_plant) &&
                              plants.production_orders_plant.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/production_orders/production_orders-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="order_number">
                                        { item.order_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="order_type">
                                        { item.order_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="planned_quantity">
                                        { item.planned_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completed_quantity">
                                        { item.completed_quantity }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="planned_start_at">
                                        { dataFormatter.dateTimeFormatter(item.planned_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="planned_end_at">
                                        { dataFormatter.dateTimeFormatter(item.planned_end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="released_at">
                                        { dataFormatter.dateTimeFormatter(item.released_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_at">
                                        { dataFormatter.dateTimeFormatter(item.due_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!plants?.production_orders_plant?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Purchase_orders Plant</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>PONumber</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>OrderedAt</th>
                                
                                
                                
                                <th>ExpectedAt</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {plants.purchase_orders_plant && Array.isArray(plants.purchase_orders_plant) &&
                              plants.purchase_orders_plant.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/purchase_orders/purchase_orders-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="po_number">
                                        { item.po_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ordered_at">
                                        { dataFormatter.dateTimeFormatter(item.ordered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expected_at">
                                        { dataFormatter.dateTimeFormatter(item.expected_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!plants?.purchase_orders_plant?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Alerts RelatedPlant</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>AlertType</th>
                                
                                
                                
                                <th>Severity</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                
                                
                                <th>TriggeredAt</th>
                                
                                
                                
                                <th>AcknowledgedAt</th>
                                
                                
                                
                                <th>ResolvedAt</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {plants.alerts_related_plant && Array.isArray(plants.alerts_related_plant) &&
                              plants.alerts_related_plant.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/alerts/alerts-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="alert_type">
                                        { item.alert_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="severity">
                                        { item.severity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title_text">
                                        { item.title_text }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="triggered_at">
                                        { dataFormatter.dateTimeFormatter(item.triggered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="acknowledged_at">
                                        { dataFormatter.dateTimeFormatter(item.acknowledged_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="resolved_at">
                                        { dataFormatter.dateTimeFormatter(item.resolved_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!plants?.alerts_related_plant?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

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

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

export default PlantsView;