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/brands/brandsSlice'
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 BrandsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { brands } = useAppSelector((state) => state.brands)

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

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

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

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

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

                <BaseDivider />

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

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

export default BrandsView;
