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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AssetTag</p>
                    <p>{machines?.asset_tag}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SerialNumber</p>
                    <p>{machines?.serial_number}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Active'>
                    <SwitchField
                      field={{name: 'active', value: machines?.active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Production_operations Machine</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>OperationNumber</th>
                                
                                
                                
                                <th>OperationName</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>PlannedStart</th>
                                
                                
                                
                                <th>PlannedEnd</th>
                                
                                
                                
                                <th>ActualStart</th>
                                
                                
                                
                                <th>ActualEnd</th>
                                
                                
                                
                                <th>GoodQuantity</th>
                                
                                
                                
                                <th>ScrapQuantity</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {machines.production_operations_machine && Array.isArray(machines.production_operations_machine) &&
                              machines.production_operations_machine.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/production_operations/production_operations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="operation_number">
                                        { item.operation_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="planned_start">
                                        { dataFormatter.dateTimeFormatter(item.planned_start) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="planned_end">
                                        { dataFormatter.dateTimeFormatter(item.planned_end) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="actual_start">
                                        { dataFormatter.dateTimeFormatter(item.actual_start) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="actual_end">
                                        { dataFormatter.dateTimeFormatter(item.actual_end) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="good_qty">
                                        { item.good_qty }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scrap_qty">
                                        { item.scrap_qty }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!machines?.production_operations_machine?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Nonconformances Machine</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>NCRNumber</th>
                                
                                
                                
                                <th>Source</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Severity</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>ReportedAt</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {machines.nonconformances_machine && Array.isArray(machines.nonconformances_machine) &&
                              machines.nonconformances_machine.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/nonconformances/nonconformances-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="ncr_number">
                                        { item.ncr_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="source">
                                        { item.source }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="severity">
                                        { item.severity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reported_at">
                                        { dataFormatter.dateTimeFormatter(item.reported_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!machines?.nonconformances_machine?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Downtime_events Machine</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>EventNumber</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>ReasonCategory</th>
                                
                                
                                
                                <th>ReasonDetail</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>EndedAt</th>
                                
                                
                                
                                <th>Planned</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {machines.downtime_events_machine && Array.isArray(machines.downtime_events_machine) &&
                              machines.downtime_events_machine.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/downtime_events/downtime_events-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="event_number">
                                        { item.event_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="reason_category">
                                        { item.reason_category }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reason_detail">
                                        { item.reason_detail }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ended_at">
                                        { dataFormatter.dateTimeFormatter(item.ended_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="planned">
                                        { dataFormatter.booleanFormatter(item.planned) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!machines?.downtime_events_machine?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Machine_readings Machine</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>RecordedAt</th>
                                
                                
                                
                                <th>RunMinutes</th>
                                
                                
                                
                                <th>DownMinutes</th>
                                
                                
                                
                                <th>GoodCount</th>
                                
                                
                                
                                <th>ScrapCount</th>
                                
                                
                                
                                <th>CycleTimeSeconds</th>
                                
                                
                                
                                <th>EnergykWh</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {machines.machine_readings_machine && Array.isArray(machines.machine_readings_machine) &&
                              machines.machine_readings_machine.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/machine_readings/machine_readings-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="recorded_at">
                                        { dataFormatter.dateTimeFormatter(item.recorded_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="run_minutes">
                                        { item.run_minutes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="down_minutes">
                                        { item.down_minutes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="good_count">
                                        { item.good_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scrap_count">
                                        { item.scrap_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="cycle_time_seconds">
                                        { item.cycle_time_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="energy_kwh">
                                        { item.energy_kwh }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!machines?.machine_readings_machine?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Maintenance_work_orders Machine</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>WorkOrderNumber</th>
                                
                                
                                
                                
                                
                                <th>WorkType</th>
                                
                                
                                
                                <th>Priority</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>RequestedAt</th>
                                
                                
                                
                                <th>ScheduledStart</th>
                                
                                
                                
                                <th>ScheduledEnd</th>
                                
                                
                                
                                <th>CompletedAt</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {machines.maintenance_work_orders_machine && Array.isArray(machines.maintenance_work_orders_machine) &&
                              machines.maintenance_work_orders_machine.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/maintenance_work_orders/maintenance_work_orders-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="work_order_number">
                                        { item.work_order_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="work_type">
                                        { item.work_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="priority">
                                        { item.priority }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requested_at">
                                        { dataFormatter.dateTimeFormatter(item.requested_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_start">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_start) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_end">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_end) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completed_at">
                                        { dataFormatter.dateTimeFormatter(item.completed_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!machines?.maintenance_work_orders_machine?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                

                <BaseDivider />

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

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

export default MachinesView;