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_regions/coffee_regionsSlice'
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_regionsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { coffee_regions } = useAppSelector((state) => state.coffee_regions)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RegionName</p>
                    <p>{coffee_regions?.region_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Slug</p>
                    <p>{coffee_regions?.slug}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>AltitudeMin(MASL)</p>
                  <p>{coffee_regions?.altitude_min_masl || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>AltitudeMax(MASL)</p>
                  <p>{coffee_regions?.altitude_max_masl || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RainfallNote</p>
                    <p>{coffee_regions?.rainfall_note}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsActive'>
                    <SwitchField
                      field={{name: 'is_active', value: coffee_regions?.is_active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SortOrder</p>
                  <p>{coffee_regions?.sort_order || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Coffee_lots Region</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>LotCode</th>
                                
                                
                                
                                
                                
                                <th>ProcessType</th>
                                
                                
                                
                                <th>Grade</th>
                                
                                
                                
                                <th>CropYear</th>
                                
                                
                                
                                <th>MoisturePercent</th>
                                
                                
                                
                                
                                
                                <th>SCAAScore</th>
                                
                                
                                
                                <th>Certifications</th>
                                
                                
                                
                                <th>AvailableQuantity(kg)</th>
                                
                                
                                
                                <th>IsAvailable</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {coffee_regions.coffee_lots_region && Array.isArray(coffee_regions.coffee_lots_region) &&
                              coffee_regions.coffee_lots_region.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/coffee_lots/coffee_lots-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="lot_code">
                                        { item.lot_code }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="process_type">
                                        { item.process_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="grade">
                                        { item.grade }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="crop_year">
                                        { item.crop_year }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="moisture_percent">
                                        { item.moisture_percent }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="scaa_score">
                                        { item.scaa_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="certifications">
                                        { item.certifications }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="available_quantity_kg">
                                        { item.available_quantity_kg }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_available">
                                        { dataFormatter.booleanFormatter(item.is_available) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!coffee_regions?.coffee_lots_region?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Inquiries RequestedRegion</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_regions.inquiries_requested_region && Array.isArray(coffee_regions.inquiries_requested_region) &&
                              coffee_regions.inquiries_requested_region.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_regions?.inquiries_requested_region?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                

                <BaseDivider />

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

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

export default Coffee_regionsView;