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

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

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

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

                      <th>Description</th>

                      <th>Status</th>

                      <th>Attachments</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.claims_agences &&
                      Array.isArray(agences.claims_agences) &&
                      agences.claims_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/claims/claims-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='category'>{item.category}</td>

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

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

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

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

                      <th>LastName</th>

                      <th>BirthDate</th>

                      <th>Phone</th>

                      <th>Address</th>

                      <th>IDType</th>

                      <th>IDNumber</th>

                      <th>IDIssueDate</th>

                      <th>LicenseNumber</th>

                      <th>LicenseIssueDate</th>

                      <th>IDCopyURL</th>

                      <th>LicenseCopyURL</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.clients_agences &&
                      Array.isArray(agences.clients_agences) &&
                      agences.clients_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/clients/clients-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='first_name'>{item.first_name}</td>

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

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

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

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

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

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

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

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

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

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

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

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

                      <th>Address</th>

                      <th>Phone</th>

                      <th>Email</th>

                      <th>Website</th>

                      <th>RC</th>

                      <th>ICE</th>

                      <th>Patente</th>

                      <th>LogoURL</th>

                      <th>PDFFooter</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.company_profiles_agences &&
                      Array.isArray(agences.company_profiles_agences) &&
                      agences.company_profiles_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/company_profiles/company_profiles-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='legal_name'>{item.legal_name}</td>

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

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

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

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

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

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

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

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

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

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

                      <th>LicenseNumber</th>

                      <th>LicenseIssueDate</th>

                      <th>IDCopyURL</th>

                      <th>LicenseCopyURL</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.drivers_agences &&
                      Array.isArray(agences.drivers_agences) &&
                      agences.drivers_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/drivers/drivers-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='full_name'>{item.full_name}</td>

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

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

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

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

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

                      <th>Category</th>

                      <th>Amount</th>

                      <th>Date</th>

                      <th>Note</th>

                      <th>AttachmentURL</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.expenses_agences &&
                      Array.isArray(agences.expenses_agences) &&
                      agences.expenses_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/expenses/expenses-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='type'>{item.type}</td>

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

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

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

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

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

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

                      <th>Amount</th>

                      <th>Status</th>

                      <th>ProofURL</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.guarantees_agences &&
                      Array.isArray(agences.guarantees_agences) &&
                      agences.guarantees_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/guarantees/guarantees-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='method'>{item.method}</td>

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

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

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

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

                      <th>Date</th>

                      <th>Location</th>

                      <th>Amount</th>

                      <th>Status</th>

                      <th>ProofURL</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.infractions_agences &&
                      Array.isArray(agences.infractions_agences) &&
                      agences.infractions_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/infractions/infractions-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='ref_no'>{item.ref_no}</td>

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

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

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

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

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

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

                      <th>AmountMAD</th>

                      <th>Status</th>

                      <th>IssuedAt</th>

                      <th>PDFURL</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.invoices_saas_agences &&
                      Array.isArray(agences.invoices_saas_agences) &&
                      agences.invoices_saas_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/invoices_saas/invoices_saas-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='invoice_number'>
                            {item.invoice_number}
                          </td>

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

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

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

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

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

                      <th>ScheduledAt</th>

                      <th>Odometer</th>

                      <th>Cost</th>

                      <th>Notes</th>

                      <th>Attachments</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.maintenance_agences &&
                      Array.isArray(agences.maintenance_agences) &&
                      agences.maintenance_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/maintenance/maintenance-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='type'>{item.type}</td>

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

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

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

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

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

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

                      <th>Amount</th>

                      <th>PaidAt</th>

                      <th>ReceiptReference</th>

                      <th>AttachmentURL</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.payments_agences &&
                      Array.isArray(agences.payments_agences) &&
                      agences.payments_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/payments/payments-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='method'>{item.method}</td>

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

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

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

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

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

                      <th>Name</th>

                      <th>Period</th>

                      <th>PriceMAD</th>

                      <th>Limits</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.plans_agences &&
                      Array.isArray(agences.plans_agences) &&
                      agences.plans_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/plans/plans-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='code'>{item.code}</td>

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

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

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

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

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

                      <th>StatusManual</th>

                      <th>StatusComputed</th>

                      <th>StartAt</th>

                      <th>EndAt</th>

                      <th>PickupAddress</th>

                      <th>ReturnAddress</th>

                      <th>Options</th>

                      <th>Days</th>

                      <th>PricePerDay</th>

                      <th>TotalPrice</th>

                      <th>PaidAmount</th>

                      <th>BalanceAmount</th>

                      <th>GuaranteeMethod</th>

                      <th>GuaranteeAmount</th>

                      <th>GuaranteeStatus</th>

                      <th>DepartureValidatedAt</th>

                      <th>ReturnValidatedAt</th>

                      <th>Notes</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.reservations_agences &&
                      Array.isArray(agences.reservations_agences) &&
                      agences.reservations_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/reservations/reservations-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='ref'>{item.ref}</td>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                      <th>CurrentPeriodStart</th>

                      <th>CurrentPeriodEnd</th>

                      <th>PaymentProvider</th>

                      <th>ProviderCustomerID</th>

                      <th>ProviderSubscriptionID</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.subscriptions_agences &&
                      Array.isArray(agences.subscriptions_agences) &&
                      agences.subscriptions_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/subscriptions/subscriptions-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='status'>{item.status}</td>

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

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

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

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

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

          <>
            <p className={'block font-bold mb-2'}>Tenants agences</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>Subdomain</th>

                      <th>Status</th>

                      <th>TrialEndsAt</th>

                      <th>BillingEmail</th>

                      <th>BillingPhone</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.tenants_agences &&
                      Array.isArray(agences.tenants_agences) &&
                      agences.tenants_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/tenants/tenants-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='name'>{item.name}</td>

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

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

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

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

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

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

                      <th>After</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.vehicle_conditions_agences &&
                      Array.isArray(agences.vehicle_conditions_agences) &&
                      agences.vehicle_conditions_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/vehicle_conditions/vehicle_conditions-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='before'>{item.before}</td>

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

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

                      <th>Brand</th>

                      <th>Model</th>

                      <th>Year</th>

                      <th>Color</th>

                      <th>Category</th>

                      <th>FuelType</th>

                      <th>Transmission</th>

                      <th>Mileage</th>

                      <th>DailyPrice</th>

                      <th>Status</th>

                      <th>InsuranceExpiry</th>

                      <th>TechVisitExpiry</th>

                      <th>Photos</th>
                    </tr>
                  </thead>
                  <tbody>
                    {agences.vehicles_agences &&
                      Array.isArray(agences.vehicles_agences) &&
                      agences.vehicles_agences.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/vehicles/vehicles-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='plate_number'>{item.plate_number}</td>

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

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

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

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

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

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

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

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

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

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

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

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

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

          <BaseDivider />

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

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

export default AgencesView;
