import {
  mdiAccount,
  mdiChartTimelineVariant,
  mdiMail,
  mdiUpload,
} 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 FormCheckRadio from '../../components/FormCheckRadio';
import FormCheckRadioGroup from '../../components/FormCheckRadioGroup';
import FormFilePicker from '../../components/FormFilePicker';
import FormImagePicker from '../../components/FormImagePicker';
import { SwitchField } from '../../components/SwitchField';

import { SelectField } from '../../components/SelectField';
import { SelectFieldMany } from '../../components/SelectFieldMany';
import { RichTextField } from '../../components/RichTextField';

import { create } from '../../stores/reservations/reservationsSlice';
import { useAppDispatch } from '../../stores/hooks';
import { useRouter } from 'next/router';
import moment from 'moment';

const initialValues = {
  tenant: '',

  ref: '',

  status_manual: 'en_attente',

  status_computed: 'a_venir',

  client: '',

  vehicle: '',

  start_at: '',

  end_at: '',

  pickup_address: '',

  return_address: '',

  options: '',

  days: '',

  price_per_day: '',

  total_price: '',

  paid_amount: '',

  balance_amount: '',

  guarantee_method: 'especes',

  guarantee_amount: '',

  guarantee_status: 'retenue',

  departure_validated_at: '',

  return_validated_at: '',

  notes: '',

  agences: '',
};

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

  // get from url params
  const { dateRangeStart, dateRangeEnd } = router.query;

  const handleSubmit = async (data) => {
    await dispatch(create(data));
    await router.push('/reservations/reservations-list');
  };
  return (
    <>
      <Head>
        <title>{getPageTitle('New Item')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title='New Item'
          main
        >
          {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            initialValues={
              dateRangeStart && dateRangeEnd
                ? {
                    ...initialValues,
                    start_at: moment(dateRangeStart).format('YYYY-MM-DDTHH:mm'),
                    end_at: moment(dateRangeEnd).format('YYYY-MM-DDTHH:mm'),
                  }
                : initialValues
            }
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>
              <FormField label='Tenant' labelFor='tenant'>
                <Field
                  name='tenant'
                  id='tenant'
                  component={SelectField}
                  options={[]}
                  itemRef={'tenants'}
                ></Field>
              </FormField>

              <FormField label='Reference'>
                <Field name='ref' placeholder='Reference' />
              </FormField>

              <FormField label='StatusManual' labelFor='status_manual'>
                <Field
                  name='status_manual'
                  id='status_manual'
                  component='select'
                >
                  <option value='en_attente'>en_attente</option>

                  <option value='confirmee'>confirmee</option>

                  <option value='annulee'>annulee</option>
                </Field>
              </FormField>

              <FormField label='StatusComputed' labelFor='status_computed'>
                <Field
                  name='status_computed'
                  id='status_computed'
                  component='select'
                >
                  <option value='a_venir'>a_venir</option>

                  <option value='en_cours'>en_cours</option>

                  <option value='a_restituer'>a_restituer</option>

                  <option value='en_retard'>en_retard</option>

                  <option value='terminee'>terminee</option>

                  <option value='annulee'>annulee</option>

                  <option value='no_show'>no_show</option>
                </Field>
              </FormField>

              <FormField label='Client' labelFor='client'>
                <Field
                  name='client'
                  id='client'
                  component={SelectField}
                  options={[]}
                  itemRef={'clients'}
                ></Field>
              </FormField>

              <FormField label='Vehicle' labelFor='vehicle'>
                <Field
                  name='vehicle'
                  id='vehicle'
                  component={SelectField}
                  options={[]}
                  itemRef={'vehicles'}
                ></Field>
              </FormField>

              <FormField label='StartAt'>
                <Field
                  type='datetime-local'
                  name='start_at'
                  placeholder='StartAt'
                />
              </FormField>

              <FormField label='EndAt'>
                <Field
                  type='datetime-local'
                  name='end_at'
                  placeholder='EndAt'
                />
              </FormField>

              <FormField label='PickupAddress'>
                <Field name='pickup_address' placeholder='PickupAddress' />
              </FormField>

              <FormField label='ReturnAddress'>
                <Field name='return_address' placeholder='ReturnAddress' />
              </FormField>

              <FormField label='Options'>
                <Field name='options' placeholder='Options' />
              </FormField>

              <FormField label='Days'>
                <Field type='number' name='days' placeholder='Days' />
              </FormField>

              <FormField label='PricePerDay'>
                <Field
                  type='number'
                  name='price_per_day'
                  placeholder='PricePerDay'
                />
              </FormField>

              <FormField label='TotalPrice'>
                <Field
                  type='number'
                  name='total_price'
                  placeholder='TotalPrice'
                />
              </FormField>

              <FormField label='PaidAmount'>
                <Field
                  type='number'
                  name='paid_amount'
                  placeholder='PaidAmount'
                />
              </FormField>

              <FormField label='BalanceAmount'>
                <Field
                  type='number'
                  name='balance_amount'
                  placeholder='BalanceAmount'
                />
              </FormField>

              <FormField label='GuaranteeMethod' labelFor='guarantee_method'>
                <Field
                  name='guarantee_method'
                  id='guarantee_method'
                  component='select'
                >
                  <option value='especes'>especes</option>

                  <option value='carte_bloquee'>carte_bloquee</option>

                  <option value='cheque'>cheque</option>

                  <option value='aucune'>aucune</option>
                </Field>
              </FormField>

              <FormField label='GuaranteeAmount'>
                <Field
                  type='number'
                  name='guarantee_amount'
                  placeholder='GuaranteeAmount'
                />
              </FormField>

              <FormField label='GuaranteeStatus' labelFor='guarantee_status'>
                <Field
                  name='guarantee_status'
                  id='guarantee_status'
                  component='select'
                >
                  <option value='retenue'>retenue</option>

                  <option value='restituee'>restituee</option>

                  <option value='partielle'>partielle</option>

                  <option value='non_applicable'>non_applicable</option>
                </Field>
              </FormField>

              <FormField label='DepartureValidatedAt'>
                <Field
                  type='datetime-local'
                  name='departure_validated_at'
                  placeholder='DepartureValidatedAt'
                />
              </FormField>

              <FormField label='ReturnValidatedAt'>
                <Field
                  type='datetime-local'
                  name='return_validated_at'
                  placeholder='ReturnValidatedAt'
                />
              </FormField>

              <FormField label='Notes'>
                <Field name='notes' placeholder='Notes' />
              </FormField>

              <FormField label='agences' labelFor='agences'>
                <Field
                  name='agences'
                  id='agences'
                  component={SelectField}
                  options={[]}
                  itemRef={'agences'}
                ></Field>
              </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('/reservations/reservations-list')}
                />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  );
};

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

export default ReservationsNew;
