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/csv_import_row_errors/csv_import_row_errorsSlice'
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 EditCsv_import_row_errors = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    csv_import_job: null,
    

    
    
    

    

    

    
    row_number: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    raw_row_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    error_message: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { csv_import_row_errorsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CSVImportJob' labelFor='csv_import_job'>
        <Field
            name='csv_import_job'
            id='csv_import_job'
            component={SelectField}
            options={initialValues.csv_import_job}
            itemRef={'csv_import_jobs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'original_filename'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="RowNumber"
    >
        <Field
            type="number"
            name="row_number"
            placeholder="RowNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="RawRowJson" hasTextareaHeight>
        <Field name="raw_row_json" as="textarea" placeholder="RawRowJson" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ErrorMessage" hasTextareaHeight>
        <Field name="error_message" as="textarea" placeholder="ErrorMessage" />
    </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('/csv_import_row_errors/csv_import_row_errors-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCsv_import_row_errors
