import { mdiChartTimelineVariant, mdiUpload } from '@mdi/js'
import Head from 'next/head'
import React, { ReactElement, useEffect, useState } from 'react'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";

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 { SelectField } from "../../components/SelectField";
import { SelectFieldMany } from "../../components/SelectFieldMany";
import { SwitchField } from '../../components/SwitchField'
import {RichTextField} from "../../components/RichTextField";

import { update, fetch } from '../../stores/qa_measurements/qa_measurementsSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";



const EditQa_measurements = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    qa_inspection: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    qa_checklist_item: null,
    

    
    
    

    

    

    

    

    

    

    

    
    result: '',
    

    

    

    

    
    
    
    'numeric_value': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    text_value: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    recorded_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    recorded_by: null,
    

    
    
    
  }
  const [initialValues, setInitialValues] = useState(initVals)

  const { qa_measurements } = useAppSelector((state) => state.qa_measurements)
  

  const { qa_measurementsId } = router.query

  useEffect(() => {
    dispatch(fetch({ id: qa_measurementsId }))
  }, [qa_measurementsId])

  useEffect(() => {
    if (typeof qa_measurements === 'object') {
      setInitialValues(qa_measurements)
    }
  }, [qa_measurements])

  useEffect(() => {
      if (typeof qa_measurements === 'object') {

          const newInitialVal = {...initVals};

          Object.keys(initVals).forEach(el => newInitialVal[el] = (qa_measurements)[el])

          setInitialValues(newInitialVal);
      }
  }, [qa_measurements])

  const handleSubmit = async (data) => {
    await dispatch(update({ id: qa_measurementsId, data }))
    await router.push('/qa_measurements/qa_measurements-list')
  }

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit qa_measurements')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit qa_measurements'} main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            enableReinitialize
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='QAInspection' labelFor='qa_inspection'>
        <Field
            name='qa_inspection'
            id='qa_inspection'
            component={SelectField}
            options={initialValues.qa_inspection}
            itemRef={'qa_inspections'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'inspection_number'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ChecklistItem' labelFor='qa_checklist_item'>
        <Field
            name='qa_checklist_item'
            id='qa_checklist_item'
            component={SelectField}
            options={initialValues.qa_checklist_item}
            itemRef={'qa_checklist_items'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'check_name'}
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Result" labelFor="result">
        <Field name="result" id="result" component="select">
          
            <option value="pass">pass</option>
          
            <option value="fail">fail</option>
          
            <option value="not_applicable">not_applicable</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="NumericValue"
    >
        <Field
            type="number"
            name="numeric_value"
            placeholder="NumericValue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="TextValue" hasTextareaHeight>
        <Field name="text_value" as="textarea" placeholder="TextValue" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="RecordedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.recorded_at ?
                  new Date(
                      dayjs(initialValues.recorded_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'recorded_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RecordedBy' labelFor='recorded_by'>
        <Field
            name='recorded_by'
            id='recorded_by'
            component={SelectField}
            options={initialValues.recorded_by}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/qa_measurements/qa_measurements-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditQa_measurements
