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/work_order_materials/work_order_materialsSlice'
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 EditWork_order_materials = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    work_order: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    material: null,
    

    
    
    
    'required_qty': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'issued_qty': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'scrap_qty': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { work_order_materialsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <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='Material' labelFor='material'>
        <Field
            name='material'
            id='material'
            component={SelectField}
            options={initialValues.material}
            itemRef={'materials'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="RequiredQuantity"
    >
        <Field
            type="number"
            name="required_qty"
            placeholder="RequiredQuantity"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="IssuedQuantity"
    >
        <Field
            type="number"
            name="issued_qty"
            placeholder="IssuedQuantity"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ScrapQuantity"
    >
        <Field
            type="number"
            name="scrap_qty"
            placeholder="ScrapQuantity"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditWork_order_materials
