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/medicine_dispatches/medicine_dispatchesSlice'
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 Medicine_dispatchesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { medicine_dispatches } = useAppSelector((state) => state.medicine_dispatches)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RequestedByRuralDoctor</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{medicine_dispatches?.requested_by_rural_doctor?.full_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ApprovedByAdmin</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{medicine_dispatches?.approved_by_admin?.full_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CoordinatedByCityDoctor</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{medicine_dispatches?.coordinated_by_city_doctor?.full_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DestinationAddress</p>
                    <p>{medicine_dispatches?.destination_address}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>DestinationLatitude</p>
                  <p>{medicine_dispatches?.destination_latitude || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>DestinationLongitude</p>
                  <p>{medicine_dispatches?.destination_longitude || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TrackingReference</p>
                    <p>{medicine_dispatches?.tracking_reference}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Dispatch_items MedicineDispatch</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Quantity</th>
                                
                                
                                
                                <th>DosageInstructions</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {medicine_dispatches.dispatch_items_medicine_dispatch && Array.isArray(medicine_dispatches.dispatch_items_medicine_dispatch) &&
                              medicine_dispatches.dispatch_items_medicine_dispatch.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/dispatch_items/dispatch_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="dosage_instructions">
                                        { item.dosage_instructions }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!medicine_dispatches?.dispatch_items_medicine_dispatch?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Truck_tracking_events MedicineDispatch</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>EventAt</th>
                                
                                
                                
                                <th>Latitude</th>
                                
                                
                                
                                <th>Longitude</th>
                                
                                
                                
                                <th>SpeedKph</th>
                                
                                
                                
                                <th>Heading</th>
                                
                                
                                
                                <th>EventNote</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {medicine_dispatches.truck_tracking_events_medicine_dispatch && Array.isArray(medicine_dispatches.truck_tracking_events_medicine_dispatch) &&
                              medicine_dispatches.truck_tracking_events_medicine_dispatch.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/truck_tracking_events/truck_tracking_events-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="event_at">
                                        { dataFormatter.dateTimeFormatter(item.event_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="latitude">
                                        { item.latitude }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="longitude">
                                        { item.longitude }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="speed_kph">
                                        { item.speed_kph }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="heading">
                                        { item.heading }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="event_note">
                                        { item.event_note }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!medicine_dispatches?.truck_tracking_events_medicine_dispatch?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Medicine_dispatchesView;