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/pregnancies/pregnanciesSlice'
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 PregnanciesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { pregnancies } = useAppSelector((state) => state.pregnancies)
    

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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Pregnancy_symptom_logs Pregnancy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>LoggedAt</th>
                                
                                
                                
                                <th>Nausea(1-10)</th>
                                
                                
                                
                                <th>Vomiting(1-10)</th>
                                
                                
                                
                                <th>Fatigue(1-10)</th>
                                
                                
                                
                                <th>Heartburn(1-10)</th>
                                
                                
                                
                                <th>Swelling(1-10)</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {pregnancies.pregnancy_symptom_logs_pregnancy && Array.isArray(pregnancies.pregnancy_symptom_logs_pregnancy) &&
                              pregnancies.pregnancy_symptom_logs_pregnancy.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/pregnancy_symptom_logs/pregnancy_symptom_logs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="logged_at">
                                        { dataFormatter.dateTimeFormatter(item.logged_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="nausea">
                                        { item.nausea }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vomiting">
                                        { item.vomiting }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="fatigue">
                                        { item.fatigue }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="heartburn">
                                        { item.heartburn }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="swelling">
                                        { item.swelling }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!pregnancies?.pregnancy_symptom_logs_pregnancy?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Breastfeeding_logs Pregnancy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>EndedAt</th>
                                
                                
                                
                                <th>Side</th>
                                
                                
                                
                                <th>Duration(minutes)</th>
                                
                                
                                
                                <th>LatchNotes</th>
                                
                                
                                
                                <th>ProductionNotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {pregnancies.breastfeeding_logs_pregnancy && Array.isArray(pregnancies.breastfeeding_logs_pregnancy) &&
                              pregnancies.breastfeeding_logs_pregnancy.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/breastfeeding_logs/breastfeeding_logs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ended_at">
                                        { dataFormatter.dateTimeFormatter(item.ended_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="side">
                                        { item.side }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="duration_minutes">
                                        { item.duration_minutes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="latch_notes">
                                        { item.latch_notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="production_notes">
                                        { item.production_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!pregnancies?.breastfeeding_logs_pregnancy?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Formula_feeding_logs Pregnancy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>FedAt</th>
                                
                                
                                
                                <th>Volume(ml)</th>
                                
                                
                                
                                <th>FormulaBrand</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {pregnancies.formula_feeding_logs_pregnancy && Array.isArray(pregnancies.formula_feeding_logs_pregnancy) &&
                              pregnancies.formula_feeding_logs_pregnancy.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/formula_feeding_logs/formula_feeding_logs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="fed_at">
                                        { dataFormatter.dateTimeFormatter(item.fed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="volume_ml">
                                        { item.volume_ml }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="formula_brand">
                                        { item.formula_brand }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!pregnancies?.formula_feeding_logs_pregnancy?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Baby_metrics Pregnancy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>MeasuredAt</th>
                                
                                
                                
                                <th>Weight(kg)</th>
                                
                                
                                
                                <th>Length(cm)</th>
                                
                                
                                
                                <th>HeadCircumference(cm)</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {pregnancies.baby_metrics_pregnancy && Array.isArray(pregnancies.baby_metrics_pregnancy) &&
                              pregnancies.baby_metrics_pregnancy.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/baby_metrics/baby_metrics-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="measured_at">
                                        { dataFormatter.dateTimeFormatter(item.measured_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="weight_kg">
                                        { item.weight_kg }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="length_cm">
                                        { item.length_cm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="head_circumference_cm">
                                        { item.head_circumference_cm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!pregnancies?.baby_metrics_pregnancy?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Epds_assessments Pregnancy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>TakenAt</th>
                                
                                
                                
                                <th>TotalScore</th>
                                
                                
                                
                                <th>ResponsesJSON</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {pregnancies.epds_assessments_pregnancy && Array.isArray(pregnancies.epds_assessments_pregnancy) &&
                              pregnancies.epds_assessments_pregnancy.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/epds_assessments/epds_assessments-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="taken_at">
                                        { dataFormatter.dateTimeFormatter(item.taken_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="score_total">
                                        { item.score_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="responses_json">
                                        { item.responses_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!pregnancies?.epds_assessments_pregnancy?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Kick_counts Pregnancy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>SessionStart</th>
                                
                                
                                
                                <th>SessionEnd</th>
                                
                                
                                
                                <th>KicksCount</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {pregnancies.kick_counts_pregnancy && Array.isArray(pregnancies.kick_counts_pregnancy) &&
                              pregnancies.kick_counts_pregnancy.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/kick_counts/kick_counts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="session_start">
                                        { dataFormatter.dateTimeFormatter(item.session_start) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="session_end">
                                        { dataFormatter.dateTimeFormatter(item.session_end) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="kicks_count">
                                        { item.kicks_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!pregnancies?.kick_counts_pregnancy?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Contraction_sessions Pregnancy</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>LabourStart</th>
                                
                                
                                
                                <th>LabourEnd</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {pregnancies.contraction_sessions_pregnancy && Array.isArray(pregnancies.contraction_sessions_pregnancy) &&
                              pregnancies.contraction_sessions_pregnancy.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/contraction_sessions/contraction_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="labour_start">
                                        { dataFormatter.dateTimeFormatter(item.labour_start) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="labour_end">
                                        { dataFormatter.dateTimeFormatter(item.labour_end) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!pregnancies?.contraction_sessions_pregnancy?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default PregnanciesView;