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/profile_pages/profile_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';

import { hasPermission } from '../../helpers/userPermissions';

const Profile_pagesView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { profile_pages } = useAppSelector((state) => state.profile_pages);

  const { currentUser } = useAppSelector((state) => state.auth);

  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 profile_pages')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title={removeLastCharacter('View profile_pages')}
          main
        >
          <BaseButton
            color='info'
            label='Edit'
            href={`/profile_pages/profile_pages-edit/?id=${id}`}
          />
        </SectionTitleLineWithButton>
        <CardBox>
          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>User</p>

            <p>{profile_pages?.user?.firstName ?? 'No data'}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Title</p>
            <p>{profile_pages?.title}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>ThemeTemplate</p>

            <p>{profile_pages?.theme_template?.name ?? 'No data'}</p>
          </div>

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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>OGTitle</p>
            <p>{profile_pages?.og_title}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>OGDescription</p>
            <p>{profile_pages?.og_description}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>OGImageURL</p>
            <p>{profile_pages?.og_image_url}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>organizations</p>

            <p>{profile_pages?.organizations?.name ?? 'No data'}</p>
          </div>

          <>
            <p className={'block font-bold mb-2'}>
              Analytics_daily ProfilePage
            </p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr>
                      <th>Date</th>

                      <th>Visits</th>

                      <th>Clicks</th>

                      <th>TopReferrers</th>
                    </tr>
                  </thead>
                  <tbody>
                    {profile_pages.analytics_daily_profile_page &&
                      Array.isArray(
                        profile_pages.analytics_daily_profile_page,
                      ) &&
                      profile_pages.analytics_daily_profile_page.map(
                        (item: any) => (
                          <tr
                            key={item.id}
                            onClick={() =>
                              router.push(
                                `/analytics_daily/analytics_daily-view/?id=${item.id}`,
                              )
                            }
                          >
                            <td data-label='date'>
                              {dataFormatter.dateTimeFormatter(item.date)}
                            </td>

                            <td data-label='visits'>{item.visits}</td>

                            <td data-label='clicks'>{item.clicks}</td>

                            <td data-label='top_referrers'>
                              {item.top_referrers}
                            </td>
                          </tr>
                        ),
                      )}
                  </tbody>
                </table>
              </div>
              {!profile_pages?.analytics_daily_profile_page?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <>
            <p className={'block font-bold mb-2'}>
              Analytics_events ProfilePage
            </p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr>
                      <th>EventType</th>

                      <th>Referrer</th>

                      <th>UserAgent</th>

                      <th>IPHash</th>

                      <th>OccurredAt</th>
                    </tr>
                  </thead>
                  <tbody>
                    {profile_pages.analytics_events_profile_page &&
                      Array.isArray(
                        profile_pages.analytics_events_profile_page,
                      ) &&
                      profile_pages.analytics_events_profile_page.map(
                        (item: any) => (
                          <tr
                            key={item.id}
                            onClick={() =>
                              router.push(
                                `/analytics_events/analytics_events-view/?id=${item.id}`,
                              )
                            }
                          >
                            <td data-label='event_type'>{item.event_type}</td>

                            <td data-label='referrer'>{item.referrer}</td>

                            <td data-label='user_agent'>{item.user_agent}</td>

                            <td data-label='ip_hash'>{item.ip_hash}</td>

                            <td data-label='occurred_at'>
                              {dataFormatter.dateTimeFormatter(
                                item.occurred_at,
                              )}
                            </td>
                          </tr>
                        ),
                      )}
                  </tbody>
                </table>
              </div>
              {!profile_pages?.analytics_events_profile_page?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <>
            <p className={'block font-bold mb-2'}>Blocks ProfilePage</p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr>
                      <th>Type</th>

                      <th>OrderIndex</th>

                      <th>Visible</th>

                      <th>Config</th>
                    </tr>
                  </thead>
                  <tbody>
                    {profile_pages.blocks_profile_page &&
                      Array.isArray(profile_pages.blocks_profile_page) &&
                      profile_pages.blocks_profile_page.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/blocks/blocks-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='type'>{item.type}</td>

                          <td data-label='order_index'>{item.order_index}</td>

                          <td data-label='visible'>
                            {dataFormatter.booleanFormatter(item.visible)}
                          </td>

                          <td data-label='config'>{item.config}</td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!profile_pages?.blocks_profile_page?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <>
            <p className={'block font-bold mb-2'}>
              Domain_mappings ProfilePage
            </p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr>
                      <th>Domain</th>

                      <th>Status</th>

                      <th>VerificationToken</th>

                      <th>SSLStatus</th>
                    </tr>
                  </thead>
                  <tbody>
                    {profile_pages.domain_mappings_profile_page &&
                      Array.isArray(
                        profile_pages.domain_mappings_profile_page,
                      ) &&
                      profile_pages.domain_mappings_profile_page.map(
                        (item: any) => (
                          <tr
                            key={item.id}
                            onClick={() =>
                              router.push(
                                `/domain_mappings/domain_mappings-view/?id=${item.id}`,
                              )
                            }
                          >
                            <td data-label='domain'>{item.domain}</td>

                            <td data-label='status'>{item.status}</td>

                            <td data-label='verification_token'>
                              {item.verification_token}
                            </td>

                            <td data-label='ssl_status'>{item.ssl_status}</td>
                          </tr>
                        ),
                      )}
                  </tbody>
                </table>
              </div>
              {!profile_pages?.domain_mappings_profile_page?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <>
            <p className={'block font-bold mb-2'}>Flags ProfilePage</p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr>
                      <th>Reason</th>

                      <th>Status</th>

                      <th>Notes</th>
                    </tr>
                  </thead>
                  <tbody>
                    {profile_pages.flags_profile_page &&
                      Array.isArray(profile_pages.flags_profile_page) &&
                      profile_pages.flags_profile_page.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/flags/flags-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='reason'>{item.reason}</td>

                          <td data-label='status'>{item.status}</td>

                          <td data-label='notes'>{item.notes}</td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!profile_pages?.flags_profile_page?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <BaseDivider />

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

Profile_pagesView.getLayout = function getLayout(page: ReactElement) {
  return (
    <LayoutAuthenticated permission={'READ_PROFILE_PAGES'}>
      {page}
    </LayoutAuthenticated>
  );
};

export default Profile_pagesView;
