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/production_orders/production_ordersSlice'
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 Production_ordersView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { production_orders } = useAppSelector((state) => state.production_orders)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>OrderNumber</p>
                    <p>{production_orders?.order_number}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BOM</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{production_orders?.bom?.bom_code ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Routing</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{production_orders?.routing?.routing_code ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PlannedQuantity</p>
                  <p>{production_orders?.planned_quantity || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CompletedQuantity</p>
                  <p>{production_orders?.completed_quantity || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Planner</p>
                    
                    
                        <p>{production_orders?.planner?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CustomerReference</p>
                    <p>{production_orders?.customer_reference}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Production_operations ProductionOrder</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>
                            {production_orders.production_operations_production_order && Array.isArray(production_orders.production_operations_production_order) &&
                              production_orders.production_operations_production_order.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_quantity">
                                        { item.good_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scrap_quantity">
                                        { item.scrap_quantity }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!production_orders?.production_operations_production_order?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Production_material_issues ProductionOrder</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>QuantityIssued</th>
                                
                                
                                
                                <th>IssuedAt</th>
                                
                                
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {production_orders.production_material_issues_production_order && Array.isArray(production_orders.production_material_issues_production_order) &&
                              production_orders.production_material_issues_production_order.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/production_material_issues/production_material_issues-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity_issued">
                                        { item.quantity_issued }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="issued_at">
                                        { dataFormatter.dateTimeFormatter(item.issued_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!production_orders?.production_material_issues_production_order?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Production_lots ProductionOrder</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ProductionLotNumber</th>
                                
                                
                                
                                <th>StartedOn</th>
                                
                                
                                
                                <th>CompletedOn</th>
                                
                                
                                
                                <th>QuantityProduced</th>
                                
                                
                                
                                
                                
                                <th>ReleaseStatus</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {production_orders.production_lots_production_order && Array.isArray(production_orders.production_lots_production_order) &&
                              production_orders.production_lots_production_order.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/production_lots/production_lots-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="lot_number">
                                        { item.lot_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_on">
                                        { dataFormatter.dateTimeFormatter(item.started_on) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completed_on">
                                        { dataFormatter.dateTimeFormatter(item.completed_on) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity_produced">
                                        { item.quantity_produced }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="release_status">
                                        { item.release_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!production_orders?.production_lots_production_order?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Qa_inspections ProductionOrder</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>InspectionNumber</th>
                                
                                
                                
                                <th>InspectionType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>ScheduledAt</th>
                                
                                
                                
                                <th>PerformedAt</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Result</th>
                                
                                
                                
                                <th>Summary</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {production_orders.qa_inspections_production_order && Array.isArray(production_orders.qa_inspections_production_order) &&
                              production_orders.qa_inspections_production_order.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/qa_inspections/qa_inspections-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="inspection_number">
                                        { item.inspection_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="inspection_type">
                                        { item.inspection_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_at">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="performed_at">
                                        { dataFormatter.dateTimeFormatter(item.performed_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="result">
                                        { item.result }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="summary">
                                        { item.summary }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!production_orders?.qa_inspections_production_order?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Nonconformances ProductionOrder</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>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>ProblemStatement</th>
                                
                                
                                
                                <th>ContainmentAction</th>
                                
                                
                                
                                <th>RootCause</th>
                                
                                
                                
                                <th>CorrectiveAction</th>
                                
                                
                                
                                <th>DueAt</th>
                                
                                
                                
                                <th>ClosedAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {production_orders.nonconformances_production_order && Array.isArray(production_orders.nonconformances_production_order) &&
                              production_orders.nonconformances_production_order.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>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="problem_statement">
                                        { item.problem_statement }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="containment_action">
                                        { item.containment_action }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="root_cause">
                                        { item.root_cause }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="corrective_action">
                                        { item.corrective_action }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_at">
                                        { dataFormatter.dateTimeFormatter(item.due_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="closed_at">
                                        { dataFormatter.dateTimeFormatter(item.closed_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!production_orders?.nonconformances_production_order?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                

                <BaseDivider />

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

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

export default Production_ordersView;