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

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

            <p>{brands?.client?.id ?? 'No data'}</p>
          </div>

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

          <FormField label='Rate Card Import'>
            <SwitchField
              field={{
                name: 'rate_card_import',
                value: brands?.rate_card_import,
              }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Default Client Contact</p>
            <p>{brands?.default_client_contact}</p>
          </div>

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

            <p>{brands?.default_account_manager?.firstName ?? 'No data'}</p>
          </div>

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

            <p>{brands?.default_sales?.firstName ?? 'No data'}</p>
          </div>

          <FormField label='Inherit Default Rate Card'>
            <SwitchField
              field={{
                name: 'inherit_default_rate_card',
                value: brands?.inherit_default_rate_card,
              }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

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

            <p>{brands?.link_rate_card?.id ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Default Costs</p>
            <p>{brands?.default_costs || 'No data'}</p>
          </div>

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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Temp</p>
            <p>{brands?.temp}</p>
          </div>

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

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

          <BaseDivider />

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

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

export default BrandsView;
