/**
 * New Project Audio Tracks 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_audio_tracks/project_audio_tracksSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';
import type { ProjectAudioTrack } from '../../types/entities';

const initialValues = {
  project: '',
  environment: 'dev',
  source_key: '',
  name: '',
  slug: '',
  url: '',
  loop: false,
  volume: '',
  sort_order: '',
  is_enabled: false,
};

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

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

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

              <FormField label='Source Key'>
                <Field name='source_key' placeholder='Source Key' />
              </FormField>

              <FormField label='Name'>
                <Field name='name' placeholder='Name' />
              </FormField>

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

              <FormField label='URL'>
                <Field name='url' placeholder='URL' />
              </FormField>

              <FormField label='Loop' labelFor='loop'>
                <Field name='loop' id='loop' component={SwitchField} />
              </FormField>

              <FormField label='Volume'>
                <Field type='number' name='volume' placeholder='Volume' />
              </FormField>

              <FormField label='Sort Order'>
                <Field
                  type='number'
                  name='sort_order'
                  placeholder='Sort Order'
                />
              </FormField>

              <FormField label='Is Enabled' labelFor='is_enabled'>
                <Field
                  name='is_enabled'
                  id='is_enabled'
                  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(
                      '/project_audio_tracks/project_audio_tracks-list',
                    )
                  }
                />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  );
};

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

export default Project_audio_tracksNew;
