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/clients/clientsSlice'
import dataFormatter from '../../helpers/dataFormatter';
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 ClientsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { clients } = useAppSelector((state) => state.clients)

    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 clients')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View clients')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/clients/clients-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>

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

                <>
                    <p className={'block font-bold mb-2'}>Quotes Client</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>Status</th>

                                <th>Quote Title</th>

                                <th>Total Quoted</th>

                                <th>Estimated Profit</th>

                                <th>Created Date</th>

                                <th>Last Modified Date</th>

                            </tr>
                            </thead>
                            <tbody>
                            {clients.quotes_client && Array.isArray(clients.quotes_client) &&
                              clients.quotes_client.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quotes/quotes-view/?id=${item.id}`)}>

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

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

                                    <td data-label="quote_title">
                                        { item.quote_title }
                                    </td>

                                    <td data-label="total_quoted">
                                        { item.total_quoted }
                                    </td>

                                    <td data-label="estimated_profit">
                                        { item.estimated_profit }
                                    </td>

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

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

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!clients?.quotes_client?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Jobs Client</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>Job Number</th>

                                <th>Job Name</th>

                                <th>Status</th>

                                <th>Scheduled Completion Date</th>

                                <th>Job Type</th>

                                <th>Total Cost</th>

                                <th>Invoiced</th>

                                <th>Uninvoiced</th>

                            </tr>
                            </thead>
                            <tbody>
                            {clients.jobs_client && Array.isArray(clients.jobs_client) &&
                              clients.jobs_client.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/jobs/jobs-view/?id=${item.id}`)}>

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

                                    <td data-label="job_number">
                                        { item.job_number }
                                    </td>

                                    <td data-label="job_name">
                                        { item.job_name }
                                    </td>

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

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

                                    <td data-label="job_type">
                                        { item.job_type }
                                    </td>

                                    <td data-label="total_cost">
                                        { item.total_cost }
                                    </td>

                                    <td data-label="invoiced">
                                        { item.invoiced }
                                    </td>

                                    <td data-label="uninvoiced">
                                        { item.uninvoiced }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!clients?.jobs_client?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Client_dam Client</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>Asset ID</th>

                                <th>Description</th>

                                <th>Brand</th>

                                <th>Product</th>

                                <th>Asset Type</th>

                            </tr>
                            </thead>
                            <tbody>
                            {clients.client_dam_client && Array.isArray(clients.client_dam_client) &&
                              clients.client_dam_client.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/client_dam/client_dam-view/?id=${item.id}`)}>

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

                                    <td data-label="asset_id">
                                        { item.asset_id }
                                    </td>

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

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

                                    <td data-label="product">
                                        { item.product }
                                    </td>

                                    <td data-label="asset_type">
                                        { item.asset_type }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!clients?.client_dam_client?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Campaigns Client</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>Status</th>

                                <th>Campaign Number</th>

                                <th>Campaign Name</th>

                                <th>Scheduled Completion Date</th>

                                <th>Billing Status</th>

                                <th>Total Cost</th>

                                <th>Total Invoiced</th>

                                <th>Uninvoiced</th>

                                <th>Remaining Invoice %</th>

                                <th>Start Date</th>

                                <th>Country</th>

                                <th>Created Date</th>

                                <th>Last Updated Date</th>

                                <th>Job Number Count</th>

                                <th>Count of Left for Invoice</th>

                            </tr>
                            </thead>
                            <tbody>
                            {clients.campaigns_client && Array.isArray(clients.campaigns_client) &&
                              clients.campaigns_client.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/campaigns/campaigns-view/?id=${item.id}`)}>

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

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

                                    <td data-label="campaign_number">
                                        { item.campaign_number }
                                    </td>

                                    <td data-label="campaign_name">
                                        { item.campaign_name }
                                    </td>

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

                                    <td data-label="billing_status">
                                        { item.billing_status }
                                    </td>

                                    <td data-label="total_cost">
                                        { item.total_cost }
                                    </td>

                                    <td data-label="total_invoiced">
                                        { item.total_invoiced }
                                    </td>

                                    <td data-label="uninvoiced">
                                        { item.uninvoiced }
                                    </td>

                                    <td data-label="remaining_invoice_decimal">
                                        { item.remaining_invoice_decimal }
                                    </td>

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

                                    <td data-label="country">
                                        { item.country }
                                    </td>

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

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

                                    <td data-label="job_number_count">
                                        { item.job_number_count }
                                    </td>

                                    <td data-label="count_left_for_invoice">
                                        { item.count_left_for_invoice }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!clients?.campaigns_client?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <BaseDivider />

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

ClientsView.getLayout = function getLayout(page: ReactElement) {
    return (
      <LayoutAuthenticated>
          {page}
      </LayoutAuthenticated>
    )
}

export default ClientsView;
