/**
 * New Project Memberships 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 { SelectField } from '../../components/SelectField';

import { create } from '../../stores/project_memberships/project_membershipsSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';
import type { ProjectMembership } from '../../types/entities';

const initialValues = {
  project: '',
  user: '',
  access_level: 'owner',
  is_active: false,
  invited_at: '',
  accepted_at: '',
};

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('New Project Membership')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title='New Project Membership'
          main
        >
          {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>
              <FormField label='Project' labelFor='project'>
                <Field
                  name='project'
                  id='project'
                  component={SelectField}
                  options={[]}
                  itemRef='projects'
                />
              </FormField>

              <FormField label='User' labelFor='user'>
                <Field
                  name='user'
                  id='user'
                  component={SelectField}
                  options={[]}
                  itemRef='users'
                />
              </FormField>

              <FormField label='Access Level' labelFor='access_level'>
                <Field name='access_level' id='access_level' component='select'>
                  <option value='owner'>owner</option>
                  <option value='editor'>editor</option>
                  <option value='reviewer'>reviewer</option>
                  <option value='viewer'>viewer</option>
                </Field>
              </FormField>

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

              <FormField label='Invited At'>
                <Field
                  type='datetime-local'
                  name='invited_at'
                  placeholder='Invited At'
                />
              </FormField>

              <FormField label='Accepted At'>
                <Field
                  type='datetime-local'
                  name='accepted_at'
                  placeholder='Accepted 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('/project_memberships/project_memberships-list')
                  }
                />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  );
};

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

export default Project_membershipsNew;
