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/poultry_lots/poultry_lotsSlice'
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 Poultry_lotsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { poultry_lots } = useAppSelector((state) => state.poultry_lots)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Codelot</p>
                    <p>{poultry_lots?.code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Nomlot</p>
                    <p>{poultry_lots?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Nombreinitial</p>
                  <p>{poultry_lots?.initial_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Nombreactuel</p>
                  <p>{poultry_lots?.current_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Mortalitécumulée</p>
                  <p>{poultry_lots?.deaths_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Tauxmortalité(%)</p>
                  <p>{poultry_lots?.mortality_rate || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Emplacement</p>
                    <p>{poultry_lots?.housing_location}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Responsable</p>
                    
                    
                        <p>{poultry_lots?.manager_user?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Egg_collections Lot</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Datecollecte</th>
                                
                                
                                
                                <th>Quantité(œufs)</th>
                                
                                
                                
                                <th>Cassés</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {poultry_lots.egg_collections_poultry_lot && Array.isArray(poultry_lots.egg_collections_poultry_lot) &&
                              poultry_lots.egg_collections_poultry_lot.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/egg_collections/egg_collections-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="collected_at">
                                        { dataFormatter.dateTimeFormatter(item.collected_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="broken_quantity">
                                        { item.broken_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!poultry_lots?.egg_collections_poultry_lot?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Treatments Lot</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Type</th>
                                
                                
                                
                                <th>Produit/Médicament</th>
                                
                                
                                
                                <th>Dosage</th>
                                
                                
                                
                                <th>Dateadministration</th>
                                
                                
                                
                                <th>Prochainrappel</th>
                                
                                
                                
                                <th>Statut</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {poultry_lots.treatments_poultry_lot && Array.isArray(poultry_lots.treatments_poultry_lot) &&
                              poultry_lots.treatments_poultry_lot.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/treatments/treatments-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="treatment_type">
                                        { item.treatment_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="product_name">
                                        { item.product_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="dosage">
                                        { item.dosage }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="administered_at">
                                        { dataFormatter.dateTimeFormatter(item.administered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="next_due_at">
                                        { dataFormatter.dateTimeFormatter(item.next_due_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!poultry_lots?.treatments_poultry_lot?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Alerts Lot</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Typealerte</th>
                                
                                
                                
                                <th>Sévérité</th>
                                
                                
                                
                                <th>Titre</th>
                                
                                
                                
                                <th>Message</th>
                                
                                
                                
                                <th>Datedéclenchement</th>
                                
                                
                                
                                <th>Échéance</th>
                                
                                
                                
                                <th>Statut</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {poultry_lots.alerts_poultry_lot && Array.isArray(poultry_lots.alerts_poultry_lot) &&
                              poultry_lots.alerts_poultry_lot.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/alerts/alerts-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="type">
                                        { item.type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="severity">
                                        { item.severity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="message">
                                        { item.message }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="triggered_at">
                                        { dataFormatter.dateTimeFormatter(item.triggered_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_at">
                                        { dataFormatter.dateTimeFormatter(item.due_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!poultry_lots?.alerts_poultry_lot?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                

                <BaseDivider />

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

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

export default Poultry_lotsView;