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/routes/routesSlice'
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 RoutesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { routes } = useAppSelector((state) => state.routes)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RouteName</p>
                    <p>{routes?.route_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RouteCode</p>
                    <p>{routes?.route_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RouteStatus</p>
                    <p>{routes?.route_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Operations Route</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>OperationName</th>
                                
                                
                                
                                <th>OperationCode</th>
                                
                                
                                
                                <th>Sequence</th>
                                
                                
                                
                                <th>SetupTime(Minutes)</th>
                                
                                
                                
                                <th>RunTime(Minutes/Unit)</th>
                                
                                
                                
                                <th>LaborTime(Minutes/Unit)</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {routes.operations_route && Array.isArray(routes.operations_route) &&
                              routes.operations_route.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/operations/operations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="operation_name">
                                        { item.operation_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="operation_code">
                                        { item.operation_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sequence">
                                        { item.sequence }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="setup_time_minutes">
                                        { item.setup_time_minutes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="run_time_minutes_per_unit">
                                        { item.run_time_minutes_per_unit }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="labor_time_minutes_per_unit">
                                        { item.labor_time_minutes_per_unit }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!routes?.operations_route?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Work_orders Route</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>Status</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>PlannedQuantity</th>
                                
                                
                                
                                <th>CompletedQuantity</th>
                                
                                
                                
                                
                                
                                <th>ScheduledStart</th>
                                
                                
                                
                                <th>ScheduledEnd</th>
                                
                                
                                
                                <th>ActualStart</th>
                                
                                
                                
                                <th>ActualEnd</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {routes.work_orders_route && Array.isArray(routes.work_orders_route) &&
                              routes.work_orders_route.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/work_orders/work_orders-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="work_order_number">
                                        { item.work_order_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="planned_quantity">
                                        { item.planned_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completed_quantity">
                                        { item.completed_quantity }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="scheduled_start">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_start) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_end">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_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="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!routes?.work_orders_route?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default RoutesView;