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/routing_operations/routing_operationsSlice'
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 EditRouting_operationsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    routing: null,
    

    
    
    

    

    

    
    operation_sequence: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'operation_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    work_center: null,
    

    
    
    
    'setup_time_minutes': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'run_time_minutes_per_unit': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'queue_time_minutes': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'move_time_minutes': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    instructions: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof routing_operations === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (routing_operations)[el])
          setInitialValues(newInitialVal);
      }
  }, [routing_operations])

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Routing' labelFor='routing'>
        <Field
            name='routing'
            id='routing'
            component={SelectField}
            options={initialValues.routing}
            itemRef={'routings'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'routing_code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="OperationSequence"
    >
        <Field
            type="number"
            name="operation_sequence"
            placeholder="OperationSequence"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="OperationName"
    >
        <Field
            name="operation_name"
            placeholder="OperationName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='WorkCenter' labelFor='work_center'>
        <Field
            name='work_center'
            id='work_center'
            component={SelectField}
            options={initialValues.work_center}
            itemRef={'work_centers'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="SetupTimeMinutes"
    >
        <Field
            type="number"
            name="setup_time_minutes"
            placeholder="SetupTimeMinutes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RunTimeMinutesPerUnit"
    >
        <Field
            type="number"
            name="run_time_minutes_per_unit"
            placeholder="RunTimeMinutesPerUnit"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="QueueTimeMinutes"
    >
        <Field
            type="number"
            name="queue_time_minutes"
            placeholder="QueueTimeMinutes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MoveTimeMinutes"
    >
        <Field
            type="number"
            name="move_time_minutes"
            placeholder="MoveTimeMinutes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditRouting_operationsPage
