/**
 * New Projects Page
 * Cleaned up version
 */

import { mdiChartTimelineVariant } from '@mdi/js';
import Head from 'next/head';
import React, { ReactElement } from 'react';

import CardBox from '../../components/CardBox';
import LayoutAuthenticated from '../../layouts/Authenticated';
import SectionMain from '../../components/SectionMain';
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton';
import { getPageTitle } from '../../config';

import { Field, Form, Formik } from 'formik';
import FormField from '../../components/FormField';
import BaseDivider from '../../components/BaseDivider';
import BaseButtons from '../../components/BaseButtons';
import BaseButton from '../../components/BaseButton';
import { SwitchField } from '../../components/SwitchField';

import { create } from '../../stores/projects/projectsSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';
import type { Project } from '../../types/entities';

const initialValues = {
  name: '',
  slug: '',
  description: '',
  logo_url: '',
  favicon_url: '',
  og_image_url: '',
  is_deleted: false,
  deleted_at_time: '',
};

const ProjectsNew = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();

  const handleSubmit = async (data: typeof initialValues) => {
    await dispatch(create(data as unknown as Partial<Project>));
    await router.push('/projects/projects-list');
  };

  return (
    <>
      <Head>
        <title>{getPageTitle('New Project')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title='New Project'
          main
        >
          {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>
              <FormField label='Name'>
                <Field name='name' placeholder='Name' />
              </FormField>

              <FormField label='Slug'>
                <Field name='slug' placeholder='Slug' />
              </FormField>

              <FormField label='Description' hasTextareaHeight>
                <Field
                  name='description'
                  id='description'
                  as='textarea'
                  rows={4}
                  placeholder='Project description'
                />
              </FormField>

              <FormField label='Logo URL'>
                <Field name='logo_url' placeholder='Logo URL' />
              </FormField>

              <FormField label='Favicon URL'>
                <Field name='favicon_url' placeholder='Favicon URL' />
              </FormField>

              <FormField label='OG Image URL'>
                <Field name='og_image_url' placeholder='OG Image URL' />
              </FormField>

              <FormField label='Is Deleted' labelFor='is_deleted'>
                <Field
                  name='is_deleted'
                  id='is_deleted'
                  component={SwitchField}
                />
              </FormField>

              <FormField label='Deleted At'>
                <Field
                  type='datetime-local'
                  name='deleted_at_time'
                  placeholder='Deleted At'
                />
              </FormField>

              <BaseDivider />
              <BaseButtons>
                <BaseButton type='submit' color='info' label='Submit' />
                <BaseButton type='reset' color='info' outline label='Reset' />
                <BaseButton
                  type='reset'
                  color='danger'
                  outline
                  label='Cancel'
                  onClick={() => router.push('/projects/projects-list')}
                />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  );
};

ProjectsNew.getLayout = function getLayout(page: ReactElement) {
  return (
    <LayoutAuthenticated permission='CREATE_PROJECTS'>
      {page}
    </LayoutAuthenticated>
  );
};

export default ProjectsNew;
