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/product_brush_kits/product_brush_kitsSlice'
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 EditProduct_brush_kits = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    product: null,
    

    
    
    
    'kit_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    brushes: [],
    
    
    

    

    

    
    brush_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { product_brush_kitsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Product' labelFor='product'>
        <Field
            name='product'
            id='product'
            component={SelectField}
            options={initialValues.product}
            itemRef={'products'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="KitName"
    >
        <Field
            name="kit_name"
            placeholder="KitName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Brushes' labelFor='brushes'>
          <Field
              name='brushes'
              id='brushes'
              component={SelectFieldMany}
              options={initialValues.brushes}
              itemRef={'brush_types'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'name'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  
    <FormField
        label="BrushCount"
    >
        <Field
            type="number"
            name="brush_count"
            placeholder="BrushCount"
        />
    </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('/product_brush_kits/product_brush_kits-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditProduct_brush_kits
