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

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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Office</p>
            <p>{clients?.office}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Address</p>
            <p>{clients?.address}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>City</p>
            <p>{clients?.city}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Post Code</p>
            <p>{clients?.post_code}</p>
          </div>

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

            <p>{clients?.country?.id ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Country Txt</p>
            <p>{clients?.country_txt}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Website</p>
            <p>{clients?.website}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Currency</p>
            <p>{clients?.currency}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Curious Customer Id</p>
            <p>{clients?.curious_customer_id}</p>
          </div>

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

            <p>{clients?.account_manager?.firstName ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Agency</p>
            <p>{clients?.agency}</p>
          </div>

          <FormField label='Inclusive Amends'>
            <SwitchField
              field={{
                name: 'inclusive_amends',
                value: clients?.inclusive_amends,
              }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Early Payment Discount</p>
            <p>{clients?.early_payment_discount || 'No data'}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Public Url</p>
            <p>{clients?.public_url}</p>
          </div>

          <>
            <p className={'block font-bold mb-2'}>All Brands</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>
                    {clients.all_brands &&
                      Array.isArray(clients.all_brands) &&
                      clients.all_brands.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>
              {!clients?.all_brands?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Production Fee</p>
            <p>{clients?.production_fee || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Insurance Fee</p>
            <p>{clients?.insurance_fee || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Xero Id</p>
            <p>{clients?.xero_id}</p>
          </div>

          <>
            <p className={'block font-bold mb-2'}>Brands Client</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>
                    {clients.brands_client &&
                      Array.isArray(clients.brands_client) &&
                      clients.brands_client.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>
              {!clients?.brands_client?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

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

                      <th>Rate Card Type</th>

                      <th>Status</th>

                      <th>Currency</th>

                      <th>Push New Values</th>

                      <th>Inherit Default Rate Card</th>

                      <th>Service Count</th>

                      <th>Discipline</th>

                      <th>Output Channel</th>

                      <th>Staff Skills</th>

                      <th>Rate Card Owner</th>

                      <th>Rate Card Id</th>

                      <th>Markets</th>

                      <th>Languages</th>

                      <th>Valid From</th>

                      <th>Valid To</th>

                      <th>Version Number</th>

                      <th>Commercial Owner</th>

                      <th>Review Date</th>

                      <th>Assumptions</th>

                      <th>No Fixed Text</th>

                      <th>Yes Fixed Text</th>
                    </tr>
                  </thead>
                  <tbody>
                    {clients.brand_rate_cards_client &&
                      Array.isArray(clients.brand_rate_cards_client) &&
                      clients.brand_rate_cards_client.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/brand_rate_cards/brand_rate_cards-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='name'>{item.name}</td>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

          <BaseDivider />

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

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

export default ClientsView;
