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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Patient</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{prescriptions?.patient?.patient_identifier ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Consultation</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{prescriptions?.consultation?.chief_complaint ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PrescribedByDoctor</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{prescriptions?.prescribed_by_doctor?.specialization ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PrescriptionStatus</p>
                    <p>{prescriptions?.prescription_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Notes</p>
                  {prescriptions.notes
                    ? <p dangerouslySetInnerHTML={{__html: prescriptions.notes}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PrescriptionFiles</p>
                    {prescriptions?.prescription_files?.length
                      ? dataFormatter.filesFormatter(prescriptions.prescription_files).map(link => (
                        <button
                          key={link.publicUrl}
                          onClick={(e) => saveFile(e, link.publicUrl, link.name)}
                        >
                            {link.name}
                        </button>
                      )) : <p>No PrescriptionFiles</p>
                    }
                </div>
              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Prescription_items Prescription</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>DoseInstructions</th>
                                
                                
                                
                                <th>Frequency</th>
                                
                                
                                
                                <th>DurationDays</th>
                                
                                
                                
                                <th>QuantityPrescribed</th>
                                
                                
                                
                                <th>SubstitutionAllowed</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {prescriptions.prescription_items_prescription && Array.isArray(prescriptions.prescription_items_prescription) &&
                              prescriptions.prescription_items_prescription.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/prescription_items/prescription_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="dose_instructions">
                                        { item.dose_instructions }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="frequency">
                                        { item.frequency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="duration_days">
                                        { item.duration_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity_prescribed">
                                        { item.quantity_prescribed }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="substitution_allowed">
                                        { dataFormatter.booleanFormatter(item.substitution_allowed) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!prescriptions?.prescription_items_prescription?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Medicine_dispatches Prescription</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>DispatchedAt</th>
                                
                                
                                
                                <th>ExpectedDeliveryAt</th>
                                
                                
                                
                                <th>DeliveredAt</th>
                                
                                
                                
                                <th>DispatchStatus</th>
                                
                                
                                
                                <th>TrackingReference</th>
                                
                                
                                
                                <th>LastKnownLatitude</th>
                                
                                
                                
                                <th>LastKnownLongitude</th>
                                
                                
                                
                                <th>DeliveryNotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {prescriptions.medicine_dispatches_prescription && Array.isArray(prescriptions.medicine_dispatches_prescription) &&
                              prescriptions.medicine_dispatches_prescription.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/medicine_dispatches/medicine_dispatches-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="dispatched_at">
                                        { dataFormatter.dateTimeFormatter(item.dispatched_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expected_delivery_at">
                                        { dataFormatter.dateTimeFormatter(item.expected_delivery_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="delivered_at">
                                        { dataFormatter.dateTimeFormatter(item.delivered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="dispatch_status">
                                        { item.dispatch_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tracking_reference">
                                        { item.tracking_reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_known_latitude">
                                        { item.last_known_latitude }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_known_longitude">
                                        { item.last_known_longitude }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="delivery_notes">
                                        { item.delivery_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!prescriptions?.medicine_dispatches_prescription?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default PrescriptionsView;