/**
 * New PWA Caches 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/pwa_caches/pwa_cachesSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';

const initialValues = {
  project: '',
  environment: 'stage',
  cache_version: '',
  manifest_json: '',
  asset_list_json: '',
  generated_at: '',
  is_active: false,
};

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('New PWA Cache')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title='New PWA Cache'
          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='stage'>stage</option>
                  <option value='production'>production</option>
                </Field>
              </FormField>

              <FormField label='Cache Version'>
                <Field name='cache_version' placeholder='Cache Version' />
              </FormField>

              <FormField label='Manifest JSON' hasTextareaHeight>
                <Field
                  name='manifest_json'
                  as='textarea'
                  placeholder='Manifest JSON'
                />
              </FormField>

              <FormField label='Asset List JSON' hasTextareaHeight>
                <Field
                  name='asset_list_json'
                  as='textarea'
                  placeholder='Asset List JSON'
                />
              </FormField>

              <FormField label='Generated At'>
                <Field
                  type='datetime-local'
                  name='generated_at'
                  placeholder='Generated At'
                />
              </FormField>

              <FormField label='Is Active' labelFor='is_active'>
                <Field
                  name='is_active'
                  id='is_active'
                  component={SwitchField}
                />
              </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('/pwa_caches/pwa_caches-list')}
                />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  );
};

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

export default Pwa_cachesNew;
