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/jobs/jobsSlice'
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 JobsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { jobs } = useAppSelector((state) => state.jobs)

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

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

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

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

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

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

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

                </div>

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

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

                </div>

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

                        <p>{jobs?.campaign?.campaign_name ?? 'No data'}</p>

                </div>

                <FormField label='Scheduled Completion Date'>
                    {jobs.scheduled_completion_date ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={jobs.scheduled_completion_date ?
                        new Date(
                          dayjs(jobs.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'}>Job Type</p>
                    <p>{jobs?.job_type}</p>
                </div>

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

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

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

                <BaseDivider />

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

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

export default JobsView;
