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/materials/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 EditMaterialsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'material_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    material_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    uom: null,
    

    
    
    
    'standard_cost': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'standard_lead_time_days': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    lot_tracked: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    serial_tracked: false,
    

    

    

    

    

    

    
    
    
    'reorder_point': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'reorder_quantity': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    preferred_supplier: null,
    

    
    
    

    
    specification: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    spec_files: [],
    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="MaterialCode"
    >
        <Field
            name="material_code"
            placeholder="MaterialCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="MaterialName"
    >
        <Field
            name="name"
            placeholder="MaterialName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="MaterialType" labelFor="material_type">
        <Field name="material_type" id="material_type" component="select">
          
            <option value="raw">raw</option>
          
            <option value="component">component</option>
          
            <option value="packaging">packaging</option>
          
            <option value="finished_good">finished_good</option>
          
            <option value="consumable">consumable</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='UnitofMeasure' labelFor='uom'>
        <Field
            name='uom'
            id='uom'
            component={SelectField}
            options={initialValues.uom}
            itemRef={'units_of_measure'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="StandardCost"
    >
        <Field
            type="number"
            name="standard_cost"
            placeholder="StandardCost"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="StandardLeadTimeDays"
    >
        <Field
            type="number"
            name="standard_lead_time_days"
            placeholder="StandardLeadTimeDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='LotTracked' labelFor='lot_tracked'>
        <Field
            name='lot_tracked'
            id='lot_tracked'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='SerialTracked' labelFor='serial_tracked'>
        <Field
            name='serial_tracked'
            id='serial_tracked'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="ReorderPoint"
    >
        <Field
            type="number"
            name="reorder_point"
            placeholder="ReorderPoint"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ReorderQuantity"
    >
        <Field
            type="number"
            name="reorder_quantity"
            placeholder="ReorderQuantity"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  
    <FormField label="Specification" hasTextareaHeight>
        <Field name="specification" as="textarea" placeholder="Specification" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

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

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

export default EditMaterialsPage
