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/operations/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 EditOperationsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    routing: null,
    

    
    
    

    

    

    
    operation_number: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'operation_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    operation_type: '',
    

    

    

    

    
    
    
    'setup_time_minutes': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'run_time_minutes_per_unit': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'queue_time_minutes': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'move_time_minutes': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    work_center: null,
    

    
    
    

    

    
    work_instructions: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    instruction_files: [],
    

    

    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit operations')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit 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="OperationNumber"
    >
        <Field
            type="number"
            name="operation_number"
            placeholder="OperationNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="OperationType" labelFor="operation_type">
        <Field name="operation_type" id="operation_type" component="select">
          
            <option value="setup">setup</option>
          
            <option value="run">run</option>
          
            <option value="inspection">inspection</option>
          
            <option value="pack">pack</option>
          
            <option value="move">move</option>
          
        </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='WorkCenter' labelFor='work_center'>
        <Field
            name='work_center'
            id='work_center'
            component={SelectField}
            options={initialValues.work_center}
            itemRef={'work_centers'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'work_center_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  
    <FormField label='WorkInstructions' hasTextareaHeight>
        <Field
            name='work_instructions'
            id='work_instructions'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='InstructionFiles'
            color='info'
            icon={mdiUpload}
            path={'operations/instruction_files'}
            name='instruction_files'
            id='instruction_files'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></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('/operations/operations-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditOperationsPage
