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/vehicles/vehiclesSlice'
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 VehiclesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { vehicles } = useAppSelector((state) => state.vehicles)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>VehicleNumber</p>
                    <p>{vehicles?.vehicle_number}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>LicensePlate</p>
                    <p>{vehicles?.license_plate}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>VIN</p>
                    <p>{vehicles?.vin}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Job_logs Vehicle</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>WorkDate</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>HoursConducted</th>
                                
                                
                                
                                <th>ClientPaid</th>
                                
                                
                                
                                <th>WorkmansCompensationClass</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>OdometerStart</th>
                                
                                
                                
                                <th>OdometerEnd</th>
                                
                                
                                
                                <th>JobAddress</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>NotesToAdmin</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {vehicles.job_logs_vehicle && Array.isArray(vehicles.job_logs_vehicle) &&
                              vehicles.job_logs_vehicle.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/job_logs/job_logs-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="work_date">
                                        { dataFormatter.dateTimeFormatter(item.work_date) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="hours_conducted">
                                        { item.hours_conducted }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="client_paid">
                                        { item.client_paid }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="workers_comp_class">
                                        { item.workers_comp_class }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="odometer_start">
                                        { item.odometer_start }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="odometer_end">
                                        { item.odometer_end }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="job_address">
                                        { item.job_address }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes_to_admin">
                                        { item.notes_to_admin }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!vehicles?.job_logs_vehicle?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                

                <BaseDivider />

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

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

export default VehiclesView;