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/species/speciesSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";


const SpeciesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { species } = useAppSelector((state) => state.species)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SpeciesName</p>
                    <p>{species?.species_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ScientificName</p>
                    <p>{species?.scientific_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TypicalHarvestSize</p>
                    <p>{species?.typical_harvest_size}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>organizations</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{species?.organizations?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Batches Species</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>BatchCode</th>
                                
                                
                                
                                <th>StockedAt</th>
                                
                                
                                
                                <th>InitialCount</th>
                                
                                
                                
                                <th>InitialAverageWeightG</th>
                                
                                
                                
                                <th>BatchStatus</th>
                                
                                
                                
                                <th>ExpectedHarvestAt</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {species.batches_species && Array.isArray(species.batches_species) &&
                              species.batches_species.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/batches/batches-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="batch_code">
                                        { item.batch_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stocked_at">
                                        { dataFormatter.dateTimeFormatter(item.stocked_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="initial_count">
                                        { item.initial_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="initial_avg_weight_g">
                                        { item.initial_avg_weight_g }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="batch_status">
                                        { item.batch_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expected_harvest_at">
                                        { dataFormatter.dateTimeFormatter(item.expected_harvest_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!species?.batches_species?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Marketplace_listings Species</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>ListingTitle</th>
                                
                                
                                
                                
                                
                                <th>ProductForm</th>
                                
                                
                                
                                <th>AvailableQuantityKg</th>
                                
                                
                                
                                <th>PricePerKg</th>
                                
                                
                                
                                <th>Currency</th>
                                
                                
                                
                                <th>AvailableFrom</th>
                                
                                
                                
                                <th>ExpiresAt</th>
                                
                                
                                
                                <th>ListingStatus</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {species.marketplace_listings_species && Array.isArray(species.marketplace_listings_species) &&
                              species.marketplace_listings_species.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/marketplace_listings/marketplace_listings-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="listing_title">
                                        { item.listing_title }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="product_form">
                                        { item.product_form }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="available_quantity_kg">
                                        { item.available_quantity_kg }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="price_per_kg">
                                        { item.price_per_kg }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currency">
                                        { item.currency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="available_from">
                                        { dataFormatter.dateTimeFormatter(item.available_from) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expires_at">
                                        { dataFormatter.dateTimeFormatter(item.expires_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="listing_status">
                                        { item.listing_status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!species?.marketplace_listings_species?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default SpeciesView;