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/inventory_movements/inventory_movementsSlice'
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 EditInventory_movementsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'movement_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    movement_type: '',
    

    

    

    

    
    
    

    

    

    

    

    
    movement_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    item: null,
    

    
    
    
    'quantity': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    uom: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    from_location: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    to_location: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    lot: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    serial: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    work_order: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    supplier: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    customer: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    reason_code: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    performed_by: null,
    

    
    
    
    'reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    documents: [],
    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="MovementNumber"
    >
        <Field
            name="movement_number"
            placeholder="MovementNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="MovementType" labelFor="movement_type">
        <Field name="movement_type" id="movement_type" component="select">
          
            <option value="receipt">receipt</option>
          
            <option value="issue">issue</option>
          
            <option value="transfer">transfer</option>
          
            <option value="adjustment">adjustment</option>
          
            <option value="production_output">production_output</option>
          
            <option value="scrap">scrap</option>
          
            <option value="customer_shipment">customer_shipment</option>
          
            <option value="customer_return">customer_return</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Item' labelFor='item'>
        <Field
            name='item'
            id='item'
            component={SelectField}
            options={initialValues.item}
            itemRef={'items'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'item_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="Quantity"
    >
        <Field
            type="number"
            name="quantity"
            placeholder="Quantity"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='UoM' labelFor='uom'>
        <Field
            name='uom'
            id='uom'
            component={SelectField}
            options={initialValues.uom}
            itemRef={'uoms'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'uom_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='FromLocation' labelFor='from_location'>
        <Field
            name='from_location'
            id='from_location'
            component={SelectField}
            options={initialValues.from_location}
            itemRef={'locations'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'location_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ToLocation' labelFor='to_location'>
        <Field
            name='to_location'
            id='to_location'
            component={SelectField}
            options={initialValues.to_location}
            itemRef={'locations'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'location_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Lot' labelFor='lot'>
        <Field
            name='lot'
            id='lot'
            component={SelectField}
            options={initialValues.lot}
            itemRef={'lots'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'lot_number'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Serial' labelFor='serial'>
        <Field
            name='serial'
            id='serial'
            component={SelectField}
            options={initialValues.serial}
            itemRef={'serials'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'serial_number'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='WorkOrder' labelFor='work_order'>
        <Field
            name='work_order'
            id='work_order'
            component={SelectField}
            options={initialValues.work_order}
            itemRef={'work_orders'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'work_order_number'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Supplier' labelFor='supplier'>
        <Field
            name='supplier'
            id='supplier'
            component={SelectField}
            options={initialValues.supplier}
            itemRef={'suppliers'}
      
        
      
        
      
        
      
        
            showField={'supplier_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Customer' labelFor='customer'>
        <Field
            name='customer'
            id='customer'
            component={SelectField}
            options={initialValues.customer}
            itemRef={'customers'}
      
        
      
        
      
        
      
        
      
        
            showField={'customer_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ReasonCode' labelFor='reason_code'>
        <Field
            name='reason_code'
            id='reason_code'
            component={SelectField}
            options={initialValues.reason_code}
            itemRef={'reason_codes'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'reason_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PerformedBy' labelFor='performed_by'>
        <Field
            name='performed_by'
            id='performed_by'
            component={SelectField}
            options={initialValues.performed_by}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Reference"
    >
        <Field
            name="reference"
            placeholder="Reference"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Notes" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Notes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='Documents'
            color='info'
            icon={mdiUpload}
            path={'inventory_movements/documents'}
            name='documents'
            id='documents'
            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('/inventory_movements/inventory_movements-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditInventory_movementsPage
