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

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    category: '',
    

    

    

    

    
    
    
    'uom': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'standard_cost': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'last_cost': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'sale_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'reorder_point': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    stocked: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    active: false,
    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { materialsId } = router.query

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

  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: materialsId, 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="Name"
    >
        <Field
            name="name"
            placeholder="Name"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Category" labelFor="category">
        <Field name="category" id="category" component="select">
          
            <option value="lumber">lumber</option>
          
            <option value="sheet_goods">sheet_goods</option>
          
            <option value="fasteners">fasteners</option>
          
            <option value="hardware">hardware</option>
          
            <option value="adhesives">adhesives</option>
          
            <option value="wraps">wraps</option>
          
            <option value="metal_connectors">metal_connectors</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="UnitofMeasure"
    >
        <Field
            name="uom"
            placeholder="UnitofMeasure"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="LastCost"
    >
        <Field
            type="number"
            name="last_cost"
            placeholder="LastCost"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SalePrice"
    >
        <Field
            type="number"
            name="sale_price"
            placeholder="SalePrice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Stocked' labelFor='stocked'>
        <Field
            name='stocked'
            id='stocked'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Active' labelFor='active'>
        <Field
            name='active'
            id='active'
            component={SwitchField}
        ></Field>
    </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('/materials/materials-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMaterials
