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/ponds/pondsSlice'
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 PondsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { ponds } = useAppSelector((state) => state.ponds)
    
    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 ponds')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View ponds')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/ponds/ponds-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Tenant</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{ponds?.tenant?.tenant_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Location</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{ponds?.location?.location_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PondName</p>
                    <p>{ponds?.pond_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PondType</p>
                    <p>{ponds?.pond_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>AreaSqM</p>
                  <p>{ponds?.area_sq_m || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>AverageDepthM</p>
                  <p>{ponds?.avg_depth_m || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Batches Pond</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>
                            {ponds.batches_pond && Array.isArray(ponds.batches_pond) &&
                              ponds.batches_pond.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>
                        {!ponds?.batches_pond?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Water_quality_logs Pond</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>SampledAt</th>
                                
                                
                                
                                <th>TemperatureC</th>
                                
                                
                                
                                <th>pH</th>
                                
                                
                                
                                <th>DissolvedOxygenMgL</th>
                                
                                
                                
                                <th>SalinityPpt</th>
                                
                                
                                
                                <th>AmmoniaMgL</th>
                                
                                
                                
                                <th>NitriteMgL</th>
                                
                                
                                
                                <th>NitrateMgL</th>
                                
                                
                                
                                <th>TurbidityNTU</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ponds.water_quality_logs_pond && Array.isArray(ponds.water_quality_logs_pond) &&
                              ponds.water_quality_logs_pond.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/water_quality_logs/water_quality_logs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sampled_at">
                                        { dataFormatter.dateTimeFormatter(item.sampled_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="temperature_c">
                                        { item.temperature_c }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ph">
                                        { item.ph }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="dissolved_oxygen_mg_l">
                                        { item.dissolved_oxygen_mg_l }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="salinity_ppt">
                                        { item.salinity_ppt }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ammonia_mg_l">
                                        { item.ammonia_mg_l }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="nitrite_mg_l">
                                        { item.nitrite_mg_l }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="nitrate_mg_l">
                                        { item.nitrate_mg_l }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="turbidity_ntu">
                                        { item.turbidity_ntu }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ponds?.water_quality_logs_pond?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default PondsView;