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/places/placesSlice'
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 PlacesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { places } = useAppSelector((state) => state.places)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>City</p>
                    <p>{places?.city}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Province</p>
                    <p>{places?.province}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PostalCode</p>
                    <p>{places?.postal_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Latitude</p>
                  <p>{places?.latitude || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Longitude</p>
                  <p>{places?.longitude || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PhoneNumber</p>
                    <p>{places?.phone_number}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>WhatsAppNumber</p>
                    <p>{places?.whatsapp_number}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Email</p>
                    <p>{places?.email}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>WebsiteURL</p>
                    <p>{places?.website_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>GoogleMapsURL</p>
                    <p>{places?.google_maps_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PriceLevel</p>
                    <p>{places?.price_level ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>AveragePrice</p>
                  <p>{places?.average_price || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>RatingAverage</p>
                  <p>{places?.rating_average || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>RatingCount</p>
                  <p>{places?.rating_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsVerified'>
                    <SwitchField
                      field={{name: 'is_verified', value: places?.is_verified}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Owner</p>
                    
                    
                        <p>{places?.owner?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Place_opening_hours Place</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>DayofWeek</th>
                                
                                
                                
                                <th>OpenTime</th>
                                
                                
                                
                                <th>CloseTime</th>
                                
                                
                                
                                <th>IsClosed</th>
                                
                                
                                
                                <th>Is24Hours</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {places.place_opening_hours_place && Array.isArray(places.place_opening_hours_place) &&
                              places.place_opening_hours_place.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/place_opening_hours/place_opening_hours-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="day_of_week">
                                        { item.day_of_week }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="open_time">
                                        { item.open_time }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="close_time">
                                        { item.close_time }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_closed">
                                        { dataFormatter.booleanFormatter(item.is_closed) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_24_hours">
                                        { dataFormatter.booleanFormatter(item.is_24_hours) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!places?.place_opening_hours_place?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Place_feature_links Place</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {places.place_feature_links_place && Array.isArray(places.place_feature_links_place) &&
                              places.place_feature_links_place.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/place_feature_links/place_feature_links-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!places?.place_feature_links_place?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Reviews Place</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Rating</th>
                                
                                
                                
                                <th>Comment</th>
                                
                                
                                
                                
                                
                                <th>ModerationStatus</th>
                                
                                
                                
                                <th>VisitedAt</th>
                                
                                
                                
                                <th>IsAnonymous</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {places.reviews_place && Array.isArray(places.reviews_place) &&
                              places.reviews_place.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/reviews/reviews-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="rating">
                                        { item.rating }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="comment">
                                        { item.comment }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="moderation_status">
                                        { item.moderation_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="visited_at">
                                        { dataFormatter.dateTimeFormatter(item.visited_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_anonymous">
                                        { dataFormatter.booleanFormatter(item.is_anonymous) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!places?.reviews_place?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Favorites Place</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>FavoritedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {places.favorites_place && Array.isArray(places.favorites_place) &&
                              places.favorites_place.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/favorites/favorites-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="favorited_at">
                                        { dataFormatter.dateTimeFormatter(item.favorited_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!places?.favorites_place?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Reports Place</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>ReportType</th>
                                
                                
                                
                                <th>Details</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>ReportedAt</th>
                                
                                
                                
                                <th>ResolvedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {places.reports_place && Array.isArray(places.reports_place) &&
                              places.reports_place.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/reports/reports-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="report_type">
                                        { item.report_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="details">
                                        { item.details }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reported_at">
                                        { dataFormatter.dateTimeFormatter(item.reported_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="resolved_at">
                                        { dataFormatter.dateTimeFormatter(item.resolved_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!places?.reports_place?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                

                <BaseDivider />

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

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

export default PlacesView;