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

import { create } from '../../stores/access_logs/access_logsSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';

const initialValues = {
  project: '',
  environment: 'admin' as 'admin' | 'stage' | 'production',
  user: '',
  path: '',
  ip_address: '',
  user_agent: '',
  accessed_at: '',
};

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

  const handleSubmit = async (data: typeof initialValues) => {
    await dispatch(create(data));
    await router.push('/access_logs/access_logs-list');
  };

  return (
    <>
      <Head>
        <title>{getPageTitle('New Access Log')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title='New Access Log'
          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='Environment' labelFor='environment'>
                <Field name='environment' id='environment' component='select'>
                  <option value='admin'>admin</option>
                  <option value='stage'>stage</option>
                  <option value='production'>production</option>
                </Field>
              </FormField>

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

              <FormField label='Path'>
                <Field name='path' placeholder='Path' />
              </FormField>

              <FormField label='IP Address'>
                <Field name='ip_address' placeholder='IP Address' />
              </FormField>

              <FormField label='User Agent' hasTextareaHeight>
                <Field
                  name='user_agent'
                  as='textarea'
                  placeholder='User Agent'
                />
              </FormField>

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

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

export default Access_logsNew;
