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/medicines/medicinesSlice'
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 MedicinesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { medicines } = useAppSelector((state) => state.medicines)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>MedicineName</p>
                    <p>{medicines?.medicine_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>GenericName</p>
                    <p>{medicines?.generic_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Category</p>
                    <p>{medicines?.category ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Strength</p>
                    <p>{medicines?.strength}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Unit</p>
                    <p>{medicines?.unit}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Manufacturer</p>
                    <p>{medicines?.manufacturer}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>UnitPrice</p>
                  <p>{medicines?.unit_price || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='RequiresPrescription'>
                    <SwitchField
                      field={{name: 'requires_prescription', value: medicines?.requires_prescription}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ReorderLevel</p>
                  <p>{medicines?.reorder_level || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Inventory_updates Medicine</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>UpdateType</th>
                                
                                
                                
                                <th>QuantityChange</th>
                                
                                
                                
                                <th>QuantityAfter</th>
                                
                                
                                
                                <th>Reason</th>
                                
                                
                                
                                <th>UpdatedOn</th>
                                
                                
                                
                                <th>IsShortageAlert</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {medicines.inventory_updates_medicine && Array.isArray(medicines.inventory_updates_medicine) &&
                              medicines.inventory_updates_medicine.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/inventory_updates/inventory_updates-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="update_type">
                                        { item.update_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity_change">
                                        { item.quantity_change }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity_after">
                                        { item.quantity_after }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reason">
                                        { item.reason }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="updated_on">
                                        { dataFormatter.dateTimeFormatter(item.updated_on) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_shortage_alert">
                                        { dataFormatter.booleanFormatter(item.is_shortage_alert) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!medicines?.inventory_updates_medicine?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Dispatch_items Medicine</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>
                            {medicines.dispatch_items_medicine && Array.isArray(medicines.dispatch_items_medicine) &&
                              medicines.dispatch_items_medicine.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>
                        {!medicines?.dispatch_items_medicine?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default MedicinesView;