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/categories/categoriesSlice'
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 CategoriesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { categories } = useAppSelector((state) => state.categories)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Icon</p>
                    <p>{categories?.icon}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Business_categories Category</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>
                            {categories.business_categories_category && Array.isArray(categories.business_categories_category) &&
                              categories.business_categories_category.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>
                        {!categories?.business_categories_category?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Leads Category</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Keyword</th>
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                <th>Urgency</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>ContactName</th>
                                
                                
                                
                                <th>ContactPhone</th>
                                
                                
                                
                                <th>ContactEmail</th>
                                
                                
                                
                                <th>Address</th>
                                
                                
                                
                                <th>City</th>
                                
                                
                                
                                <th>State</th>
                                
                                
                                
                                <th>ZIP</th>
                                
                                
                                
                                <th>Latitude</th>
                                
                                
                                
                                <th>Longitude</th>
                                
                                
                                
                                <th>InferredTagsJSON</th>
                                
                                
                                
                                <th>TenantKey</th>
                                
                                
                                
                                <th>CreatedAt</th>
                                
                                
                                
                                <th>UpdatedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {categories.leads_category && Array.isArray(categories.leads_category) &&
                              categories.leads_category.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/leads/leads-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="keyword">
                                        { item.keyword }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="urgency">
                                        { item.urgency }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="contact_name">
                                        { item.contact_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="contact_phone">
                                        { item.contact_phone }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="contact_email">
                                        { item.contact_email }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="address">
                                        { item.address }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="city">
                                        { item.city }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="state">
                                        { item.state }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="zip">
                                        { item.zip }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lat">
                                        { item.lat }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lng">
                                        { item.lng }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="inferred_tags_json">
                                        { item.inferred_tags_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tenant_key">
                                        { item.tenant_key }
                                    </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>
                        {!categories?.leads_category?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default CategoriesView;