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/cms_pages/cms_pagesSlice'
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 Cms_pagesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { cms_pages } = useAppSelector((state) => state.cms_pages)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PageTitle</p>
                    <p>{cms_pages?.page_title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PageType</p>
                    <p>{cms_pages?.page_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Published'>
                    <SwitchField
                      field={{name: 'published', value: cms_pages?.published}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Seo_entries Page</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>TargetType</th>
                                
                                
                                
                                <th>TargetSlug</th>
                                
                                
                                
                                <th>MetaTitle</th>
                                
                                
                                
                                <th>MetaDescription</th>
                                
                                
                                
                                <th>CanonicalURL</th>
                                
                                
                                
                                <th>Indexable</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {cms_pages.seo_entries_page && Array.isArray(cms_pages.seo_entries_page) &&
                              cms_pages.seo_entries_page.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/seo_entries/seo_entries-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="target_type">
                                        { item.target_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_slug">
                                        { item.target_slug }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="meta_title">
                                        { item.meta_title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="meta_description">
                                        { item.meta_description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="canonical_url">
                                        { item.canonical_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="indexable">
                                        { dataFormatter.booleanFormatter(item.indexable) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!cms_pages?.seo_entries_page?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Cms_pagesView;