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/campaigns/campaignsSlice'
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 CampaignsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { campaigns } = useAppSelector((state) => state.campaigns)

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

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{campaigns?.status}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Campaign Number</p>
                    <p>{campaigns?.campaign_number}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Thyme Campaign Number</p>
                    <p>{campaigns?.thyme_campaign_number}</p>
                </div>

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Client</p>

                        <p>{campaigns?.client?.name ?? 'No data'}</p>

                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Brand</p>

                        <p>{campaigns?.brand?.name ?? 'No data'}</p>

                </div>

                <FormField label='Scheduled Completion Date'>
                    {campaigns.scheduled_completion_date ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={campaigns.scheduled_completion_date ?
                        new Date(
                          dayjs(campaigns.scheduled_completion_date).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No Scheduled Completion Date</p>}
                </FormField>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Account Manager</p>

                        <p>{campaigns?.account_manager?.firstName ?? 'No data'}</p>

                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Sales</p>

                        <p>{campaigns?.sales?.firstName ?? 'No data'}</p>

                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Billing Status</p>
                    <p>{campaigns?.billing_status}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Departments</p>

                        <p>{campaigns?.departments?.name ?? 'No data'}</p>

                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Created By</p>

                        <p>{campaigns?.created_by?.firstName ?? 'No data'}</p>

                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Total Cost</p>
                  <p>{campaigns?.total_cost || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Total Invoiced</p>
                  <p>{campaigns?.total_invoiced || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Uninvoiced</p>
                  <p>{campaigns?.uninvoiced || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Remaining Invoice %</p>
                  <p>{campaigns?.remaining_invoice_decimal || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TimingProjectID</p>
                    <p>{campaigns?.timing_project_id}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TimingApp_DepartmentID</p>
                    <p>{campaigns?.timing_app_department_id}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TimingID</p>
                    <p>{campaigns?.timing_id}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Client Details</p>
                    <p>{campaigns?.client_details}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Sub Brand</p>
                    <p>{campaigns?.sub_brand}</p>
                </div>

                <FormField label='Start Date'>
                    {campaigns.start_date ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={campaigns.start_date ?
                        new Date(
                          dayjs(campaigns.start_date).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No Start Date</p>}
                </FormField>

                <FormField label='Admin Override'>
                    <SwitchField
                      field={{name: 'admin_override', value: campaigns?.admin_override}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>

                <FormField label='Current Date'>
                    {campaigns.current_date ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={campaigns.current_date ?
                        new Date(
                          dayjs(campaigns.current_date).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No Current Date</p>}
                </FormField>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Contact Info</p>
                    <p>{campaigns?.contact_info}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Client Contact</p>
                    <p>{campaigns?.client_contact}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Country</p>
                    <p>{campaigns?.country}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Language</p>
                    <p>{campaigns?.language}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Client Ref</p>
                    <p>{campaigns?.client_ref}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Client Job Number</p>
                    <p>{campaigns?.client_job_number}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Invoice Number</p>
                    <p>{campaigns?.invoice_number}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Revenue Code</p>
                    <p>{campaigns?.revenue_code}</p>
                </div>

                <FormField label='Created Date'>
                    {campaigns.created_date ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={campaigns.created_date ?
                        new Date(
                          dayjs(campaigns.created_date).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No Created Date</p>}
                </FormField>

                <FormField label='Last Updated Date'>
                    {campaigns.last_updated_date ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={campaigns.last_updated_date ?
                        new Date(
                          dayjs(campaigns.last_updated_date).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No Last Updated Date</p>}
                </FormField>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TimingApp</p>
                    <p>{campaigns?.timing_app}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Organizations</p>

                        <p>{campaigns?.organizations?.name ?? 'No data'}</p>

                </div>

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

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

                <BaseDivider />

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

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

export default CampaignsView;
