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/surgery_cases/surgery_casesSlice'
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 Surgery_casesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { surgery_cases } = useAppSelector((state) => state.surgery_cases)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>رقمالعملية</p>
                    <p>{surgery_cases?.case_number}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>المريض</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{surgery_cases?.patient?.full_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>الجراح</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{surgery_cases?.surgeon?.full_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              
                <FormField label='وقتبدئالعملية'>
                    {surgery_cases.scheduled_start ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={surgery_cases.scheduled_start ?
                        new Date(
                          dayjs(surgery_cases.scheduled_start).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No وقتبدئالعملية</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='وقتانتهاءالعملية'>
                    {surgery_cases.scheduled_end ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={surgery_cases.scheduled_end ?
                        new Date(
                          dayjs(surgery_cases.scheduled_end).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No وقتانتهاءالعملية</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>تفاصيلالعملية</p>
                  {surgery_cases.surgical_details
                    ? <p dangerouslySetInnerHTML={{__html: surgery_cases.surgical_details}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>سعرالعملية</p>
                  <p>{surgery_cases?.price || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>المستهلكات</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>اسمالمستهلك</th>
                                
                                
                                
                                    <th>الكمية</th>
                                
                                
                                
                                    <th>سعرالوحدة</th>
                                
                                
                                
                                    <th>الإجمالي</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {surgery_cases.consumables && Array.isArray(surgery_cases.consumables) &&
                              surgery_cases.consumables.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/surgery_consumables/surgery_consumables-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_price">
                                        { item.unit_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_price">
                                        { item.total_price }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!surgery_cases?.consumables?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>حالةالعملية</p>
                    <p>{surgery_cases?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Surgery_consumables الحالةالجراحية</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>اسمالمستهلك</th>
                                
                                
                                
                                <th>الكمية</th>
                                
                                
                                
                                <th>سعرالوحدة</th>
                                
                                
                                
                                <th>الإجمالي</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {surgery_cases.surgery_consumables_surgery_case && Array.isArray(surgery_cases.surgery_consumables_surgery_case) &&
                              surgery_cases.surgery_consumables_surgery_case.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/surgery_consumables/surgery_consumables-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_price">
                                        { item.unit_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_price">
                                        { item.total_price }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!surgery_cases?.surgery_consumables_surgery_case?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Anesthesia_records الحالةالجراحية</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>موادالتخدير</th>
                                
                                
                                
                                <th>ساعةالدخول</th>
                                
                                
                                
                                <th>ساعةالخروج</th>
                                
                                
                                
                                <th>سجلالاستفاقة</th>
                                
                                
                                
                                <th>سعرالتخدير</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {surgery_cases.anesthesia_records_surgery_case && Array.isArray(surgery_cases.anesthesia_records_surgery_case) &&
                              surgery_cases.anesthesia_records_surgery_case.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/anesthesia_records/anesthesia_records-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="medications">
                                        { item.medications }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="start_time">
                                        { dataFormatter.dateTimeFormatter(item.start_time) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="end_time">
                                        { dataFormatter.dateTimeFormatter(item.end_time) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="recovery_notes">
                                        { item.recovery_notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="price">
                                        { item.price }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!surgery_cases?.anesthesia_records_surgery_case?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Surgery_casesView;