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

const initialValues = {
    
    
    audit_number: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    audit_type: 'internal',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    status: 'planned',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    lead_auditor: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    supplier: '',
    
    
    
    
    
    
    
    
    
    start_at: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    end_at: '',
    
    
    
    
    
    
    
    
    
    
    
    scope: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    summary: '',
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    audit_files: [],
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    mapped_clauses: [],
    
    
}


const AuditsNew = () => {
  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('/audits/audits-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="AuditNumber"
  >
      <Field
          name="audit_number"
          placeholder="AuditNumber"
      />
  </FormField>









































  <FormField label="AuditType" labelFor="audit_type">
      <Field name="audit_type" id="audit_type" component="select">
      
        <option value="internal">internal</option>
      
        <option value="supplier">supplier</option>
      
        <option value="notified_body">notified_body</option>
      
        <option value="regulatory">regulatory</option>
      
        <option value="certification">certification</option>
      
        <option value="other">other</option>
      
      </Field>
  </FormField>



























  <FormField label="Status" labelFor="status">
      <Field name="status" id="status" component="select">
      
        <option value="planned">planned</option>
      
        <option value="in_progress">in_progress</option>
      
        <option value="report_issued">report_issued</option>
      
        <option value="closed">closed</option>
      
        <option value="cancelled">cancelled</option>
      
      </Field>
  </FormField>































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



























  <FormField label="Supplier" labelFor="supplier">
      <Field name="supplier" id="supplier" component={SelectField} options={[]} itemRef={'suppliers'}></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="Scope" hasTextareaHeight>
        <Field name="scope" as="textarea" placeholder="Scope" />
    </FormField>



























    <FormField label="Summary" hasTextareaHeight>
        <Field name="summary" as="textarea" placeholder="Summary" />
    </FormField>
















































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
























    <FormField label='MappedClauses' labelFor='mapped_clauses'>
        <Field
            name='mapped_clauses'
            id='mapped_clauses'
            itemRef={'clauses'}
            options={[]}
            component={SelectFieldMany}>
        </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('/audits/audits-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default AuditsNew
