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/adbuilder_templates/adbuilder_templatesSlice'
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 Adbuilder_templatesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { adbuilder_templates } = useAppSelector((state) => state.adbuilder_templates)

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

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

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

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Last Modified By</p>
                    <p>{adbuilder_templates?.last_modified_by}</p>
                </div>

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

                <BaseDivider />

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

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

export default Adbuilder_templatesView;
