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/invoices/invoicesSlice'
import { useAppDispatch } from '../../stores/hooks'
import { useRouter } from 'next/router'
import moment from 'moment';

const initialValues = {
    
    
    number: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    client: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    quote: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    sale: '',
    
    
    
    
    
    
    
    
    
    issue_date: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    due_date: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    status: 'draft',
    
    
    
    
    
    
    
    subtotal: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    discount_total: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    tax_total: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    total: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    amount_paid: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    amount_due: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    notes: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    pdf: [],
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    created_by_user: '',
    
    
    
}


const InvoicesNew = () => {
  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('/invoices/invoices-list')
  }
  return (
    <>
      <Head>
        <title>{getPageTitle('New Item')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title="New Item" main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            initialValues={
                
                
                
                dateRangeStart && dateRangeEnd ?
                {
                    ...initialValues,
                    issue_date: moment(dateRangeStart).format('YYYY-MM-DDTHH:mm'),
                    due_date: moment(dateRangeEnd).format('YYYY-MM-DDTHH:mm'),
                } : initialValues
                
            }
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>



  <FormField
      label="Numérofacture"
  >
      <Field
          name="number"
          placeholder="Numérofacture"
      />
  </FormField>













































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



























  <FormField label="Devissource" labelFor="quote">
      <Field name="quote" id="quote" component={SelectField} options={[]} itemRef={'quotes'}></Field>
  </FormField>



























  <FormField label="Ventesource" labelFor="sale">
      <Field name="sale" id="sale" component={SelectField} options={[]} itemRef={'sales'}></Field>
  </FormField>



















  <FormField
      label="Dated'émission"
  >
      <Field
          type="datetime-local"
          name="issue_date"
          placeholder="Dated'émission"
      />
  </FormField>



























  <FormField
      label="Dated'échéance"
  >
      <Field
          type="datetime-local"
          name="due_date"
          placeholder="Dated'échéance"
      />
  </FormField>































  <FormField label="Statut" labelFor="status">
      <Field name="status" id="status" component="select">
      
        <option value="draft">draft</option>
      
        <option value="issued">issued</option>
      
        <option value="overdue">overdue</option>
      
        <option value="paid">paid</option>
      
        <option value="cancelled">cancelled</option>
      
        <option value="refunded">refunded</option>
      
      </Field>
  </FormField>



















    <FormField
        label="Sous-total"
    >
        <Field
            type="number"
            name="subtotal"
            placeholder="Sous-total"
        />
    </FormField>



























    <FormField
        label="Remise"
    >
        <Field
            type="number"
            name="discount_total"
            placeholder="Remise"
        />
    </FormField>



























    <FormField
        label="Taxes"
    >
        <Field
            type="number"
            name="tax_total"
            placeholder="Taxes"
        />
    </FormField>



























    <FormField
        label="Total"
    >
        <Field
            type="number"
            name="total"
            placeholder="Total"
        />
    </FormField>



























    <FormField
        label="Montantpayé"
    >
        <Field
            type="number"
            name="amount_paid"
            placeholder="Montantpayé"
        />
    </FormField>



























    <FormField
        label="Resteàpayer"
    >
        <Field
            type="number"
            name="amount_due"
            placeholder="Resteàpayer"
        />
    </FormField>























    <FormField label="Notes" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Notes" />
    </FormField>
















































  <FormField>
      <Field
          label='PDF'
          color='info'
          icon={mdiUpload}
          path={'invoices/pdf'}
          name='pdf'
          id='pdf'
          schema={{
            size: undefined,
            formats: undefined,
          }}
          component={FormFilePicker}
      ></Field>
  </FormField>






















  <FormField label="Créépar" labelFor="created_by_user">
      <Field name="created_by_user" id="created_by_user" component={SelectField} options={[]} itemRef={'users'}></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('/invoices/invoices-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

InvoicesNew.getLayout = function getLayout(page: ReactElement) {
  return (
      <LayoutAuthenticated
        
          permission={'CREATE_INVOICES'}
        
      >
          {page}
      </LayoutAuthenticated>
  )
}

export default InvoicesNew
