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/departments/departmentsSlice'
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 DepartmentsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { departments } = useAppSelector((state) => state.departments)

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

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

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

                <BaseDivider />

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

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

export default DepartmentsView;
