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/pharmacies/pharmaciesSlice'
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 PharmaciesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { pharmacies } = useAppSelector((state) => state.pharmacies)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>User</p>
                    
                    
                        <p>{pharmacies?.user?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PharmacyName</p>
                    <p>{pharmacies?.pharmacy_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Phone</p>
                    <p>{pharmacies?.phone}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Medications Pharmacy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>MedicationName</th>
                                
                                
                                
                                <th>DoseAmount</th>
                                
                                
                                
                                <th>DoseUnit</th>
                                
                                
                                
                                <th>Frequency</th>
                                
                                
                                
                                <th>EveryXHours</th>
                                
                                
                                
                                <th>FrequencyNotes</th>
                                
                                
                                
                                <th>Route</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>ConditionTreated</th>
                                
                                
                                
                                <th>StartDate</th>
                                
                                
                                
                                <th>EndDate</th>
                                
                                
                                
                                <th>RefillReminderDate</th>
                                
                                
                                
                                <th>RemindMetoTakeIt</th>
                                
                                
                                
                                <th>ControlledSubstance</th>
                                
                                
                                
                                
                                
                                <th>Instructions</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {pharmacies.medications_pharmacy && Array.isArray(pharmacies.medications_pharmacy) &&
                              pharmacies.medications_pharmacy.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/medications/medications-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="medication_name">
                                        { item.medication_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="dose_amount">
                                        { item.dose_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="dose_unit">
                                        { item.dose_unit }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="frequency_type">
                                        { item.frequency_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="frequency_interval_hours">
                                        { item.frequency_interval_hours }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="frequency_notes">
                                        { item.frequency_notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="route">
                                        { item.route }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="condition_treated">
                                        { item.condition_treated }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="start_date">
                                        { dataFormatter.dateTimeFormatter(item.start_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="end_date">
                                        { dataFormatter.dateTimeFormatter(item.end_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="refill_reminder_date">
                                        { dataFormatter.dateTimeFormatter(item.refill_reminder_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reminders_enabled">
                                        { dataFormatter.booleanFormatter(item.reminders_enabled) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_controlled_substance">
                                        { dataFormatter.booleanFormatter(item.is_controlled_substance) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="instructions">
                                        { item.instructions }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!pharmacies?.medications_pharmacy?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default PharmaciesView;