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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>WorkCenterCode</p>
                    <p>{work_centers?.code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Type</p>
                    <p>{work_centers?.type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Machines WorkCenter</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>MachineName</th>
                                
                                
                                
                                <th>AssetTag</th>
                                
                                
                                
                                <th>Manufacturer</th>
                                
                                
                                
                                <th>Model</th>
                                
                                
                                
                                <th>SerialNumber</th>
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>CommissionedOn</th>
                                
                                
                                
                                <th>Active</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {work_centers.machines_work_center && Array.isArray(work_centers.machines_work_center) &&
                              work_centers.machines_work_center.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/machines/machines-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="asset_tag">
                                        { item.asset_tag }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="manufacturer">
                                        { item.manufacturer }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="model">
                                        { item.model }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="serial_number">
                                        { item.serial_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="commissioned_on">
                                        { dataFormatter.dateTimeFormatter(item.commissioned_on) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="active">
                                        { dataFormatter.booleanFormatter(item.active) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!work_centers?.machines_work_center?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Routing_steps WorkCenter</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>StepNumber</th>
                                
                                
                                
                                <th>StepName</th>
                                
                                
                                
                                
                                
                                <th>SetupTimeMinutes</th>
                                
                                
                                
                                <th>RunTimeMinutes</th>
                                
                                
                                
                                <th>LaborMinutes</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {work_centers.routing_steps_work_center && Array.isArray(work_centers.routing_steps_work_center) &&
                              work_centers.routing_steps_work_center.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/routing_steps/routing_steps-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="step_number">
                                        { item.step_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="setup_time_minutes">
                                        { item.setup_time_minutes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="run_time_minutes">
                                        { item.run_time_minutes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="labor_minutes">
                                        { item.labor_minutes }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!work_centers?.routing_steps_work_center?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Production_operations WorkCenter</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>
                            {work_centers.production_operations_work_center && Array.isArray(work_centers.production_operations_work_center) &&
                              work_centers.production_operations_work_center.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>
                        {!work_centers?.production_operations_work_center?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Work_centersView;