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/users/usersSlice';
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 UsersView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { users } = useAppSelector((state) => state.users);

  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 users')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title={removeLastCharacter('View users')}
          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}</p>
          </div>

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

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

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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Avatar</p>
            {users?.avatar?.length ? (
              <ImageField
                name={'avatar'}
                image={users?.avatar}
                className='w-20 h-20'
              />
            ) : (
              <p>No Avatar</p>
            )}
          </div>

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

            <p>{users?.app_role?.name ?? 'No data'}</p>
          </div>

          <>
            <p className={'block font-bold mb-2'}>Custom Permissions</p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr>
                      <th>Name</th>
                    </tr>
                  </thead>
                  <tbody>
                    {users.custom_permissions &&
                      Array.isArray(users.custom_permissions) &&
                      users.custom_permissions.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/permissions/permissions-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='name'>{item.name}</td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!users?.custom_permissions?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Client Id</p>
            <p>{users?.clientId}</p>
          </div>

          <>
            <p className={'block font-bold mb-2'}>
              Brands Default Account Manager
            </p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr>
                      <th>Name</th>

                      <th>Rate Card Import</th>

                      <th>Client Fixed Text</th>

                      <th>Yes Fixed Text</th>

                      <th>Default Client Contact</th>

                      <th>Inherit Default Rate Card</th>

                      <th>Default Costs</th>

                      <th>Production Fee</th>

                      <th>Insurance Fee</th>

                      <th>Temp</th>

                      <th>Xero Id</th>
                    </tr>
                  </thead>
                  <tbody>
                    {users.brands_default_account_manager &&
                      Array.isArray(users.brands_default_account_manager) &&
                      users.brands_default_account_manager.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/brands/brands-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='name'>{item.name}</td>

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

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

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

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

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

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

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

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

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

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

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

                      <th>Rate Card Import</th>

                      <th>Client Fixed Text</th>

                      <th>Yes Fixed Text</th>

                      <th>Default Client Contact</th>

                      <th>Inherit Default Rate Card</th>

                      <th>Default Costs</th>

                      <th>Production Fee</th>

                      <th>Insurance Fee</th>

                      <th>Temp</th>

                      <th>Xero Id</th>
                    </tr>
                  </thead>
                  <tbody>
                    {users.brands_default_sales &&
                      Array.isArray(users.brands_default_sales) &&
                      users.brands_default_sales.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/brands/brands-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='name'>{item.name}</td>

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

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

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

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

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

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

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

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

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

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

          <>
            <p className={'block font-bold mb-2'}>Clients Account Manager</p>
            <CardBox
              className='mb-6 border border-gray-300 rounded overflow-hidden'
              hasTable
            >
              <div className='overflow-x-auto'>
                <table>
                  <thead>
                    <tr>
                      <th>Thyme Id</th>

                      <th>Name</th>

                      <th>Office</th>

                      <th>Address</th>

                      <th>City</th>

                      <th>Post Code</th>

                      <th>Country Txt</th>

                      <th>Phone Number</th>

                      <th>Website</th>

                      <th>Currency</th>

                      <th>Curious Customer Id</th>

                      <th>Agency</th>

                      <th>Inclusive Amends</th>

                      <th>Early Payment Discount</th>

                      <th>Client Id Link</th>

                      <th>Public Url</th>

                      <th>Production Fee</th>

                      <th>Insurance Fee</th>

                      <th>Xero Id</th>
                    </tr>
                  </thead>
                  <tbody>
                    {users.clients_account_manager &&
                      Array.isArray(users.clients_account_manager) &&
                      users.clients_account_manager.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/clients/clients-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='thyme_id'>{item.thyme_id}</td>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

          <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;
