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

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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Default Prefill</p>
            <p>{countries?.default_prefill}</p>
          </div>

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

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

          <BaseDivider />

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

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

export default CountriesView;
