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/businesses/businessesSlice'
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 BusinessesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { businesses } = useAppSelector((state) => state.businesses)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>OwnerUser</p>
                    
                    
                        <p>{businesses?.owner_user?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Phone</p>
                    <p>{businesses?.phone}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Website</p>
                    <p>{businesses?.website}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>State</p>
                    <p>{businesses?.state}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ZIP</p>
                    <p>{businesses?.zip}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

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

              

              

              

              

              

              

              

              

              
              

              

              

              

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

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AvailabilityStatus</p>
                    <p>{businesses?.availability_status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ReliabilityScore</p>
                  <p>{businesses?.reliability_score || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ResponseTimeMedianMinutes</p>
                  <p>{businesses?.response_time_median_minutes || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TenantKey</p>
                    <p>{businesses?.tenant_key}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='CreatedAt'>
                    {businesses.created_at_ts ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={businesses.created_at_ts ?
                        new Date(
                          dayjs(businesses.created_at_ts).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No CreatedAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='UpdatedAt'>
                    {businesses.updated_at_ts ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={businesses.updated_at_ts ?
                        new Date(
                          dayjs(businesses.updated_at_ts).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No UpdatedAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Business_photos Business</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>CreatedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {businesses.business_photos_business && Array.isArray(businesses.business_photos_business) &&
                              businesses.business_photos_business.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/business_photos/business_photos-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="created_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.created_at_ts) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!businesses?.business_photos_business?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Business_categories Business</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>CreatedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {businesses.business_categories_business && Array.isArray(businesses.business_categories_business) &&
                              businesses.business_categories_business.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/business_categories/business_categories-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="created_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.created_at_ts) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!businesses?.business_categories_business?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Service_prices Business</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ServiceName</th>
                                
                                
                                
                                <th>MinPrice</th>
                                
                                
                                
                                <th>MaxPrice</th>
                                
                                
                                
                                <th>TypicalPrice</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                <th>CreatedAt</th>
                                
                                
                                
                                <th>UpdatedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {businesses.service_prices_business && Array.isArray(businesses.service_prices_business) &&
                              businesses.service_prices_business.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/service_prices/service_prices-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="service_name">
                                        { item.service_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="min_price">
                                        { item.min_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_price">
                                        { item.max_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="typical_price">
                                        { item.typical_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="created_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.created_at_ts) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="updated_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.updated_at_ts) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!businesses?.service_prices_business?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Business_badges Business</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>BadgeType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>GrantedAt</th>
                                
                                
                                
                                <th>RevokedAt</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                <th>CreatedAt</th>
                                
                                
                                
                                <th>UpdatedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {businesses.business_badges_business && Array.isArray(businesses.business_badges_business) &&
                              businesses.business_badges_business.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/business_badges/business_badges-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="badge_type">
                                        { item.badge_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="granted_at">
                                        { dataFormatter.dateTimeFormatter(item.granted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="revoked_at">
                                        { dataFormatter.dateTimeFormatter(item.revoked_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="created_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.created_at_ts) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="updated_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.updated_at_ts) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!businesses?.business_badges_business?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Verification_submissions Business</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>BadgeType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                <th>AdminNotes</th>
                                
                                
                                
                                <th>CreatedAt</th>
                                
                                
                                
                                <th>UpdatedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {businesses.verification_submissions_business && Array.isArray(businesses.verification_submissions_business) &&
                              businesses.verification_submissions_business.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/verification_submissions/verification_submissions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="badge_type">
                                        { item.badge_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="admin_notes">
                                        { item.admin_notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="created_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.created_at_ts) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="updated_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.updated_at_ts) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!businesses?.verification_submissions_business?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Lead_matches Business</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>MatchScore</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>SentAt</th>
                                
                                
                                
                                <th>ViewedAt</th>
                                
                                
                                
                                <th>RespondedAt</th>
                                
                                
                                
                                <th>ScheduledAt</th>
                                
                                
                                
                                <th>CompletedAt</th>
                                
                                
                                
                                <th>DeclinedAt</th>
                                
                                
                                
                                <th>CreatedAt</th>
                                
                                
                                
                                <th>UpdatedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {businesses.lead_matches_business && Array.isArray(businesses.lead_matches_business) &&
                              businesses.lead_matches_business.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/lead_matches/lead_matches-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="match_score">
                                        { item.match_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sent_at">
                                        { dataFormatter.dateTimeFormatter(item.sent_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="viewed_at">
                                        { dataFormatter.dateTimeFormatter(item.viewed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="responded_at">
                                        { dataFormatter.dateTimeFormatter(item.responded_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_at">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completed_at">
                                        { dataFormatter.dateTimeFormatter(item.completed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="declined_at">
                                        { dataFormatter.dateTimeFormatter(item.declined_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="created_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.created_at_ts) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="updated_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.updated_at_ts) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!businesses?.lead_matches_business?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Reviews Business</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>Text</th>
                                
                                
                                
                                <th>IsVerifiedJob</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>ModerationNotes</th>
                                
                                
                                
                                <th>CreatedAt</th>
                                
                                
                                
                                <th>UpdatedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {businesses.reviews_business && Array.isArray(businesses.reviews_business) &&
                              businesses.reviews_business.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="text">
                                        { item.text }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_verified_job">
                                        { dataFormatter.booleanFormatter(item.is_verified_job) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="moderation_notes">
                                        { item.moderation_notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="created_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.created_at_ts) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="updated_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.updated_at_ts) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!businesses?.reviews_business?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Trust_adjustments Business</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Delta</th>
                                
                                
                                
                                <th>Reason</th>
                                
                                
                                
                                <th>CreatedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {businesses.trust_adjustments_business && Array.isArray(businesses.trust_adjustments_business) &&
                              businesses.trust_adjustments_business.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/trust_adjustments/trust_adjustments-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="delta">
                                        { item.delta }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reason">
                                        { item.reason }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="created_at_ts">
                                        { dataFormatter.dateTimeFormatter(item.created_at_ts) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!businesses?.trust_adjustments_business?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                

                <BaseDivider />

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

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

export default BusinessesView;