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/kooperatif/kooperatifSlice';
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';

import { hasPermission } from '../../helpers/userPermissions';

const KooperatifView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { kooperatif } = useAppSelector((state) => state.kooperatif);

  const { currentUser } = useAppSelector((state) => state.auth);

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

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

                      <th>Last Name</th>

                      <th>Phone Number</th>

                      <th>E-Mail</th>

                      <th>Disabled</th>
                    </tr>
                  </thead>
                  <tbody>
                    {kooperatif.users_kooperatif &&
                      Array.isArray(kooperatif.users_kooperatif) &&
                      kooperatif.users_kooperatif.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/users/users-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='firstName'>{item.firstName}</td>

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

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

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

                          <td data-label='disabled'>
                            {dataFormatter.booleanFormatter(item.disabled)}
                          </td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!kooperatif?.users_kooperatif?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

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

                      <th>DebtLimit</th>

                      <th>CurrentDebt</th>

                      <th>AccountType</th>
                    </tr>
                  </thead>
                  <tbody>
                    {kooperatif.accounts_kooperatif &&
                      Array.isArray(kooperatif.accounts_kooperatif) &&
                      kooperatif.accounts_kooperatif.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/accounts/accounts-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='balance'>{item.balance}</td>

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

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

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

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

                      <th>PublishedDate</th>
                    </tr>
                  </thead>
                  <tbody>
                    {kooperatif.announcements_kooperatif &&
                      Array.isArray(kooperatif.announcements_kooperatif) &&
                      kooperatif.announcements_kooperatif.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/announcements/announcements-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='title'>{item.title}</td>

                          <td data-label='published_date'>
                            {dataFormatter.dateTimeFormatter(
                              item.published_date,
                            )}
                          </td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!kooperatif?.announcements_kooperatif?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

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

                      <th>OfferDate</th>
                    </tr>
                  </thead>
                  <tbody>
                    {kooperatif.barter_offers_kooperatif &&
                      Array.isArray(kooperatif.barter_offers_kooperatif) &&
                      kooperatif.barter_offers_kooperatif.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/barter_offers/barter_offers-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='offer_details'>
                            {item.offer_details}
                          </td>

                          <td data-label='offer_date'>
                            {dataFormatter.dateTimeFormatter(item.offer_date)}
                          </td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!kooperatif?.barter_offers_kooperatif?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

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

                      <th>DueDate</th>

                      <th>Notified</th>
                    </tr>
                  </thead>
                  <tbody>
                    {kooperatif.delayed_payments_kooperatif &&
                      Array.isArray(kooperatif.delayed_payments_kooperatif) &&
                      kooperatif.delayed_payments_kooperatif.map(
                        (item: any) => (
                          <tr
                            key={item.id}
                            onClick={() =>
                              router.push(
                                `/delayed_payments/delayed_payments-view/?id=${item.id}`,
                              )
                            }
                          >
                            <td data-label='amount'>{item.amount}</td>

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

                            <td data-label='notified'>
                              {dataFormatter.booleanFormatter(item.notified)}
                            </td>
                          </tr>
                        ),
                      )}
                  </tbody>
                </table>
              </div>
              {!kooperatif?.delayed_payments_kooperatif?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

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

                      <th>ResourceType</th>
                    </tr>
                  </thead>
                  <tbody>
                    {kooperatif.islamic_finance_library_kooperatif &&
                      Array.isArray(
                        kooperatif.islamic_finance_library_kooperatif,
                      ) &&
                      kooperatif.islamic_finance_library_kooperatif.map(
                        (item: any) => (
                          <tr
                            key={item.id}
                            onClick={() =>
                              router.push(
                                `/islamic_finance_library/islamic_finance_library-view/?id=${item.id}`,
                              )
                            }
                          >
                            <td data-label='title'>{item.title}</td>

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

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

                      <th>LoanType</th>

                      <th>ApplicationDate</th>

                      <th>Approved</th>
                    </tr>
                  </thead>
                  <tbody>
                    {kooperatif.loan_applications_kooperatif &&
                      Array.isArray(kooperatif.loan_applications_kooperatif) &&
                      kooperatif.loan_applications_kooperatif.map(
                        (item: any) => (
                          <tr
                            key={item.id}
                            onClick={() =>
                              router.push(
                                `/loan_applications/loan_applications-view/?id=${item.id}`,
                              )
                            }
                          >
                            <td data-label='amount'>{item.amount}</td>

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

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

                            <td data-label='approved'>
                              {dataFormatter.booleanFormatter(item.approved)}
                            </td>
                          </tr>
                        ),
                      )}
                  </tbody>
                </table>
              </div>
              {!kooperatif?.loan_applications_kooperatif?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <>
            <p className={'block font-bold mb-2'}>Partners kooperatif</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>TCNumber</th>

                      <th>TaxNumber</th>

                      <th>InsuranceNumber</th>

                      <th>Phone</th>

                      <th>Sector</th>

                      <th>RegistrationDate</th>
                    </tr>
                  </thead>
                  <tbody>
                    {kooperatif.partners_kooperatif &&
                      Array.isArray(kooperatif.partners_kooperatif) &&
                      kooperatif.partners_kooperatif.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/partners/partners-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='name'>{item.name}</td>

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

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

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

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

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

                          <td data-label='registration_date'>
                            {dataFormatter.dateTimeFormatter(
                              item.registration_date,
                            )}
                          </td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!kooperatif?.partners_kooperatif?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

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

                      <th>StartDate</th>

                      <th>EndDate</th>
                    </tr>
                  </thead>
                  <tbody>
                    {kooperatif.projects_kooperatif &&
                      Array.isArray(kooperatif.projects_kooperatif) &&
                      kooperatif.projects_kooperatif.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/projects/projects-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='title'>{item.title}</td>

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

                          <td data-label='end_date'>
                            {dataFormatter.dateTimeFormatter(item.end_date)}
                          </td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!kooperatif?.projects_kooperatif?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

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

                      <th>TransactionDate</th>

                      <th>TransactionType</th>
                    </tr>
                  </thead>
                  <tbody>
                    {kooperatif.transactions_kooperatif &&
                      Array.isArray(kooperatif.transactions_kooperatif) &&
                      kooperatif.transactions_kooperatif.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/transactions/transactions-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='amount'>{item.amount}</td>

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

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

          <BaseDivider />

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

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

export default KooperatifView;
