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/brand_rate_cards/brand_rate_cardsSlice';
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 Brand_rate_cardsView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { brand_rate_cards } = useAppSelector(
    (state) => state.brand_rate_cards,
  );

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Rate Card Type</p>
            <p>{brand_rate_cards?.rate_card_type}</p>
          </div>

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

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

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

            <p>{brand_rate_cards?.brand?.id ?? 'No data'}</p>
          </div>

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

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

          <FormField label='Push New Values'>
            <SwitchField
              field={{
                name: 'push_new_values',
                value: brand_rate_cards?.push_new_values,
              }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Service Count</p>
            <p>{brand_rate_cards?.service_count || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Discipline</p>
            <p>{brand_rate_cards?.discipline}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Output Channel</p>
            <p>{brand_rate_cards?.output_channel}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Staff Skills</p>
            <p>{brand_rate_cards?.staff_skills}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Rate Card Owner</p>
            <p>{brand_rate_cards?.rate_card_owner}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Rate Card Id</p>
            <p>{brand_rate_cards?.rate_card_id}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Markets</p>
            <p>{brand_rate_cards?.markets}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Languages</p>
            <p>{brand_rate_cards?.languages}</p>
          </div>

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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Version Number</p>
            <p>{brand_rate_cards?.version_number}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Commercial Owner</p>
            <p>{brand_rate_cards?.commercial_owner}</p>
          </div>

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

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

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

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

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

          <BaseDivider />

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

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

export default Brand_rate_cardsView;
