import React, { ReactElement, useEffect } from 'react';
import Head from 'next/head';
import { useRouter } from 'next/router';
import { mdiAccountCircle } from '@mdi/js';
import { useAppDispatch, useAppSelector } from '../../stores/hooks';
import { fetch } from '../../stores/users/usersSlice';
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 dataFormatter from '../../helpers/dataFormatter';

const UsersView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { users } = useAppSelector((state) => state.users);
  const { id } = router.query;

  useEffect(() => {
    dispatch(fetch({ id }));
  }, [dispatch, id]);

  return (
    <>
      <Head>
        <title>{getPageTitle('View user')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiAccountCircle} title='View user' main>
          <BaseButton color='info' label='Edit' href={`/users/users-edit/?id=${id}`} />
        </SectionTitleLineWithButton>
        <CardBox>
          <div className='mb-4'>
            <p className='block font-bold mb-2'>First Name</p>
            <p>{users?.firstName || 'No data'}</p>
          </div>
          <div className='mb-4'>
            <p className='block font-bold mb-2'>Last Name</p>
            <p>{users?.lastName || 'No data'}</p>
          </div>
          <div className='mb-4'>
            <p className='block font-bold mb-2'>Phone Number</p>
            <p>{users?.phoneNumber || 'No data'}</p>
          </div>
          <div className='mb-4'>
            <p className='block font-bold mb-2'>E-Mail</p>
            <p>{users?.email || 'No data'}</p>
          </div>
          <div className='mb-4'>
            <p className='block font-bold mb-2'>Disabled</p>
            <p>{dataFormatter.booleanFormatter(users?.disabled)}</p>
          </div>
          <div className='mb-4'>
            <p className='block font-bold mb-2'>Email Verified</p>
            <p>{dataFormatter.booleanFormatter(users?.emailVerified)}</p>
          </div>
          <div className='mb-4'>
            <p className='block font-bold mb-2'>Role</p>
            <p>{users?.app_role?.name || 'No data'}</p>
          </div>

          <BaseDivider />

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

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

export default UsersView;
