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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>LotCode</p>
                    <p>{coffee_lots?.lot_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Region</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{coffee_lots?.region?.region_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ProcessType</p>
                    <p>{coffee_lots?.process_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Grade</p>
                    <p>{coffee_lots?.grade ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CropYear</p>
                  <p>{coffee_lots?.crop_year || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MoisturePercent</p>
                  <p>{coffee_lots?.moisture_percent || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TastingNotes</p>
                  {coffee_lots.tasting_notes
                    ? <p dangerouslySetInnerHTML={{__html: coffee_lots.tasting_notes}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SCAAScore</p>
                  <p>{coffee_lots?.scaa_score || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>AvailableQuantity(kg)</p>
                  <p>{coffee_lots?.available_quantity_kg || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsAvailable'>
                    <SwitchField
                      field={{name: 'is_available', value: coffee_lots?.is_available}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Images</p>
                    {coffee_lots?.images?.length
                      ? (
                        <ImageField
                          name={'images'}
                          image={coffee_lots?.images}
                          className='w-20 h-20'
                        />
                      ) : <p>No Images</p>
                    }
                </div>
              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Inquiries RequestedLot</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>InquiryType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>FullName</th>
                                
                                
                                
                                <th>Email</th>
                                
                                
                                
                                <th>Phone</th>
                                
                                
                                
                                <th>CompanyName</th>
                                
                                
                                
                                <th>Country</th>
                                
                                
                                
                                <th>BuyerType</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>TargetVolume(kg)</th>
                                
                                
                                
                                <th>Incoterm</th>
                                
                                
                                
                                <th>PreferredContactMethod</th>
                                
                                
                                
                                <th>SubmittedAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {coffee_lots.inquiries_requested_lot && Array.isArray(coffee_lots.inquiries_requested_lot) &&
                              coffee_lots.inquiries_requested_lot.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/inquiries/inquiries-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="inquiry_type">
                                        { item.inquiry_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="full_name">
                                        { item.full_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="email">
                                        { item.email }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="phone">
                                        { item.phone }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="company_name">
                                        { item.company_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="country">
                                        { item.country }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="buyer_type">
                                        { item.buyer_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="target_volume_kg">
                                        { item.target_volume_kg }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="incoterm">
                                        { item.incoterm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="preferred_contact_method">
                                        { item.preferred_contact_method }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="submitted_at">
                                        { dataFormatter.dateTimeFormatter(item.submitted_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!coffee_lots?.inquiries_requested_lot?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                

                <BaseDivider />

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

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

export default Coffee_lotsView;